0

I'm having a class which used to convert POJO to json string and compare it with another json string(read from logs). Recently we added 2 new boolean parameters to json and POJO and now the order of those 2 newly added variables is getting vary time to time.

I know by adding @JsonPropertyOrder(alphabetic = true) or @JsonPropertyOrder({ "att1", "att2", "att3" }) annotations, we can ensure the order. But I just want to know, how we got the same output earlier(with same order. Note: we had that code since 5years back)

Please find the sample code :

package com.....api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;


public class TestClass {

    public String writeAsJson(Object object) throws JsonProcessingException {
        JsonMapper.Builder jsonMapperBuilder = JsonMapper.builder();
        jsonMapperBuilder.addModule(new Jdk8Module())
                        .addModule((new JavaTimeModule()))
                        .addModule((new SimpleModule()))
                        .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
                        .disable(new SerializationFeature[]{SerializationFeature.WRITE_DATES_AS_TIMESTAMPS})
                        .enable(new MapperFeature[]{MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS})
                        .disable(new MapperFeature[]{MapperFeature.DEFAULT_VIEW_INCLUSION})
                        .disable(new DeserializationFeature[]{DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES});
        ObjectMapper objectMapper = jsonMapperBuilder.build();
        return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
    }
}

As per I checked, we can not ensure the order of the variables in the POJO when it serializing and deserializing json<--> POJO. But I'm curious how we got the same output for such a long period and suddenly how it fail after adding 2 new boolean variables.

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • Apart from adding new fields by any chance dependeny of the jackson or spring-web was also modified or upgraded – Jay Yadav Jun 22 '23 at 02:27

0 Answers0