Is your feature request related to a problem? Please describe.
no, this is my own need and idea.
Describe the solution you'd like
such as json:
{
"name": "root",
"child": {
"name": "child"
}
}
and i want to deserialize it into the following Java object:
public class TestPojo {
private String name;
private String child;
// getter setter
}
I hope the deserialization can be successful, and the result of deserialization is: name=root child={"name":"child"}
Usage example
@Test
public void acceptSubJsonTest() throws Exception {
String json = "{\"name\":\"root\"," +
"\"child\":{\"name\":\"child\"}," +
"\"children\":[{\"name\":\"children\"}]," +
"\"childrenList\":[{\"name\":\"childrenList\"}]}";
ObjectMapper mapper = new ObjectMapper();
TestPojo testPojo = mapper.readValue(json, TestPojo.class);
Assertions.assertEquals(testPojo.getChild(), "{\"name\":\"child\"}");
Assertions.assertEquals(testPojo.getChildren(), "[{\"name\":\"children\"}]");
Assertions.assertEquals(testPojo.getChildrenList().get(0), "{\"name\":\"childrenList\"}");
}
Additional context
I have submitted a PR to support this issue, please click here to view.
Comment From: pjfanning
Feels like a weird addition if we don't allow serialization like this. Serializing would lead to the text being included in the output JSON in escaped format (the opposite of what the deserializer does when this feature is enabled).
Comment From: kfyty
In fact, Object can be used to receive an uncertain JSON substring. But my actual scenario is migrating from fastjson to Jackson. This migration work will result in a large number of String type fields being modified to Object type. So I hope Jackson can also support similar deserialization.
Comment From: kfyty
In fact, Object can be used to receive an uncertain JSON substring. But my actual scenario is migrating from fastjson to Jackson. This migration work will result in a large number of String type fields being modified to Object type. So I hope Jackson can also support similar deserialization.
Sorry, I replied to you here and didn't directly reply to the comment @pjfanning
Comment From: yawkat
So basically a deserialization variant of @JsonRawValue
? Only works if the raw value is valid json of course, which is not required on the serialization side.
I could see it, but not like in #5232 – if anything, this should be a jackson-core feature as well.
Comment From: cowtowncoder
Yeah, as per my comment on PR, I don't think this is a good idea to add:.
To bind arbitrary sub-trees, one should use types Object
or JsonNode
and go from there
Comment From: cowtowncoder
So basically a deserialization variant of
@JsonRawValue
? Only works if the raw value is valid json of course, which is not required on the serialization side.I could see it, but not like in #5232 – if anything, this should be a jackson-core feature as well.
Right. As per my note, getting arbitrary sub-trees is done by binding to JsonNode
, extracting stuff from there (or re-serializing if that is desired).
Getting "raw" JSON (... or any other format) is something only specialized decoders/parsers can provide: Jackson JsonParser
does not (and likely never will).