2

I'm having problems to de-serialize an object containing a JSON field in spring. I have a DataTable data structure that has both String fields and a field that should contain a JSONObject:

public class DataTable {

        private String identifier;  
        private String version;         
        private JSONObject content;

        //getters and setters 
}

I want to be able to persist this object and retrieve it later on. When I post such a DataTable object to my Controller and try to retrieve it later, my content field will be empty, regardless of the contents I posted:

{
  "identifier": "id1",
  "version": "0.0.1",
  "content": {
    "empty": true
  }
}

It seems that jackson is not able to correctly de-serialize the JSONObject type field and will just leave it empty instead. How can I make it deserialize the field correctly?

T A
  • 1,677
  • 4
  • 21
  • 29
  • Not sure but I think using Gson will solve your problem. [See:](https://stackoverflow.com/questions/38636254/how-to-convert-json-to-java-object-using-gson) – kiranbirajdar Aug 24 '18 at 11:38

3 Answers3

2

Jackson has the jackson-datatype-json-org module, which allows us to combine json.org objects with POJOs or deserialize to json.org objects as the top level object.

<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-json-org</artifactId>
  <version>${jackson2.version}</version>
</dependency>

Here is a test (don't forget to register the JsonOrgModule).

public class JsonOrgJacksonTest {

    private final String json =
            "{" +
            "  \"id\": \"one\"," +
            "  \"content\": {" +
            "    \"foo\": \"bar\"," +
            "    \"baz\": \"blah\"" +
            "  }" +
            "}";

    @Test
    public void testJsonOrg() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JsonOrgModule());

        Model model = mapper.readValue(json, Model.class);

        assertThat(model.getId()).isEqualTo("one");
        assertThat(model.getContent().get("foo")).isEqualTo("bar");
        assertThat(model.getContent().get("baz")).isEqualTo("blah");
    }

    public static class Model {
        private String id;
        private JSONObject content;

        public String getId() { return id; }
        public void setId(String id) { this.id = id; }

        public JSONObject getContent() { return content; }
        public void setContent(JSONObject content) { this.content = content; }
    }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
1

JSONObject is not supposed to be serialized. It is for an internal use. If you want to have a JSON representation of this data structure without a model (dedidated class) try converting it into a Map.

Map<String,Object> result =
    new ObjectMapper().readValue(dataTable.getContent(), HashMap.class);
Sebastian
  • 160
  • 1
  • 10
1

You can use a custom deserializer for this:

public class Test {
  public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    final SimpleModule module = new SimpleModule("configModule",   Version.unknownVersion());
    module.addDeserializer(DataTable.class, new DeSerializer());
    mapper.registerModule(module);
    // DataTable readValue = mapper.readValue(<xml source>);
  }
}

class DeSerializer extends StdDeserializer<DataTable> {

  protected DeSerializer() {
    super(DataTable.class);
  }

  @Override
  public DataTable deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    // use p.getText() and p.nextToken to navigate through the xml and construct DataTable object
    return new DataTable();

  }
}
S.K.
  • 3,597
  • 2
  • 16
  • 31