How to compare JSON documents and return the differences with Jackson or Gson?

Reading the JSON documents as Maps and comparing them You could read both JSON documents as Map<K, V>. See the below examples for Jackson and Gson: ObjectMapper mapper = new ObjectMapper(); TypeReference<HashMap<String, Object>> type = new TypeReference<HashMap<String, Object>>() {}; Map<String, Object> leftMap = mapper.readValue(leftJson, type); Map<String, Object> rightMap = mapper.readValue(rightJson, type); Gson gson = new … Read more

How do I deserialize timestamps that are in seconds with Jackson?

I wrote a custom deserializer to handle timestamps in seconds (Groovy syntax). class UnixTimestampDeserializer extends JsonDeserializer<DateTime> { Logger logger = LoggerFactory.getLogger(UnixTimestampDeserializer.class) @Override DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { String timestamp = jp.getText().trim() try { return new DateTime(Long.valueOf(timestamp + ‘000’)) } catch (NumberFormatException e) { logger.warn(‘Unable to deserialize timestamp: ‘ + timestamp, e) … Read more

Exception in thread “main” java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonFactory.requiresPropertyOrdering()Z

I was getting the exactly same issue. I was using Maven for dependency management and had added dependency for jackson-databind module only like this <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> and then I resolved it by doing this.. I added its transitive dependencies explicitly with the same jackson.version mentioned for each of them in the pom.xml … Read more

How to distinguish between null and not provided values for partial updates in Spring Rest Controller

Another option is to use java.util.Optional. import com.fasterxml.jackson.annotation.JsonInclude; import java.util.Optional; @JsonInclude(JsonInclude.Include.NON_NULL) private class PersonDTO { private Optional<String> firstName; private Optional<String> lastName; /* getters and setters … */ } If firstName is not set, the value is null, and would be ignored by the @JsonInclude annotation. Otherwise, if implicitly set in the request object, firstName would … Read more

Deserializing an enum with Jackson

EDIT: Starting from Jackson 2.6, you can use @JsonProperty on each element of the enum to specify its serialization/deserialization value (see here): public enum Status { @JsonProperty(“ready”) READY, @JsonProperty(“notReady”) NOT_READY, @JsonProperty(“notReadyAtAll”) NOT_READY_AT_ALL; } (The rest of this answer is still valid for older versions of Jackson) You should use @JsonCreator to annotate a static method … Read more

Casting LinkedHashMap to Complex Object

You can use ObjectMapper.convertValue(), either value by value or even for the whole list. But you need to know the type to convert to: POJO pojo = mapper.convertValue(singleObject, POJO.class); // or: List<POJO> pojos = mapper.convertValue(listOfObjects, new TypeReference<List<POJO>>() { }); this is functionally same as if you did: byte[] json = mapper.writeValueAsBytes(singleObject); POJO pojo = mapper.readValue(json, … Read more