简体   繁体   English

Jackson JsonNode序列化

[英]Jackson JsonNode Serialization

I'm using AWS Lambda with a Java 8 function. 我正在将AWS Lambda与Java 8函数一起使用。 Lambda has a builtin Jackson Serializer so when your method returns an object it serializes it to a JSON representation. Lambda具有内置的Jackson序列化器,因此当您的方法返回一个对象时,它将其序列化为JSON表示形式。

I have an onject that is made up of the following properties: 我有一个由以下属性组成的对象:

private String name;
private JsonNode model;
private JsonNode field;

I've omitted all the rest of the class for simplicity but it has getters / setters etc. 为了简单起见,我省略了所有其他类,但是它具有getters / setters等。

Normally when I run this in my native application it works perfectly. 通常,当我在本机应用程序中运行它时,它可以完美运行。 The JsonNode Tree structure is rendered as a JSON. JsonNode树结构呈现为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
        }
    }
}

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. 但是,由于某种未知的原因,当我在Lambda中运行此代码时,实际的JsonNode对象本身(而不是树,而是包装对象)已被序列化。 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? 是否有人对此有何见解以及对解决方案/解决方法的任何建议?

UPDATE: 更新:

I'm specifically using a JsonNode because the model and field are dynamic and are provided at runtime. 我特别使用JsonNode,因为modelfield是动态的,并且在运行时提供。 So I wont know the structure ahead of time. 所以我不会提前知道结构。

Provided that "model" and "field" will always be objects, and not arrays, you could use a Map<String, Object> for them. 假设“模型”和“字段”始终是对象,而不是数组,则可以为它们使用Map<String, Object> For child objects, simply add other maps as the value. 对于子对象,只需添加其他映射作为值。

private String name;
private Map<String, Object> model;
private Map<String, Object> field;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM