skip to Main Content

I’m having json type in my Postgres DB and getting it as POJO.
Then I’m trying to write my POJO to String.
POJO is being populated correctly, but when Jackson writes it to String, I get {"traversableAgain" : true, "empty" : false} for nested JSON whose type in POJO is Object.

My POJO:

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MyClass {

  private String property1;
  private Double property2;
  private Boolean property3;
  private Object problematicProperty;

}

JSON I’m retrieving from DB:

{
"property1" : "randomString",
"property2" : 0.3,
"property3" : false,
"problematicProperty" :  {
                "yearFromDate": [
                    "dob",
                    "yyyyMMdd"
                ]
            }
}

My code:

MyClass myPOJO = myRepository.getOne(pojoID);
ObjectMapper obj = getObjectMapper();
obj.enable(SerializationFeature.INDENT_OUTPUT);
obj.enable(SerializationFeature.WRAP_ROOT_VALUE);

String stringLayout = obj
        .writerWithDefaultPrettyPrinter()
        .writeValueAsString(myPOJO);

When I print stringLayout, I get:

{
"property1" : "randomString",
"property2" : 0.3,
"property3" : false,
"problematicProperty" :  {"traversableAgain" : true, "empty" : false}
}

instead of

{
"property1" : "randomString",
"property2" : 0.3,
"property3" : false,
"problematicProperty" :  {
                "yearFromDate": [
                    "dob",
                    "yyyyMMdd"
                ]
            }
}

I used java.lang.Object type because problematicProperty is "dynamic", sometimes it will be nested json like above, and sometimes string e.g. like this:

"problematicProperty" :  "randomText"

When testing, if I use com.fasterxml.jackson.databind.node.ObjectNode type instead of Object, string is properly being written. But I can’t use ObjectNode with Hibernate.

Why is this happening and is there a way to configure objectMapper to write java.lang.Object(that is actually a nested JSON) to String properly?

2

Answers


  1. Chosen as BEST ANSWER

    Inspired by this answer and this link I added

    objectMapper.registerModule(new DefaultScalaModule());
    

    And now it works fine and code looks like this:

    MyClass myPOJO = myRepository.getOne(pojoID);
    ObjectMapper obj = getObjectMapper();
    objectMapper.registerModule(new DefaultScalaModule());
    obj.enable(SerializationFeature.INDENT_OUTPUT);
    obj.enable(SerializationFeature.WRAP_ROOT_VALUE);
    
    String stringLayout = obj
        .writerWithDefaultPrettyPrinter()
        .writeValueAsString(myPOJO);
    

  2. A complete example would help here. If I create a dummy example from your code – see below, then it works as expected.

    The {"traversableAgain" : true, "empty" : false} JSON is coming from what is most likely a Scala type, probably a Scala Map. It’s difficult to say how to fix it without seeing where this data is coming from. One possible solution might be to change the problematicProperty field type to a (Java) Map, and convert the Scala Map when the object is constructed.

    import com.fasterxml.jackson.annotation.*;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.*;
    
    public class JacksonTest {
    
        @JsonIgnoreProperties(ignoreUnknown = true)
        @JsonInclude(JsonInclude.Include.NON_NULL)
        public static class MyClass {
    
            private String property1;
            private Double property2;
            private Boolean property3;
            private Object problematicProperty;
    
            public MyClass() {}
    
            public MyClass(String property1, Double property2, Boolean property3, Object problematicProperty) {
                this.property1 = property1;
                this.property2 = property2;
                this.property3 = property3;
                this.problematicProperty = problematicProperty;
            }
    
            // getters and setters
        }
    
        private static final String TEST_JSON = "{n" +
                ""property1" : "randomString",n" +
                ""property2" : 0.3,n" +
                ""property3" : false,n" +
                ""problematicProperty" :  {n" +
                "                "yearFromDate": [n" +
                "                    "dob",n" +
                "                    "yyyyMMdd"n" +
                "                ]n" +
                "            }n" +
                "}";
    
        public static void main(String[] args) throws JsonProcessingException {
            final ObjectMapper obj = new ObjectMapper();
            obj.enable(SerializationFeature.INDENT_OUTPUT);
            obj.enable(SerializationFeature.WRAP_ROOT_VALUE);
    
            MyClass myPOJO = obj.readValue(TEST_JSON, MyClass.class);
    
            final String json = obj
                    .writerWithDefaultPrettyPrinter()
                    .writeValueAsString(myPOJO);
    
            System.out.println(json);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search