Package Torello.Java.JSON
Class WriteJSON
- java.lang.Object
-
- Torello.Java.JSON.WriteJSON
-
public class WriteJSON extends java.lang.Object
Serializing Java to JSON:
Converting Java-Data (both Java-Primitive's & Reference-Object's) intoJava-Script Object Notation
has one major & extremely-simplifying factor: the Error-Checking stuff is virtually eliminated from your code!
Although reading JSON into Java means worrying about data that may have come from as far away as Timbuktu, converting Java-Data into JSON is literally just reading memory from your own computer's internal heap, and changing that into Stringified JSON Data. Though this certainly is not guaranteed to be an error-free process, it does have the distinct advantage of your knowing there is really no way to tell if you have made an error, or caused an exception, without something or someone from very far away literally calling you on the telephone to tell you that your recent API change has made their code throw exceptions!
As such, the code for this class is extremely simple, and offers virtually no exception hierarchy or error-checking at all. It is tremendously shorter than the other JSON-Binding & Read-JSON classes.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.
Hi-Lited Source-Code:- View Here: Torello/Java/JSON/WriteJSON.java
- Open New Browser-Tab: Torello/Java/JSON/WriteJSON.java
File Size: 9,261 Bytes Line Count: 231 '\n' Characters Found
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
- 2 Method(s), 2 declared static
- 0 Field(s)
-
-
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)
-
-
-
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 aString
- 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
- Aboolean[]
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); } } }
-
-