4

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 object 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.

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
tarka
  • 5,289
  • 10
  • 51
  • 75

1 Answers1

0

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;
rmlan
  • 4,387
  • 2
  • 21
  • 28
  • I can't! I'm specifically using JsonNode because the object is dynamic. – tarka Nov 12 '16 at 15:13
  • You should mention that in your question. – rmlan Nov 12 '16 at 15:13
  • The problem is that the `Object` arrives into the system as a JSON string and has to be converted to java object. So even if I define it as an `Object` its still a `JsonNode`. – tarka Nov 12 '16 at 15:34
  • Are you sure about that? I am unable to reproduce that behavior with a simple Jackson ObjectMapper, your provided Json document, and the data structure from my edited answer. The type of the templateOptions field is LinkedHashMap, _not_ JsonNode – rmlan Nov 12 '16 at 15:50
  • Yes but the `Object` inside the `Map` is a `JsonNode` and ends up with the same problem. The only difference is that its now wrapped in a `Map`. – tarka Nov 12 '16 at 15:52
  • I'm not sure you understood what I am saying. In my test, the `Object` inside the Map was yet another Map (LinkedHashMap, specifically). When you say it "is a JsonNode", are you saying this from experience, or is that just a conjecture? – rmlan Nov 12 '16 at 16:06
  • I think there is a miscommunication. I really cant change the fact that the `model` and the `field` are `JsonNode`. The question is more targeted at how to manage the serialization given that I don't have direct control of the Lambda Serializer. – tarka Nov 12 '16 at 16:19
  • So you can't change the sample object that you posted? – rmlan Nov 12 '16 at 16:39
  • 2
    I cant change the `Type` of the `model` and 'field`. They both need to be `JsonNode`. – tarka Nov 12 '16 at 16:52
  • 3
    I am facing exactly the same issue, due to in our system deserialisation is happening with `com.fasterxml.jackson.databind.JsonNode` and we are serialising with `org.codehaus.jackson.JsonNode` – Evilguy Apr 09 '18 at 15:58