package com.pcalouche.spat.restservices.api;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/api/test")
public class TestController {

    @PostMapping(value = "/json-node")
    public void parseJsonNode(@RequestBody JsonNode jsonNode) {
        if (jsonNode.get("y") != null) {
            System.out.println(jsonNode.get("y"));
            System.out.println(jsonNode.get("y").getNodeType());
        }
    }

    @PostMapping(value = "/object-node")
    public void parseObjectNode(@RequestBody ObjectNode objectNode) {
        if (objectNode.get("y") != null) {
            System.out.println(objectNode.get("y"));
            System.out.println(objectNode.get("y").getNodeType());
        }
        objectNode.put("u", 42);
        System.out.println(objectNode.get("u"));
        System.out.println(objectNode.get("u").getNodeType());
    }
}

@@ -83,24 +83,130 @@
 d.JsonNode;%0A
+import com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.fasterxml.jackson.databind.node.ArrayNode;%0A
 import com.f
@@ -499,222 +499,1040 @@
 r;%0A%0A
-@RestController%0A@RequestMapping(value = %22/api/test%22)%0Apublic class TestController %7B%0A%0A    @PostMapping(value = %22/json-node%22)%0A    public void parseJsonNode(@RequestBody JsonNode jsonNode) %7B%0A        if (jsonNode.get(%22y
+/**%0A * Rough example to show using Jackson's JsonNode and ObjectNode to parse some%0A * arbitrary JSON.  Assume your have a POST with a JSON request body as follows:%0A * %3Cpre%3E%0A * %7B%0A * %09%22formType%22: %22formA%22,%0A * %09%22formData%22: %7B%0A * %09%09%22field1%22: %22field1Value%22,%0A * %09%09%22comments%22: %5B%0A *                        %7B%0A * %09%09%09%09%22id%22: 1,%0A * %09%09%09%09%22text%22: %22comment 1 text%22%0A *            %7D,%0A *            %7B%0A * %09%09%09%09%22id%22: 2,%0A * %09%09%09%09%22text%22: %22comment 2 text%22%0A *            %7D%0A * %09%09%5D%0A *    %7D%0A * %7D%0A */%0A@RestController%0A@RequestMapping(value = %22/api/test%22)%0Apublic class TestController %7B%0A    private final ObjectMapper objectMapper;%0A%0A    public TestController(ObjectMapper objectMapper) %7B%0A        this.objectMapper = objectMapper;%0A    %7D%0A%0A    // JsonNode is immutable.  Great if you just need to read the arbitrary JSON%0A    @PostMapping(value = %22/json-node%22)%0A    public void parseJsonNode(@RequestBody JsonNode jsonNode) %7B%0A        // Probably could be made more elegant to check if keys exists or not%0A        if (jsonNode.get(%22formType%22) != null && jsonNode.get(%22formData
 %22) !
@@ -1578,33 +1578,40 @@
 n(jsonNode.get(%22
-y
+formType
 %22));%0A           
@@ -1636,33 +1636,98 @@
 n(jsonNode.get(%22
-y
+formData%22));%0A            System.out.println(jsonNode.get(%22formData
 %22).getNodeType()
@@ -1720,32 +1720,49 @@
 .getNodeType());
+ // Prints Object
 %0A        %7D%0A    %7D
@@ -1759,24 +1759,81 @@
    %7D%0A    %7D%0A%0A
+    // Use ObjectNode if you need to manipulate the JSON%0A
     @PostMap
@@ -1959,17 +1959,62 @@
 de.get(%22
-y
+formType%22) != null && objectNode.get(%22formData
 %22) != nu
@@ -2066,17 +2066,24 @@
 de.get(%22
-y
+formType
 %22));%0A   
@@ -2130,51 +2130,228 @@
 et(%22
-y%22).getNodeType());%0A        %7D%0A        objec
+formData%22));%0A            System.out.println(objectNode.get(%22formData%22).getNodeType());%0A        %7D%0A        // Adds a new comment to the array%0A        ObjectNode commentNode = objectMapper.createObjectNode();%0A        commen
 tNod
@@ -2361,14 +2361,14 @@
 ut(%22
-u%22, 42
+id%22, 3
 );%0A 
@@ -2378,48 +2378,134 @@
     
-System.out.println(objectNode.get(%22u%22));
+commentNode.put(%22text%22, %22comment 3 text%22);%0A        ((ArrayNode) objectNode.get(%22formData%22).get(%22comments%22)).add(commentNode);%0A
 %0A   
@@ -2548,24 +2548,33 @@
 et(%22
-u%22).getNodeType(
+formData%22).get(%22comments%22
 ));%0A

update example