json and java

satya - 8/11/2020, 10:14:50 AM

I did some research before for Android

I did some research before for Android

satya - 8/11/2020, 10:15:04 AM

gson vs org.json

gson vs org.json

Search for: gson vs org.json

satya - 8/11/2020, 1:51:44 PM

For docs links see the link above

For docs links see the link above

satya - 8/11/2020, 6:37:45 PM

The link above also has a few quick samples

The link above also has a few quick samples

satya - 8/11/2020, 6:38:25 PM

An early thought


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

satya - 8/11/2020, 6:39:01 PM

gson as they indicate at the site is threadsafe as it doesn't hold state

I believe and proceed from there

satya - 8/11/2020, 9:25:51 PM

Updated JSONUtils


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;
   }
}

satya - 8/11/2020, 9:28:24 PM

Exercising JSON: Integrating JSON with Aspire configuration an early example


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");
   }
}

satya - 8/11/2020, 9:28:45 PM

I will have a suitable github link when ready!

I will have a suitable github link when ready!

satya - 8/11/2020, 9:29:24 PM

A quick unit test


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);
   }
}

satya - 8/11/2020, 9:36:25 PM

This will allow me configuration like this


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}       ]    }