Class WriteJSON


  • public class WriteJSON
    extends java.lang.Object
    ⚠️ This class is designated as an "internal use only class." While there is nothing detrimental about using this class, it is actually only declared 'public' because it's methods & fields are indeed needed in packages outside of Torello.Browser. Classes which are declared 'public' are automatically documented by 'javadoc'. Therefore this page is included for viewing, even though Java-HTML Library users probably don't actually need to know about it!

    💡 Feel free to review it's contents. Perhaps there are explanations which might be of interest.
    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.

    Generating Json can be easy with the already provided Glass-Fish Library.



    Stateless Class:
    This class neither contains any program-state, nor can it be instantiated. The @StaticFunctional Annotation may also be called 'The Spaghetti Report'. Static-Functional classes are, essentially, C-Styled Files, without any constructors or non-static member fields. It is a concept very similar to the Java-Bean's @Stateless Annotation.

    • 1 Constructor(s), 1 declared private, zero-argument constructor
    • 4 Method(s), 4 declared static
    • 0 Field(s)


    • Method Summary

       
      Convert an Object to a JSON String
      Modifier and Type Method
      static void get​(JsonGenerator jGen, byte paramType, String paramName, Object paramValue, boolean optional)
       
      Convert an Object to JSON, Prepared for Browser RDP API
      Modifier and Type Method
      static String get​(byte paramType, String paramName, boolean optional, String methodName, Object paramValue)
      static String get​(CommandDescriptor<?> cd, String methodName, Object... parameterValues)
      • 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​(byte paramType,
                                           java.lang.String paramName,
                                           boolean optional,
                                           java.lang.String methodName,
                                           java.lang.Object paramValue)
        This is only used when a CDP command accepts just a single parameter. This eliminates the need for the Code Generator's need to wrap the parameter's name, type & values as arrays.

        This is very handy for shortening the code needed for all of the CDP commands that accept just one parameter. No more, no less!
        Parameters:
        paramType - The "type" of this method's "lone parameter", encoded as a Java byte primitive. You may view all available types in the class CDPTypes.
        paramName - The name of the method-parameter, as defined in the CDP specs. This is critical for ensuring that the browser understands what value is being passed to it, when invoking a browser method.
        optional - Whether the method's lone parameter is optional or not. If BOTH 'false' is passed here, AND 'null' is passed to 'paramValue', then an exception shall throw.
        methodName - The name of the CDP method being invoked
        paramValue - The value to place into the JSON property for this browser method's lone parameter.
        Returns:
        The JSON Web-Socket Request, encoded as a java.lang.String
        Throws:
        UnrecognizedCTABError - If the the value passed to 'paramType' isn't valid.
        See Also:
        CDPTypes
        Code:
        Exact Method Body:
         StringWriter   strW = new StringWriter();
         JsonGenerator  jGen = Json.createGenerator(strW);
        
         jGen.writeStartObject()
             .write("method", methodName);
        
         jGen.writeStartObject("params");
         get(jGen, paramType, paramName, paramValue, optional);
         jGen.writeEnd();
        
         jGen.writeEnd();
         jGen.close();
        
         return strW.toString();
        
      • get

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.String get​(CommandDescriptor<?> cd,
                                           java.lang.String methodName,
                                           java.lang.Object... parameterValues)
        Converts a list of class types, names & values into a CDP Web-Socket Request. This method generates a JSON-String.

        This method is strictly intended to be used with CDP functions / commands which accept at least two parameters.
        Parameters:
        cd - All pertinent information about the method being invoked
        methodName - The name of the command being passed over the Web-Socket Connection.
        parameterValues - the values the user has passed to this Web-Socket Request
        Returns:
        Returns the JSON Web-Socket Request as a java.lang.String
        Code:
        Exact Method Body:
         if (parameterValues.length != cd.size) throw new UnreachableError();
        
         StringWriter   strW = new StringWriter();
         JsonGenerator  jGen = Json.createGenerator(strW);
        
         jGen.writeStartObject()
             .write("method", methodName)
             .writeStartObject("params");
        
         for (int i=0; i < cd.size; i++)
        
             get(
                 jGen,
                 cd.types.get(i),
                 cd.names.get(i),
                 parameterValues[i],
                 cd.optionals.get(i)
             );
        
         jGen.writeEnd()
             .writeEnd()
             .close();
        
         return strW.toString();
        
      • get

        🡅     🗕  🗗  🗖
        public static void get​(JsonGenerator jGen,
                               byte paramType,
                               java.lang.String paramName,
                               java.lang.Object paramValue,
                               boolean optional)
        Abject human torture
        Parameters:
        jGen - Because this method may be used recursively, the current generator that is being used must be passed to this method.
        paramType - parameter type
        paramName - parameter name
        paramValue - parameter value
        optional - indicates if 'null' is allowed as a value
        Throws:
        UnrecognizedCTABError - If the the value passed to 'paramType' isn't valid.
        Code:
        Exact Method Body:
         // Makes the typing in the switch-statement below easier
         final String n = paramName;
        
         if (paramValue == null)
         {
             if (! optional) throw new WriteJSONError(
                 "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 return; // Just leave off of the JSON Completely!
         }
        
         switch (paramType)
         {
             // This may "seem" counter-intuitive, but 'int' is Auto-Boxed
             // to an "Integer" when it is passed to parameter "paramValue"
        
             case PRIMITIVE_INT :
             case BOXED_INTEGER :
                 jGen.write(n, CHECK(paramValue, Integer.class).intValue());
                 break;
        
             // SAME AS ABOVE, IBID
             case PRIMITIVE_BOOLEAN :
             case BOXED_BOOLEAN :
                 jGen.write(n, CHECK(paramValue, Boolean.class).booleanValue());
                 break;
        
             case STRING :
                 jGen.write(n, CHECK(paramValue, String.class));
                 break;
        
             case NUMBER :
                 NumberCrap.handleNumber(jGen, n, (Number) paramValue);
                 break;
        
             case INT_ARRAY_1D :
                 jGen.writeStartArray(n);
                 for (final int i : CHECK(paramValue, int[].class)) jGen.write(i);
                 jGen.writeEnd();
                 break;
        
             case BOOLEAN_ARRAY_1D :
                 jGen.writeStartArray(n);
                 for (final boolean b : CHECK(paramValue, boolean[].class)) jGen.write(b);
                 jGen.writeEnd();
                 break;
        
             case STRING_ARRAY_1D :
                 jGen.writeStartArray(n);
                 for (final String s : CHECK(paramValue, String[].class))
                     if (s == null)  jGen.writeNull();
                     else            jGen.write(s);
                 jGen.writeEnd();
                 break;
        
             case NUMBER_ARRAY_1D :
                 NumberCrap.handleNumberArr(jGen, n, paramValue);
                 break;
        
             case INT_ARRAY_2D :
                 jGen.writeStartArray(n);
                 for (final int[] iArr : CHECK(paramValue, int[][].class))
                 {
                     jGen.writeStartArray();
                     for (final int i : iArr) jGen.write(i);
                     jGen.writeEnd();
                 }
                 jGen.writeEnd();
                 break;
        
             case BOOLEAN_ARRAY_2D :
                 jGen.writeStartArray(n);
                 for (final boolean[] bArr : CHECK(paramValue, boolean[][].class))
                 {
                     jGen.writeStartArray();
                     for (final boolean b : bArr) jGen.write(b);
                     jGen.writeEnd();
                 }
                 jGen.writeEnd();
                 break;
        
             case STRING_ARRAY_2D :
                 jGen.writeStartArray(n);
                 for (final String[] sArr : CHECK(paramValue, String[][].class))
                 {
                     jGen.writeStartArray();
                     for (final String s : sArr)
                         if (s == null)  jGen.writeNull();
                         else            jGen.write(s);
                     jGen.writeEnd();
                 }
                 jGen.writeEnd();
                 break;
        
             case NUMBER_ARRAY_2D :
                 NumberCrap.handleNumberArr2D(jGen, n, paramValue);
                 break;
        
             case CDP_TYPE : 
                 CHECK(paramValue, BaseType.class).toJSON(n, jGen);
                 break;
        
             case CDP_TYPE_ARRAY_1D :
                 jGen.writeStartArray(n);
                 for (final BaseType<?> bt : CHECK(paramValue, BaseType[].class))
                     if (bt == null) jGen.writeNull();
                     else            bt.toJSON(null, jGen);
                 jGen.writeEnd();
                 break;
        
             default : throw new UnrecognizedCTABError(paramType);
         }