Class WriteJSON


  • public class WriteJSON
    extends java.lang.Object
    Generates JSON Requests from lists of Parameter-Names, Parameter-Types and Parameter-Values.

    This class is not meant to be an exhaustive way to serialize Java Objects into JSON Properties, but it is the main serializer for the Chrome Remote Interface package in this Java HTML JAR Library.


    • Method Summary

       
      Convert an Object to a JSON String
      Modifier and Type Method
      static void get​(JsonGenerator jGen, Vector<Class<?>> cVec, Vector<String> nVec, Vector<Object> oVec, boolean[] optArr)
       
      Convert an Object to JSON, Prepared for Browser RDP API
      Modifier and Type Method
      static String get​(Vector<Class<?>> paramTypes, Vector<String> paramNames, boolean[] optionals, int webSocketID, String methodName, Object... paramValues)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • get

        🡇    
        public static java.lang.String get​
                    (java.util.Vector<java.lang.Class<?>> paramTypes,
                     java.util.Vector<java.lang.String> paramNames,
                     boolean[] optionals,
                     int webSocketID,
                     java.lang.String methodName,
                     java.lang.Object... paramValues)
        
        Converts a list of class types, names & values into a Json Request, as a String
        Parameters:
        paramTypes - These are the types being passed to the JSON Request.
        paramNames - These are the names of the parameters, and these are used for the names of the JSON parameters passed.
        optionals - This is a boolean array that indicates which parameter-values are allowed to be null.
        methodName - The name of the command being passed over the Web-Socket Connection.
        Returns:
        Returns the JSON Web-Socket Request as a java.lang.String
        Code:
        Exact Method Body:
         StringWriter   strW        = new StringWriter();
         JsonGenerator  jGen        = Json.createGenerator(strW);
        
         jGen.writeStartObject()
             .write("id", webSocketID)
             .write("method", methodName);
        
         if (paramTypes.size() > 0) 
         {
             Vector<Object> oVec = new Vector<>();
             for (Object o : paramValues) oVec.add(o);
        
             jGen.writeStartObject("params");
             get(jGen, paramTypes, paramNames, oVec, optionals);
             jGen.writeEnd();
         }
         // else jGen.writeNull("params"); // Is this the right idea?
        
         jGen.writeEnd();
         jGen.close();
        
         return strW.toString();
        
      • get

        🡅    
        public static void get​(JsonGenerator jGen,
                               java.util.Vector<java.lang.Class<?>> cVec,
                               java.util.Vector<java.lang.String> nVec,
                               java.util.Vector<java.lang.Object> oVec,
                               boolean[] optArr)
        Converts a list of class types, names & values into a series of Json-Generator commands.
        Parameters:
        jGen - Because this method may be used recursively, the current generator that is being used must be passed to this method.
        cVec - The list of types for each entity being converted to a Json Request.
        nVec - The list of names for each entity being converted to a Json Request.
        oVec - The list of values for each entity being converted to a Json Request.
        optArr - A boolean[] array indicating which parameters may be null.
        Code:
        Exact Method Body:
         for (int k=0; k < nVec.size(); k++)
         {
             boolean     opt = optArr[k];
             Class<?>    c   = cVec.elementAt(k);
             String      n   = nVec.elementAt(k);
             Object      o   = oVec.elementAt(k);
        
             if (o == null)
             {
                 if (! opt) throw new Error(
                     "The Operating Assertion is that if a Field is null in this class, it must " +
                     "have been declared 'optional.'  Field [" + n + "] is null, but not optional."
                 );
        
                 else continue; // Just leave off of the JSON Completely!
             }
        
             switch (c.getSimpleName())
             {
                 case "int"      : 
                 case "Integer"  :   jGen.write(n, ((Integer) o).intValue());        break;
                 case "boolean"  :
                 case "Boolean"  :   jGen.write(n, ((Boolean) o).booleanValue());    break;
                 case "String"   :   jGen.write(n, (String) o);                      break;
        
                 case "Number" :
                     if (o instanceof Long)  jGen.write(n, ((Long) o).longValue());
                     else                    jGen.write(n, ((Double) o).doubleValue());
                     break;
        
                 case "int[]" :
                     jGen.writeStartArray(n);
                     for (int i : (int[]) o) jGen.write(i);
                     jGen.writeEnd();
                     break;
        
                 case "boolean[]" :
                     jGen.writeStartArray(n);
                     for (boolean b : (boolean[]) o) jGen.write(b);
                     jGen.writeEnd();
                     break;
        
                 case "String[]" :
                     jGen.writeStartArray(n);
                     for (String s : (String[]) o)
                         if (s == null)  jGen.writeNull();
                         else            jGen.write(s);
                     jGen.writeEnd();
                     break;
        
                 case "Number[]" :
                     jGen.writeStartArray(n);
                     for (Number num : (Number[]) o)
                         if (num == null)                jGen.writeNull();
                         else if (num instanceof Long)   jGen.write(n, ((Long) num).longValue());
                         else                            jGen.write(n, ((Double) num).doubleValue());
                     jGen.writeEnd();
                     break;
        
                 case "int[][]" :
                     jGen.writeStartArray(n);
                     for (int[] iArr : (int[][]) o)
                     {
                         jGen.writeStartArray();
                         for (boolean b : (boolean[]) o) jGen.write(b);
                         jGen.writeEnd();
                     }
                     jGen.writeEnd();
                     break;
        
                 case "boolean[][]" :
                     jGen.writeStartArray(n);
                     for (int[] iArr : (int[][]) o)
                     {
                         jGen.writeStartArray();
                         for (boolean b : (boolean[]) o) jGen.write(b);
                         jGen.writeEnd();
                     }
                     jGen.writeEnd();
                     break;
        
                 case "String[][]" :
                     jGen.writeStartArray(n);
                     for (String[] sArr : (String[][]) o)
                     {
                         jGen.writeStartArray();
                         for (String s : (String[]) o)
                             if (s == null)  jGen.writeNull();
                             else            jGen.write(s);
                         jGen.writeEnd();
                     }
                     jGen.writeEnd();
                     break;
        
        
                 case "Number[][]" :
                     jGen.writeStartArray(n);
                     NUM_ARR:
                     for (Number[] sArr : (Number[][]) o)
                     {
                         if (sArr == null)
                         { jGen.writeNull(); continue NUM_ARR; }
        
                         jGen.writeStartArray();
                         for (Number num : (Number[]) o)
                             if (num == null)                jGen.writeNull();
                             else if (num instanceof Long)   jGen.write(n, ((Long) num).longValue());
                             else                            jGen.write(n, ((Double) num).doubleValue());
                         jGen.writeEnd();
                     }
                     jGen.writeEnd();
                     break;
        
                 default:
                     if (c.isArray())
                     {
                         if (! BaseType.class.isAssignableFrom(c.getComponentType()))
                             throw new Error(
                                 "One of the Fields [" + n + "] is declared an array, " +
                                 "(type " + c.getSimpleName() + "), but the Array Elements do " +
                                 "not implement the interface 'BaseType'"
                             );
                                
                         if (StringParse.countCharacters(c.getSimpleName(), '[') > 1)
                             throw new Error(
                                 "One of the Fields [" + n + "] is declared an array, " +
                                 "(type " + c.getSimpleName() + "), but the Array has a dimension " +
                                 "greater than 1.  This was agreed, impossible."
                             );
        
                         jGen.writeStartArray(n);
                         for (BaseType bt : (BaseType[]) o)
                             if (bt == null) jGen.writeNull();
                             else            bt.toJSON(null, jGen);
                         jGen.writeEnd();
                     }
                     else
                     {
                         if (! BaseType.class.isAssignableFrom(c)) throw new Error(
                             "Field [" + n +"] has a class / type [" + c.getSimpleName() + "], " +
                             "but this class does not implement the interface BaseType!"
                         );
        
                         else ((BaseType) o).toJSON(n, jGen);
                     }
             }
         }