diff --git a/README b/README index 48c1833..9d2924e 100644 --- a/README +++ b/README @@ -118,13 +118,15 @@ Overview: Abstract class JsonObject; + Java objects extender. Unload/load fields of a Java object to/from a Json object. - visibility of object fields as from the object constructor, except for private fields - Java transient fields are ignored; - Java final fields are unloaded, but not initialized at load; - the accessible fields of the object MUST be initialized; - see Json set/get/cast rules for Java object fields in the notes for Json object; - - arrays of custom objects and collections MUST be managed using replacer/reviewer. + - arrays of custom objects and collections MUST be managed using replacer/reviewer; + - it is highly recommended to create a default constructor Constants: protected static final transient Object IGNORED; @@ -133,19 +135,19 @@ Overview: Methods: Json toJson() throws IllegalArgumentException, IllegalAccessException; - // returns Json object from this object + // returns a Json object from this object T fromJson(Json jsonObj) throws IllegalArgumentException, IllegalAccessException; - // load Json to this object + // loads Json to this object Json toJson(Object targetObj) throws IllegalArgumentException, IllegalAccessException; - // returns Json object from target object + // returns a Json object from the target object T fromJson(T targetObj, Json json) throws IllegalArgumentException, IllegalAccessException; - // load Json to target object + // loads Json to target object protected Object replacer(String name, Object value); - applies on unloading; @@ -160,7 +162,10 @@ Overview: - returns a value that is compatible with the object field or IGNORED protected void onError(String name, Exception e); - default action: print error message to System.err + // default action: print exception message to System.err + + String toString(); + // overriden. Generate text in JSON format as a single line protected T getTarget(); // get target object. Accessible from replacer/reviver diff --git a/dist/json-3.0.0-alpha.jar b/dist/json-3.0.0-alpha.jar new file mode 100644 index 0000000..2c8f727 Binary files /dev/null and b/dist/json-3.0.0-alpha.jar differ diff --git a/src/org/miktim/json/JSON.java b/src/org/miktim/json/JSON.java index 2c22860..2755c49 100644 --- a/src/org/miktim/json/JSON.java +++ b/src/org/miktim/json/JSON.java @@ -50,7 +50,7 @@ public static void registerJsonObject(JsonObject obj, String className) { */ public static T toJSON(T obj, OutputStream out, int space, String charsetName) throws IOException { - GeneratorJSON generator = new GeneratorJSON(out, space, charsetName); + JSONGenerator generator = new JSONGenerator(out, space, charsetName); generator.generateObject(obj, 0); return obj; } @@ -72,7 +72,7 @@ public static String toJSON(Object obj) throws IOException { public static Object fromJSON(InputStream in, String charsetName) throws IOException, ParseException { - ParserJSON parser = new ParserJSON(in, charsetName); + JSONParser parser = new JSONParser(in, charsetName); return parser.parseObject(); } diff --git a/src/org/miktim/json/GeneratorJSON.java b/src/org/miktim/json/JSONGenerator.java similarity index 90% rename from src/org/miktim/json/GeneratorJSON.java rename to src/org/miktim/json/JSONGenerator.java index ef57917..0666b52 100644 --- a/src/org/miktim/json/GeneratorJSON.java +++ b/src/org/miktim/json/JSONGenerator.java @@ -1,13 +1,12 @@ /** * JSON generator, MIT (c) 2020-2024 miktim@mail.ru * - * Release notes: - * - generator converts to JSON Java objects: - * Json object, String, Number, Boolean, null, Object[] array of listed types; + * Generator converts to JSON Java objects: + * - Json object, String, Number, Boolean, null, Object[] array of listed types; * - Java primitives and arrays of primitives; - * - in addition, the generator converts Java Collections to JSON arrays - * and Java Maps to Json objects. The null key is converted to a "null" member name. - * Other Java objects are converted to string representation. + * - Java Collections to JSON arrays and Java Maps to Json objects. + * The null key is converted to a "null" member name; + * - Other Java objects are converted to string representation. */ package org.miktim.json; @@ -20,14 +19,14 @@ import java.util.Collection; import java.util.Map; -class GeneratorJSON { +class JSONGenerator { int intend = 0; String charsetName = "UTF-8"; OutputStream stream; byte[] newLine = "\n".getBytes(); - GeneratorJSON(OutputStream outStream, int space, String charsetName) { + JSONGenerator(OutputStream outStream, int space, String charsetName) { intend = space; this.charsetName = charsetName; stream = outStream; diff --git a/src/org/miktim/json/ParserJSON.java b/src/org/miktim/json/JSONParser.java similarity index 94% rename from src/org/miktim/json/ParserJSON.java rename to src/org/miktim/json/JSONParser.java index 16ef2ac..f5a61a8 100644 --- a/src/org/miktim/json/ParserJSON.java +++ b/src/org/miktim/json/JSONParser.java @@ -1,10 +1,8 @@ /** - * JSON parser, MIT (c) 2020 -2024 miktim@mail.ru + * JSON parser, MIT (c) 2020-2024 miktim@mail.ru * - * Release notes: - * - parser converts JSON text to Java objects: - * Json object, String, Number (Double or Long), Boolean, null, Object[] array of listed types; - * - JSON object members (name/value pairs) are stored in creation/appearance order; + * - Parser converts JSON text to Java objects: + * Json object, String, Number, Boolean, null, Object[] array of listed types; * - when the names within an object are not unique, parser stores the last value only; */ package org.miktim.json; @@ -20,12 +18,12 @@ import static java.util.Arrays.binarySearch; import java.util.List; -class ParserJSON { +class JSONParser { String charsetName; InputStream stream; - ParserJSON(InputStream inStream, String charsetName) throws UnsupportedEncodingException { + JSONParser(InputStream inStream, String charsetName) throws UnsupportedEncodingException { this.charsetName = charsetName; stream = inStream; reader = new InputStreamReader(inStream, charsetName); diff --git a/src/org/miktim/json/Json.java b/src/org/miktim/json/Json.java index fb34da9..bdf81a3 100644 --- a/src/org/miktim/json/Json.java +++ b/src/org/miktim/json/Json.java @@ -6,7 +6,7 @@ * - Java 7+, Android compatible; * - Json members: * Json object, String, Number (Double or Long), Boolean, null, Object[] array of listed types; - * - Json object setter accept all Java primitives and primitive arrays; + * - Json setters accept any Java objects, all Java primitives and primitive arrays; * - avoid recursion!. * * Created: 2020-03-07 @@ -61,8 +61,8 @@ public Json toJSON(OutputStream outStream) throws IOException { public String toString() { try { return JSON.toJSON(this); - } catch (IOException ex) { - ex.printStackTrace(); + } catch (IOException ie) { + ie.printStackTrace(); return null; } } diff --git a/test/json/JsonTest.java b/test/json/JsonTest.java index 301fe3e..11c8b1d 100644 --- a/test/json/JsonTest.java +++ b/test/json/JsonTest.java @@ -98,7 +98,7 @@ public static void main(String[] args) throws Exception { log(json.toJSON("Two")); log(json.getString("3")); log(Arrays.toString(json.getJson("Nested Json").getArray("array", 1))); - log(json.getJson("Nested Json").getNumber("array", 1, 1).intValue()); + log(json.getJson("Nested Json").getNumber("array", 1, 1)); log("\n\rTest JSON typecast."); json = (new Json())