Skip to content

Commit

Permalink
3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
miktim committed Oct 13, 2024
1 parent 173daaa commit b42daf1
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 27 deletions.
17 changes: 11 additions & 6 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>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> 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;
Expand All @@ -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> T getTarget();
// get target object. Accessible from replacer/reviver
Expand Down
Binary file added dist/json-3.0.0-alpha.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/org/miktim/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void registerJsonObject(JsonObject obj, String className) {
*/
public static <T> 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;
}
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/org/miktim/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit b42daf1

Please sign in to comment.