-
Notifications
You must be signed in to change notification settings - Fork 4
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
Comments
Would be cool with a small example in the readme file (or a link to it) 👍 |
Sure. All contributions are welcome =) |
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);
} |
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. |
No description provided.
The text was updated successfully, but these errors were encountered: