Skip to content

Commit

Permalink
2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
miktim committed Jun 15, 2024
1 parent e302145 commit 4c98549
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
28 changes: 17 additions & 11 deletions README
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Java 7+/Android JSON parser/generator, MIT (c) 2020-2022 miktim@mail.ru

Release notes:
- Java SE 7+/Android compatible;
- in accordance with RFC 8259: https://datatracker.ietf.org/doc/rfc8259/?include_text=1 ;
- Java SE 7+/Android RFC 8259 compliant package
(see: https://datatracker.ietf.org/doc/rfc8259/?include_text=1 );
- no external dependencies;
- JavaScript JSON - like interface.

package org.miktim.json
Expand All @@ -23,13 +24,14 @@ Overview:
- JSON text is generated as one line.

Put, set, get notes:
- RFC 8259 does not recommend using Java BigDecimal and BigInteger as JSON member values;
- put, set methods cast Java primitives to the corresponding objects.
Java objects and arrays are stored "as is" (as reference).
For example: int -> Integer, int[] -> int[], String[] -> String[]
- after JSON text parsing or normalization, they are stored as:
BigDecimal, Object[] {BigDecimal,...}, Object[] {String,...};
- getters eliminate these differences by casting types to JSON-declared.

Constructors:
JSON(Object... members) throws IndexOutOfBoundsException; // name,value pairs
JSON(String jsonText) throws IOException, ParseException;
Expand All @@ -55,8 +57,8 @@ Overview:
Boolean getBoolean(String memberName, int... indices) throws ClassCastException;
Object[] getArray(String memberName, int... indices) throws ClassCastException;

<T> T cast(String memberName, T sample, int... indices); // cast value or array by sample
<T> T cast(String memberName, Class <T> cls, int... indices); // cast value or array by Class
<T> T cast(String memberName, T sample, int... indices); // casting value or array by sample
<T> T cast(String memberName, Class <T> cls, int... indices); // casting value or array by Class

JSON normalize() throws IOException, ParseException; // not required to generate JSON text

Expand All @@ -66,7 +68,7 @@ Overview:

Class JSONAdapter;

Casting by sample or Class of a JSON variable or array to a Java primitive or array.
Casting a JSON variable or array to a Java primitive or array using a sample or class.
- sample must be initialized;
- casting null to a Java primitive returns corresponding initial value;
- casting null to an array returns an empty array;
Expand All @@ -80,7 +82,7 @@ Overview:

Abstract class JSONObject;

Unloads/loads the accessible fields of the Java object to/from the JSON object.
Unloads/loads the accessible fields (properties) of the Java object to/from the JSON object.
- Java final, interface, abstract, transient, strict fields are ignored;
- see JSON set/get/cast rules for Java object fields in the notes for JSON object
and JSONAdapter.
Expand All @@ -91,13 +93,17 @@ Overview:
Methods:
Object toJSON()
throws IllegalArgumentException, IllegalAccessException;
// returns JSON object
// convertible object parameters must be initialized
protected Object replacer(String name, Object value);
// first call with object class name and empty JSON object as value
JSONObject fromJSON(Object obj)
Object fromJSON(Object jsonObj)
throws IllegalArgumentException, IllegalAccessException;
// returns this object
// convertible object parameters must be initialized
protected Object reviver(String name, Object value);
// first call with object class name and obj argument as value
static boolean isClassName(String name);
static final boolean isClassName(String name);
protected <T> T castMember(String memberName, JSON jsonObj, T sample);
// returns the sample if JSON member does not exists
protected void setIgnored(String[] fldNames); // set ignored field names
Expand All @@ -108,5 +114,5 @@ Usage see:
./test/json/JSONAdapterTest.java
./test/json/JSONObjectTest.java

RFC compatibility parsing test result:
https://miktim.github.io/JSONTestSuite/results/parsing.html
The result of the JSON parsing test for compliance with RFC 8259:
https://miktim.github.io/JSONTestSuite/results/parsing.html
Binary file renamed dist/json-2.0.1.jar → dist/json-2.0.2.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion src/org/miktim/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ protected Object replacer(String fldName, Object value) {
return value;
}

public void fromJSON(Object obj)
public Object fromJSON(Object obj)
throws IllegalArgumentException, IllegalAccessException {
fromJSON(this, obj);
return this;
}

@SuppressWarnings("unchecked")
Expand Down
38 changes: 37 additions & 1 deletion test/json/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public class JSONObjectTest extends JSONObject {

C[] mC = {new C(), new C()};
public static String mS = "mS value";
static final String mSf = "Final fields ignored";
private double md = 3.14159;
Expand Down Expand Up @@ -40,13 +41,35 @@ protected Object replacer(String name, Object value) {
return IGNORED; // another way to ignore the field
} else if (name.equals("mS")) {
return "mS value replaced";
} else if (name.equals("mC")) {
Object[] mmC = new Object[mC.length];
for (int i = 0; i < mC.length; i++) {
try {
mmC[i] = mC[i].toJSON();
} catch (IllegalAccessException | IllegalArgumentException ex) {
log(ex);
}
}
value = mmC;
}
return value;
}

@Override
protected Object reviver(String name, Object value) {
logName(this, name);
// logName(this, name);
if (name.equals("mC")) {
Object[] jmC = (Object[]) value;
this.mC = new C[jmC.length];
for (int i = 0; i < jmC.length; i++) {
try {
(this.mC[i] = new C()).fromJSON(jmC[i]);
} catch (IllegalAccessException | IllegalArgumentException ex) {
log(ex);
}
}
return IGNORED;
}
return value;
}

Expand All @@ -60,6 +83,7 @@ protected Object replacer(String name, Object value) {
((JSON) value).set("jc", getJc());
log("Unloaded J.jc by getter: " + getJc());
}
super.replacer(name, value);
return value;
}

Expand All @@ -75,6 +99,18 @@ protected Object reviver(String name, Object value) {
}
}

class C extends JSONObject {

public String mS = "mS value";
static final String mSf = "Final fields ignored";
private double md = 3.14159;
private int mi = 123;
private String mSp = "mSp value";

C() {
}
}

public static void main(String[] args)
throws IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException, Exception {
log("\n\rJSONObject test\n\r");
Expand Down

0 comments on commit 4c98549

Please sign in to comment.