We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Build a JacksonAdapter fluid builder style
JacksonAdapter jacksonAdapter = JacksonAdapter.newBuilder() .jsonFactory(HUMAN_READABLE_JSON_FACTORY) .build();
Use it to build Jsonb
Jsonb jsonb = Jsonb.newBuilder() .adapter(jacksonAdapter) .add(new MyComponent()) .build();
Full example:
class JacksonAdapterTest { public static final JsonFactory JSON_FACTORY = new JsonFactory() .configure(JsonParser.Feature.ALLOW_COMMENTS, true) .configure(JsonParser.Feature.ALLOW_YAML_COMMENTS, true) .configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true) // required to read line breaks .configure(JsonParser.Feature.ALLOW_TRAILING_COMMA, true); public static final JsonFactory HUMAN_READABLE_JSON_FACTORY = JSON_FACTORY.copy() .setCharacterEscapes(new HumanReadableCharacterEscapes()); @Test void custom_JsonFactory() { JacksonAdapter jacksonAdapter = JacksonAdapter.newBuilder() .jsonFactory(HUMAN_READABLE_JSON_FACTORY) .build(); Jsonb jsonb = Jsonb.newBuilder() .adapter(jacksonAdapter) .add(new MyComponent()) .build(); String jc = "\n// a comment \n ## yaml-comment \n{\"street\":\"my-street\nwith-new-lines\tand-tabs\"}"; Address address = jsonb.type(Address.class).fromJson(jc); assertThat(address.street()).isEqualTo("my-street\n" + "with-new-lines\tand-tabs"); Address withHumanNewLinesEtc = new Address(); withHumanNewLinesEtc.street("my-street\nwith-new-lines\tand-tabs"); String asJson = jsonb.toJson(withHumanNewLinesEtc); assertThat(asJson).isEqualTo("{\"street\":\"my-street\n" + "with-new-lines\tand-tabs\"}"); } private static class HumanReadableCharacterEscapes extends CharacterEscapes { private static final long serialVersionUID = 1L; private static final int[] asciiEscapes = CharacterEscapes.standardAsciiEscapesForJSON(); static { asciiEscapes['\t'] = CharacterEscapes.ESCAPE_NONE; asciiEscapes['\r'] = CharacterEscapes.ESCAPE_NONE; asciiEscapes['\n'] = CharacterEscapes.ESCAPE_NONE; } @Override public SerializableString getEscapeSequence(final int ch) { return null; } @Override public int[] getEscapeCodesForAscii() { return asciiEscapes; } } }
The text was updated successfully, but these errors were encountered:
#20 - Add builder for JacksonAdapter
7e960a8
rbygrave
No branches or pull requests
Build a JacksonAdapter fluid builder style
Use it to build Jsonb
Full example:
The text was updated successfully, but these errors were encountered: