Class BaseType


  • public abstract class BaseType
    extends java.lang.Object
    This class may be used to help serialize and de-serialize Java Objects into Json Objects.

    Currently, the primary use of this class is to serialize and de-serialize any JsonObject which is transmitted between the Browser's Web-Socket Remote Debugging Protocol, and this Java Jar Library. The classes in that package contain fields, all of which are declared both public and final.


    • Constructor Summary

      Constructors 
      Modifier Description
      protected
      Do Nothing Constructor
    • Method Summary

       
      Convert to java.lang.String
      Modifier and Type Method
      String toString()
       
      Serialize 'this' to JSON String
      Modifier and Type Method
      void toJSON​(String name, JsonGenerator jGen)
       
      Identify which JSON Properties may be absent or null
      Modifier and Type Method
      abstract boolean[] optionals()
      • Methods inherited from class java.lang.Object

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

      • optionals

        🡅  🡇    
        public abstract boolean[] optionals()
        Implementing this method allows sub-classes to specify which JSON Properties may be absent or null. When binding a JsonObject to a Java-Object, if some of the expected fields for the Java-Object map to Properties which might be left-out or omitted, then that may be indicated by setting that fields array position TRUE.

        NOTE: This array should have a length equal to the number of fields contained by the Java Object. The first boolean in the array should specify whether the first Object Field may by absent. The second boolean should specify whether the second Object Field is optional in the JSON - and so on and so forth...
        Returns:
        A boolean[] array whose length is precisely equal to the number of fields in the Java Object.
      • toString

        🡅  🡇    
        public java.lang.String toString()
        This method uses Java Reflection to convert the inheriting object into a String.
        Overrides:
        toString in class java.lang.Object
        Returns:
        A java.lang.String representation of 'this' object.
        Code:
        Exact Method Body:
         return toString(0);
        
      • toJSON

        🡅    
        public void toJSON​(java.lang.String name,
                           JsonGenerator jGen)
        Serializes 'this' object into JSON.

        1. It is OK if 'name' is null.
        2. It is NOT-OK if 'jGen' is null.
        Parameters:
        name - The name being assigned to this JsonObject Property. This name may be null. If null is passed here, the JsonGenerator will treat this Object as the top level JSON Object, not a property of a larger object.
        jGen - This is the generator instance. Note that often, when serializing Java Objects to JSON (and vice-versa!), one object may be a property or field of another object. When serializing an object to JSON, if there is a field that also needs to be serialized as a JsonObject, just pass the name and this generator instance in order to include that field as a sub-property.
        Code:
        Exact Method Body:
         boolean[]           optArr  = optionals();
         Field[]             fArr    = this.getClass().getFields();
         Vector<Class<?>>    cVec    = new Vector<>();
         Vector<String>      nVec    = new Vector<>();
         Vector<Object>      oVec    = new Vector<>();
        
         if (fArr.length == 0) throw new JsonException(
             "Likely, this is an error, but not necessarily.  You are attempting to serialize a " +
             "class that has 0 public fields, into JSON"
         );
        
         // Retrieve all of the Fields Available to this class.
         // These Fields should all be public and final
         try
         {
             for (Field f : fArr) if (! Modifier.isStatic(f.getModifiers()))
             {
                 cVec.add(f.getType());
                 nVec.add(f.getName());
                 oVec.add(f.get(this));
             }
         }
         catch (IllegalAccessException e)
         {
             System.out.println("Unable to Access the Field.");
             System.err.println(EXCC.toString(e));
             throw new UnreachableError();
         }
        
         // Write the value of these fields to the Json-Generator.  If no 'name' was provided,
         // then write the "Top-Level Json-Object"
        
         if (name == null)   jGen.writeStartObject();
         else                jGen.writeStartObject(name);
        
         WriteJSON.get(jGen, cVec, nVec, oVec, optArr);
        
         // Close the writing of 'this' object.
         jGen.writeEnd();