[英]How can I serialize a Java object that has one member which is a json string
I have a Pojo that contains one member displayPropsJson
which is a clientside json string. 我有一个Pojo,其中包含一个成员
displayPropsJson
,这是一个客户端json字符串。 It is validated with a JSON schema before storing on the server. 在存储在服务器上之前,将使用JSON模式对其进行验证。 ie
即
public class Item {
Long id; //23
String name; //"itemsName"
String displayPropsJson; // "{\"bold\" : true, \"htmlAllowed\" : true, \"icon\" :\"star.jpg\" }"
}
I'd like the serialized version of this to output the displayPropsJson as displayProps sub object for example: 我想要此的序列化版本将displayPropsJson作为displayProps子对象输出:
{
"id" :23,
"name: : "itemsName",
"displayProps" : {
"bold" : true,
"htmlAllowed" : true,
"icon" : "star.jpg"
}
} }
How can I do this with a Jackson serializer that outputs elements and the json string as json? 我该如何使用将元素和json字符串输出为json的Jackson序列化程序来做到这一点? The displayPropsJson will vary but is always valid json.
displayPropsJson会有所不同,但始终是有效的json。
Yes I am sure this can be done with custom Jackson serializer. 是的,我确定可以使用自定义Jackson序列化程序来完成。 Another thing you could do is implement JsonSerializable, }
您可以做的另一件事是实现JsonSerializable,}
Yet another possibility is to implement the JsonSerializable interface 另一个可能性是实现JsonSerializable接口
A final possibility would be to switch libraries and use Google's GSON , which makes it easy to serialize objects into and out of json. 最后一种可能性是切换库并使用Google的GSON ,这使得将对象序列化为json和从json序列化变得容易。
You can consider two options apart of creating a custom serializer. 除了创建自定义序列化程序外,您可以考虑两个选项。
@JsonRawString
annotation to mark a String field that should be serialized as is without quoting of characters. @JsonRawString
批注来标记@JsonRawString
序列化而不带引号的String字段。 ObjectMapper
available inside your object instance ( consider value injection ) and provide a getter method that returns JsonNode
deserialized from your json string value. ObjectMapper
在对象实例内部可用( 考虑值注入 ),并提供一个getter方法,该方法返回从json字符串值反序列化的JsonNode
。 Here is an example demonstrating both: 这是展示这两个示例的示例:
public class JacksonRawString {
public static class Item {
final private ObjectMapper mapper;
public Long id = 23l;
public String name = "itemsName";
@JsonRawValue
public String displayPropsJson = "{\"bold\" : true, \"htmlAllowed\" : true, " +
"\"icon\" :\"star.jpg\" }";
public JsonNode getDisplayPropsJson2() throws IOException {
return mapper.readTree(displayPropsJson);
}
public Item(ObjectMapper mapper) {
this.mapper = mapper;
}
}
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
System.out.println(
mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Item(mapper)));
}
}
Output: 输出:
{
"id" : 23,
"name" : "itemsName",
"displayPropsJson" : {"bold" : true, "htmlAllowed" : true, "icon" :"star.jpg" },
"displayPropsJson2" : {
"bold" : true,
"htmlAllowed" : true,
"icon" : "star.jpg"
}
}
Note that the displayPropsJson2
get pretty output since it was serialized as JsonNode
请注意,由于
displayPropsJson2
被序列化为JsonNode
,因此获得了漂亮的输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.