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 JsonHelper #2001

Closed
DamnClin opened this issue Jun 7, 2022 · 2 comments · Fixed by #2579
Closed

Add JsonHelper #2001

DamnClin opened this issue Jun 7, 2022 · 2 comments · Fixed by #2579

Comments

@DamnClin
Copy link
Collaborator

DamnClin commented Jun 7, 2022

JsonHelper is a tool to help unit test of json serialization / deserialization

public final class JsonHelper {

  private static final ObjectMapper jsonMapper = jsonMapper();

  private JsonHelper() {}

  public static ObjectMapper jsonMapper() {
    return JsonMapper
      .builder()
      .serializationInclusion(JsonInclude.Include.NON_EMPTY)
      .addModule(new JavaTimeModule())
      .addModules(new Jdk8Module())
      .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
      .disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY)
      .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
      .build();
  }

  public static <T> String writeAsString(T object) {
    try {
      return jsonMapper.writeValueAsString(object);
    } catch (JsonProcessingException e) {
      throw new AssertionError("Error serializing object: " + e.getMessage(), e);
    }
  }

  public static <T> T readFromJson(String json, Class<T> clazz) {
    try {
      return jsonMapper.readValue(json, clazz);
    } catch (IOException e) {
      throw new AssertionError("Error reading value from json: " + e.getMessage(), e);
    }
  }
}
@pascalgrimaud
Copy link
Member

When do you think we should generate this? Java base or something related to Spring Boot? I'd say the 2nd option.

@DamnClin
Copy link
Collaborator Author

I think we should add it it spring MVC.

When done we'll need to use that in code for dummy feature (#2444)

This was referenced Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants