Jackson JsonNode Serialization

2019-09-13 05:03发布

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条回答
地球回转人心会变
2楼-- · 2019-09-13 05:50

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;
查看更多
登录 后发表回答