Skip to content

Instantly share code, notes, and snippets.

@tabdulradi
Created April 22, 2015 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tabdulradi/c11e710e972ec0795818 to your computer and use it in GitHub Desktop.
Save tabdulradi/c11e710e972ec0795818 to your computer and use it in GitHub Desktop.
public final class ImmutableDummy {
public final Integer x;
public final Option<Integer> y;
@JsonCreator
public ImmutableDummy(@JsonProperty("x") Integer x,
@JsonProperty("y") Integer y) {
this.x = x;
this.y = Option.apply(y);
}
}
@Test
public void test() throws IOException {
String json1 = "{\n" +
" \"x\": 1,\n" +
" \"y\": 2\n" +
"}";
String json2 = "{\n" +
" \"x\": 1\n" +
"}";
String json3 = "{\n" +
" \"x\": 1,\n" +
" \"y\": null\n" +
"}";
String json4 = "{\n" +
" \"x\": 1,\n" +
" \"y\": \"t\"\n" +
"}";
ImmutableDummy result1 = SingleObjectMapper.mapper().readValue(json1, ImmutableDummy.class);
assertThat(result1.x, is(1));
assertThat(result1.y, is(Option.apply(2)));
ImmutableDummy result2 = SingleObjectMapper.mapper().readValue(json2, ImmutableDummy.class);
assertThat(result2.x, is(1));
assertThat(result2.y.isDefined(), is(false));
ImmutableDummy result3 = SingleObjectMapper.mapper().readValue(json3, ImmutableDummy.class);
assertThat(result3.x, is(1));
assertThat(result3.y.isDefined(), is(false));
ImmutableDummy result4 = SingleObjectMapper.mapper().readValue(json4, ImmutableDummy.class);
assertThat(result4.x, is(1));
assertThat(result4.y.isDefined(), is(false));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment