Is this possible? I want to deserialise a JSON into a POJO structure but in addition to this, save a copy of the raw json in the POJO (or sub POJO). For example, lets say I have this structure:
{
"test": 123,
"testStr": "foo",
"testSubModel": {
"testStr2": "foobar",
"testFloat2": 1.2
}
}
now I have a simple set of two POJOs:
package test.model;
public class TestModel {
private int test;
private String testStr;
private TestSubModel testSubModel;
public int getTest() {
return test;
}
public String getTestStr() {
return testStr;
}
public TestSubModel getTestSubModel() {
return testSubModel;
}
}
package test.model;
public class TestSubModel {
private String testStr2;
private float testFloat2;
private String rawJson; // i want this to contain something like { "testStr2": "foobar", "testFloat2": 1.2 }
public String getTestStr2() {
return testStr2;
}
public float getTestFloat2() {
return testFloat2;
}
}
is it possible to get the rawJson in TestSubModel set with the full JSON of the class in addition to getting the pojo fields set correctly?
whilst I can re-invent this with a custom method, any extra JSON fields that I didn't map, gets lost which I want to keep for exception logging purposes (i.e. I need the raw JSON sent by the upstream system and not a res-constructed one that may miss fields that i don't normally store in the POJO).
I was hoping there was a way of doing this with an annotation (but don't think its there) or a custom post de-serializer hook (so that Jackson does its usual stuff to map the object without me having to write this code all myself for each applicable class). I tried something with a DelegatingDeserializer but the JsonParser isn't repeatable, as in when I read it once it wasn't reusable to call Object deserializedObject = super.deserialize(p, ctxt); in addition to getting the tree out and converting to a string.