json and java

I did some research before for Android

gson vs org.json

Search for: gson vs org.json

For docs links see the link above

The link above also has a few quick samples


public class JSONUtils 
{
   private static Gson gson = null;
   static {
      gson = new Gson();
   }
   
   public static String convertToJson(Object obj)
   {
      return gson.toJson(obj);
   }
}

I believe and proceed from there


package com.ai.common;

import com.google.gson.Gson;

public class JSONUtils 
{
   private static Gson gson = null;
   static {
      gson = new Gson();
   }
   
   public static String convertToJson(Object obj)
   {
      return gson.toJson(obj);
   }
   
   public static<T> Object convertFromJson(String json, Class<T> classObj)
   {
      Object o = gson.fromJson(json, classObj);
      return o;
   }
}

package com.ai.aspire.config;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.ai.application.interfaces.ConfigException;
import com.ai.application.utils.AppObjects;
import com.ai.common.JSONUtils;


/*
    {
       "name":"TestObject",
       "fieldList":
       [
          {"name":"field1","type":"string","emptyFlag":true},
          {"name":"field2","type":"string","emptyFlag":true},
          {"name":"field3","type":"string","emptyFlag":true}
       ]
    } 
*/

public class ObjectDefinition 
{
   String name;
   
   String qualifiedJavaClassname=null;
   
   private List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
   
   static class FieldInfo {
      String name;
      String type;
      String regex;
      Boolean emptyFlag;
      
      public FieldInfo(String name, String type, String regex, Boolean emptyFlag)
      {
         this.name = name;
         this.type = type;
         this.regex = regex;
         this.emptyFlag = emptyFlag;
      }
   }
   
   public ObjectDefinition(String name)
   {
      this.name = name;
   }
   
   public Iterator<FieldInfo> getFields()
   {
      return fieldList.iterator();
   }
   
   public ObjectDefinition addField(FieldInfo f)
   {
      fieldList.add(f);
      return this;
   }
   public Object getObject()
   {
      return null;
   }
   
   public static ObjectDefinition createASample()
   {
      ObjectDefinition od = new ObjectDefinition("TestObject");
      
      FieldInfo fi = new FieldInfo("field1","string",null,true);
      od.addField(fi);
      
      fi = new FieldInfo("field2","string",null,true);
      od.addField(fi);
      
      fi = new FieldInfo("field3","string",null,true);
      od.addField(fi);
      return od;
   }
   
   public String convertToJSON()
   {
      return JSONUtils.convertToJson(this);
   }
   
   static public ObjectDefinition convertFromJSON(String jsonString)
   {
      Class<ObjectDefinition> o = ObjectDefinition.class;
      Object reto = JSONUtils.convertFromJson(jsonString,o);
      return (ObjectDefinition)reto;
   }

   /*
    ******************************************************************
    * Testing
    ******************************************************************
    */
   static public void test()
   throws Exception
   {
      ObjectDefinition.testConvertToJson();
      ObjectDefinition.testConvertFromJson();
      
   }

   static public void testConvertToJson()
   {
      ObjectDefinition od = ObjectDefinition.createASample();
      String json = od.convertToJSON();
      System.out.println(json);
      
   }
   
   static public void testConvertFromJson()
   throws ConfigException
   {
      String s = AppObjects.getValue("recorddefs.record1.json");
      
      System.out.println(s);
      ObjectDefinition o = ObjectDefinition.convertFromJSON(s);
      s = o.convertToJSON(); 
      System.out.println(s);
      System.out.println("Test complete");
   }
}

I will have a suitable github link when ready!


package com.ai.aspire.config;

import org.junit.Test;
import com.ai.application.utils.AppObjects;

public class ModuleTest 
{
   @Test
   public void mainTest()
   throws Exception
   {
      //setup
      AppObjects.initApplicationFromEnvPropertiesFile();
      
      //actual tests
      ObjectDefinition.test();
   }
   private void test1()
   {
      p("Hello");
   }
   
   private void p(String s)
   {
      System.out.println(s);
   }
}

request.test.classname=com.ai.parts.integration.TestCSVReaderPart
request.test.csvFileToRead=aspire:\\csv\\WindPlant1_2020-03-26_12-50-00.csv


recorddefs.record1.json=    {        "name":"TestObject",       "fieldList":       [          {"name":"field1","type":"string","emptyFlag":true},          {"name":"field2","type":"string","emptyFlag":true},          {"name":"field3","type":"string","emptyFlag":true}       ]    }