package com.pearson.statsagg.utilities.json_utils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.pearson.statsagg.utilities.core_utils.StackTrace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Jeffrey Schmidt
 */
public class JsonUtils {

    private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class.getName());

    public static Integer getIntegerFieldFromJsonObject(JsonObject jsonObject, String fieldName) {
        
        if (jsonObject == null) {
            return null;
        }
        
        Integer returnInteger = null;
        
        try {
            JsonElement jsonElement = jsonObject.get(fieldName);
            if (jsonElement != null) returnInteger = jsonElement.getAsInt();
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));    
        }

        return returnInteger;
    }
    
    public static String getStringFieldFromJsonObject(JsonObject jsonObject, String fieldName) {
        
        if (jsonObject == null) {
            return null;
        }
        
        String returnString = null;
        
        try {
            JsonElement jsonElement = jsonObject.get(fieldName);
            
            if (jsonElement != null) {
                JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive();
                returnString = jsonPrimitive.getAsString();
            }
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));    
        }

        return returnString;
    }
    
    public static Boolean getBooleanFieldFromJsonObject(JsonObject jsonObject, String fieldName) {
        
        if (jsonObject == null) {
            return null;
        }
        
        Boolean returnBoolean = null;
        
        try {
            JsonElement jsonElement = jsonObject.get(fieldName);
            if (jsonElement != null) returnBoolean = jsonElement.getAsBoolean();
        }
        catch (Exception e) {
            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));    
        }

        return returnBoolean;
    }
    
}

@@ -45,16 +45,45 @@
 utils;%0A%0A
+import com.google.gson.Gson;%0A
 import c
@@ -145,16 +145,51 @@
 Object;%0A
+import com.google.gson.JsonParser;%0A
 import c
@@ -2390,10 +2390,1136 @@
  %7D%0A    %0A
+    public static JsonObject getJsonObjectFromRequestBody(String json) %7B%0A        %0A        if (json == null) %7B%0A            return null;%0A        %7D%0A        %0A        JsonObject jsonObject = null;%0A        %0A        try %7B%0A            JsonParser parser = new JsonParser();%0A            JsonElement jsonElement = parser.parse(json);%0A            jsonObject = new Gson().toJsonTree(jsonElement).getAsJsonObject();%0A        %7D %0A        catch (Exception e) %7B%0A            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));%0A        %7D %0A        %0A        return jsonObject;%0A    %7D%0A    %0A    public static JsonElement getJsonElementFromRequestBody(String json) %7B%0A        %0A        if (json == null) %7B%0A            return null;%0A        %7D%0A        %0A        JsonElement jsonElement = null;%0A        %0A        try %7B%0A            JsonParser parser = new JsonParser();%0A            jsonElement = parser.parse(json);%0A        %7D %0A        catch (Exception e) %7B%0A            logger.error(e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e));%0A        %7D %0A        %0A        return jsonElement;%0A    %7D%0A    %0A
 %7D%0A

add some helper methods