Skip to content

Commit

Permalink
2.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
miktim committed Jun 17, 2024
1 parent 4c98549 commit 97f602a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
16 changes: 12 additions & 4 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ Overview:

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

String gen(String memberName, int... indices); // stringify value or array element
String toString(); // overridden, stringify JSON object
String toString(); // overridden, stringify JSON object
String toString(String memberName, int... indices);
// stringify member or array element


Class JSONAdapter;
Expand All @@ -84,8 +85,8 @@ Overview:

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.
- see JSON set/get/cast rules for Java object fields in the notes for JSON object and JSONAdapter;
- arrays and collections of objects must be managed using replacer/reviewer.

Constant:
static final Object IGNORED; // returns from replacer/reviver to skip the field
Expand All @@ -97,12 +98,19 @@ Overview:
// 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
public String toJSONText()
throws IllegalArgumentException, IllegalAccessException
// returns JSON text as one line

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
public Object fromJSONText(String jsonText)
throws IOException, ParseException, IllegalArgumentException, IllegalAccessException

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
Expand Down
Binary file renamed dist/json-2.0.2.jar → dist/json-2.0.3.jar
Binary file not shown.
10 changes: 5 additions & 5 deletions src/org/miktim/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ public JSON(Object... members) throws IndexOutOfBoundsException {
}
}

public String gen() {
return generate(this);
}
// public String generate() {
// return JSON.generate(this);
// }

public String gen(String memberName, int... indices) {
public String toString(String memberName, int... indices) {
return JSON.generate(get(memberName, indices));
}

@Override
public String toString() {
return gen();
return JSON.generate(this);
}

public List<String> listNames() {
Expand Down
19 changes: 15 additions & 4 deletions src/org/miktim/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
*/
package org.miktim.json;

import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.text.ParseException;
import java.util.Arrays;
import java.util.LinkedHashMap;

Expand All @@ -23,17 +25,26 @@ public Object toJSON()
throws IllegalArgumentException, IllegalAccessException {
return toJSON(this);
}


public String toJSONText() throws IllegalArgumentException, IllegalAccessException {
return JSON.generate(toJSON());
}

protected Object replacer(String fldName, Object value) {
return value;
}

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


public Object fromJSONText(String jsonText)
throws IOException, ParseException, IllegalArgumentException, IllegalAccessException {
return fromJSON(JSON.parse(jsonText));
}

@SuppressWarnings("unchecked")
protected <T> T castMember(String memberName, JSON jsonObj, T sample) {
if (jsonObj.exists(memberName)) {
Expand Down
17 changes: 13 additions & 4 deletions test/json/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import json.A;
import org.miktim.json.JSON;
import org.miktim.json.JSONAdapter;
Expand Down Expand Up @@ -122,15 +123,23 @@ public static void main(String[] args) throws Exception {
i = json.getNumber("array", 1, 3).intValue();
log(i);

log("\n\rTest generator with other Java objects (ArrayList with Array, Date and File entries):");
log("\n\rTest generator with other Java objects:");
log(" HashMap with int[3], Date and File entries:");
HashMap<String,Object> hashMap = new HashMap<>();
hashMap.put("int[]",new int[3]);
hashMap.put("Date",new Date());
hashMap.put("File", new File(path, "json.json"));
String s = JSON.generate(hashMap);
log(s);
log(" ArrayList with int[3], Date and File entries:");
ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(new int[2]);
arrayList.add(new int[3]);
arrayList.add(new Date());
arrayList.add(new File(path, "json.json"));
String s = JSON.generate(arrayList);
s = JSON.generate(arrayList);
log(s);
Object obj = JSON.parse(s);
log("is Array?: " + obj.getClass().isArray());
log("is Array?: " + (obj instanceof Object[]));//obj.getClass().isArray());

log("\n\rTest examples:");
// examples from RFC 8259 https://datatracker.ietf.org/doc/rfc8259/?include_text=1
Expand Down

0 comments on commit 97f602a

Please sign in to comment.