package com.pcalouche.spat.restservices.api;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
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;

/**
 * Rough example to show using Jackson's JsonNode and ObjectNode to parse some
 * arbitrary JSON.  Assume your have a POST with a JSON request body as follows:
 * <pre>
 * {
 * 	"formType": "formA",
 * 	"formData": {
 * 		"field1": "field1Value",
 * 		"comments": [
 *                        {
 * 				"id": 1,
 * 				"text": "comment 1 text"
 *            },
 *            {
 * 				"id": 2,
 * 				"text": "comment 2 text"
 *            }
 * 		]
 *    }
 * }
 */
@RestController
@RequestMapping(value = "/api/test")
public class TestController {
    private final ObjectMapper objectMapper;

    public TestController(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    // JsonNode is immutable.  Great if you just need to read the arbitrary JSON
    @PostMapping(value = "/json-node")
    public void parseJsonNode(@RequestBody JsonNode jsonNode) {
        // Probably could be made more elegant to check if keys exists or not
        if (jsonNode.get("formType") != null && jsonNode.get("formData") != null) {
            System.out.println(jsonNode.get("formType"));
            System.out.println(jsonNode.get("formData"));
            System.out.println(jsonNode.get("formData").getNodeType()); // Prints Object
        }
    }

    // Use ObjectNode if you need to manipulate the JSON
    @PostMapping(value = "/object-node")
    public void parseObjectNode(@RequestBody ObjectNode objectNode) {
        if (objectNode.get("formType") != null && objectNode.get("formData") != null) {
            System.out.println(objectNode.get("formType"));
            System.out.println(objectNode.get("formData"));
            System.out.println(objectNode.get("formData").getNodeType());
        }
        // Adds a new comment to the array
        ObjectNode commentNode = objectMapper.createObjectNode();
        commentNode.put("id", 3);
        commentNode.put("text", "comment 3 text");
        ((ArrayNode) objectNode.get("formData").get("comments")).add(commentNode);

        System.out.println(objectNode.get("formData").get("comments"));
    }
}

@@ -720,17 +720,18 @@
 : %7B%0A * %09
-%09
+  
 %22field1%22
@@ -751,17 +751,18 @@
 e%22,%0A * %09
-%09
+  
 %22comment
@@ -769,34 +769,16 @@
 s%22: %5B%0A *
-                  
       %7B%0A
@@ -778,26 +778,26 @@
      %7B%0A * %09%09
-%09%09
+  
 %22id%22: 1,%0A * 
@@ -794,26 +794,26 @@
 d%22: 1,%0A * %09%09
-%09%09
+  
 %22text%22: %22com
@@ -823,30 +823,24 @@
 t 1 text%22%0A *
-      
       %7D,%0A * 
@@ -848,23 +848,17 @@
     
-  
-    
 %7B%0A * %09%09
-%09%09
+  
 %22id%22
@@ -867,18 +867,18 @@
 2,%0A * %09%09
-%09%09
+  
 %22text%22: 
@@ -906,26 +906,20 @@
     
-   
-   
 %7D%0A * %09
-%09
+  
 %5D%0A *
- 
    %7D

fix JSON formatting