Jackson JsonNode序列化 [英] Jackson JsonNode Serialization
问题描述
我正在使用带有Java的 AWS Lambda 8功能。 Lambda有一个内置的Jackson Serializer,所以当你的方法返回一个对象时,它会将它序列化为JSON表示。
I'm using AWS Lambda with a Java 8 function. Lambda has a builtin Jackson Serializer so when your method returns an object it serializes it to a JSON representation.
我有一个由以下属性组成的onject:
I have an onject that is made up of the following properties:
private String name;
private JsonNode model;
private JsonNode field;
为简单起见,我省略了所有其余的类,但它有getter / setters等。
I've omitted all the rest of the class for simplicity but it has getters / setters etc.
通常当我在我的原生应用程序中运行它时,它运行得很好。 JsonNode
树结构呈现为JSON。例如:
Normally when I run this in my native application it works perfectly. The JsonNode
Tree structure is rendered as a JSON. For example:
{
"name": "example",
"model": {
"key": "ipAddress",
"type": "input",
"templateOptions": {
"label": "IP",
"placeholder": "Something",
"description": "The IP address.",
"required": true
}
},
"field": {
"key": "pro",
"type": "input",
"templateOptions": {
"label": "Pro",
"placeholder": "Something",
"description": "Pro Example",
"required": false
}
}
}
但是,由于某些未知原因,当我在Lambda中运行它时,实际的JsonNode对象本身(不是树而是包装器对象)是序列化。所以我得到了这个:
However, for some unknown reason when I run this in Lambda the actual JsonNode object itself (not the tree but the wrapper object) is serialized. So I'm getting this instead:
{
"name": "example",
"model": {
"nodeType": "NULL",
"array": false,
"null": true,
"valueNode": true,
"containerNode": false,
"missingNode": false,
"object": false,
"pojo": false,
"number": false,
"integralNumber": false,
"floatingPointNumber": false,
"short": false,
"int": false,
"long": false,
"float": false,
"double": false,
"bigDecimal": false,
"bigInteger": false,
"textual": false,
"boolean": false,
"binary": false
},
"fields": {
"nodeType": "ARRAY",
"array": true,
"null": false,
"valueNode": false,
"containerNode": true,
"missingNode": false,
"object": false,
"pojo": false,
"number": false,
"integralNumber": false,
"floatingPointNumber": false,
"short": false,
"int": false,
"long": false,
"float": false,
"double": false,
"bigDecimal": false,
"bigInteger": false,
"textual": false,
"boolean": false,
"binary": false
},
"schedule": "0 0/1 * 1/1 * ? *"
}
有没有人知道为什么会这样,以及任何建议解决方法/解决方法?
Does anybody have any insight as to why this is happening and any suggestions for solutions / workarounds?
更新:
我特意使用JsonNode,因为 model
和 field
是动态的,在运行时提供。所以我不会提前知道结构。
I'm specifically using a JsonNode because the model
and field
are dynamic and are provided at runtime. So I wont know the structure ahead of time.
推荐答案
如果model和field始终是对象,而不是数组,则可以使用 Map< String,对象>
对于他们。对于子对象,只需添加其他地图作为值。
Provided that "model" and "field" will always be objects, and not arrays, you could use a Map<String, Object>
for them. For child objects, simply add other maps as the value.
private String name;
private Map<String, Object> model;
private Map<String, Object> field;
这篇关于Jackson JsonNode序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!