Jackson JsonNode Serialization

有些话、适合烂在心里 提交于 2019-12-13 07:28:44

问题


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.

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.

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
        }
    }
}

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?

UPDATE:

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.


回答1:


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;


来源:https://stackoverflow.com/questions/40564031/jackson-jsonnode-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!