Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs #11

Open
hamnis opened this issue Jan 25, 2017 · 4 comments
Open

Docs #11

hamnis opened this issue Jan 25, 2017 · 4 comments

Comments

@hamnis
Copy link
Owner

hamnis commented Jan 25, 2017

No description provided.

@esiqveland
Copy link
Contributor

Would be cool with a small example in the readme file (or a link to it) 👍

@hamnis
Copy link
Owner Author

hamnis commented Jun 12, 2017

Sure. All contributions are welcome =)

@esiqveland
Copy link
Contributor

How about something like this in a 'Usage' section, or maybe a link to a test file with something like it is easier to maintain:

Usage

    static class Event {
        public final UUID ID;
        public final List<String> tags;
        public final String message;

        public Event(UUID ID, List<String> tags, String message) {
            this.ID = ID;
            this.tags = tags;
            this.message = message;
        }
    }

    @Test
    public void testEvent() throws Exception {
        String expected = "{\"id\":\"1e2f28ff-54b5-4ad4-9edb-36712dc52202\",\"tags\":[\"travel\",\"code\"],\"message\":\"This is a test\"}";

        // create a decoder for our Event
        DecodeJson<Event> decode = Decoders.decode(
                FieldDecoder.TString("id").tryNarrow(UUID::fromString),
                FieldDecoder.TJArray("tags")
                        .mapToList(jValue -> jValue.asString().getOrElse(""))
                        .withDefaultValue(List.empty()),
                FieldDecoder.TString("message"),
                Event::new
        );

        // create an encoder that will encode our Event into json
        EncodeJson<Event> encode = Encoders.encode(
                FieldEncoder.typedFieldOf("id", Encoders.EString.contramap(UUID::toString)),
                FieldEncoder.EList("tags", Encoders.EString),
                FieldEncoder.EString("message")
        ).contramap(event -> Tuple.of(event.ID, event.tags, event.message));

        // a codec can both encode and decode a value
        JsonCodec<Event> codec = JsonCodec.lift(decode, encode);

        // parse raw json with help of jackson parser
        Json.JValue jValue = new JacksonStreamingParser().parse(expected);

        String json = codec
                .toJson(codec.fromJson(jValue).unsafeGet())
                .nospaces();

        assertThat(json).isEqualTo(expected);
    }

@hamnis
Copy link
Owner Author

hamnis commented Jun 14, 2017

Great! Something like this is very useful. PR?

I have had some plans with creating a github site, but I seem to hit a wall every time I try starting with that.
Any help with that would be GREATLY appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants