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

Add builder for JacksonAdapter #20

Closed
rbygrave opened this issue Apr 21, 2022 · 0 comments
Closed

Add builder for JacksonAdapter #20

rbygrave opened this issue Apr 21, 2022 · 0 comments
Assignees
Milestone

Comments

@rbygrave
Copy link
Contributor

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;
    }
  }

}
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

1 participant