Skip to content

Commit bd17624

Browse files
committed
1.1.0
1 parent 43535a9 commit bd17624

File tree

5 files changed

+119
-58
lines changed

5 files changed

+119
-58
lines changed

README

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Java JSON parser/generator, MIT (c) 2020-2022 miktim@mail.ru
1+
# Java 7+/Android JSON parser/generator, MIT (c) 2020-2022 miktim@mail.ru
22

33
Release notes:
44
- Java SE 7+, Android compatible;
@@ -8,11 +8,18 @@ Release notes:
88
- JSON object members (name/value pairs) are stored in order of appearance/creation;
99
- when the names within an object are not unique, parser stores the last value only;
1010
- JSON object setter accepts any Java object, all Java primitives and primitive arrays;
11-
- avoid recursion!;
12-
- in addition to the parsed types, the generator converts Java Lists,Sets to JSON arrays
13-
and Java Maps to JSON objects. The null key is converted to a "null" member name.
11+
- AVOID RECURSION!;
12+
- in addition, the generator converts Java Lists,Sets to JSON arrays, Java Maps to JSON objects.
13+
The null key is converted to a "null" member name.
1414
Other Java objects are converted to JSON strings.
1515

16+
Put, set, get notes:
17+
- put, set methods casts Java primitives to the corresponding objects.
18+
Java objects and arrays are stored "as is" (as reference).
19+
For example: int -> Integer, int[] -> int[], String[] -> String[]
20+
- after JSON text parsing or normalization, they are stored as:
21+
BigDecimal, Object[] {BigDecimal,...}, Object[] {String,...}
22+
1623
package org.miktim
1724

1825
Overview:
@@ -26,11 +33,20 @@ Overview:
2633
static Object parse(Reader reader) throws IOException, ParseException;
2734
static String stringify(Object object); // generate JSON text
2835

29-
String toString(); // overridden, stringify JSON object
3036
List<String> list(); // returns list of member names
31-
boolean exists(String memberName);
32-
Object get(String memberName); // inherited
37+
boolean exists(String memberName); // alias of the inherited containsKey()
38+
Object put(String memberName, Object value); // inherited
3339
JSON set(String memberName, Object value); // create or replace member
34-
Object remove(String memberName); // inherited, returns the current value or null
40+
Object get(String memberName); // inherited
41+
Object get(String memberName, int... indices); // returns value or array element
42+
JSON getJSON(String memberName, int... indices) throws ClassCastException;
43+
String getString(String memberName, int... indices) throws ClassCastException;
44+
Number getNumber(String memberName, int... indices) throws ClassCastException;
45+
Boolean getBoolean(String memberName, int... indices) throws ClassCastException;
46+
Object[] getArray(String memberName, int... indices) throws ClassCastException;
47+
Object remove(String memberName); // inherited
48+
JSON normalize() throws Exception;
49+
String stringify(String memberName, int... indices); // stringify value or array element
50+
String toString(); // overridden, stringify JSON object
3551

3652
Usage: see ./test/json/JSONTest.java

dist/json-1.0.5.jar

-11.4 KB
Binary file not shown.

dist/json-1.1.0.jar

6.79 KB
Binary file not shown.

src/org/miktim/JSON.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* - when the names within an object are not unique, parser stores the last value only;
1111
* - JSON object setter accepts any Java object, all Java primitives and primitive arrays;
1212
* - avoid recursion!;
13-
* - in addition to the parsed types, the generator converts Java Lists, Sets to JSON arrays
13+
* - in addition, the generator converts Java Lists, Sets to JSON arrays
1414
* and Java Maps to JSON objects. The null key is converted to a "null" member name.
1515
* Other Java objects are converted to JSON strings.
1616
*
@@ -54,10 +54,14 @@ public JSON(Object... members) throws IndexOutOfBoundsException {
5454
this.put(String.valueOf(members[i++]), members[i++]);
5555
}
5656
}
57-
57+
5858
public String stringify() {
5959
return stringify(this);
6060
}
61+
62+
public String stringify(String memberName, int... indices) {
63+
return JSON.stringify(get(memberName, indices));
64+
}
6165

6266
@Override
6367
public String toString() {
@@ -77,6 +81,39 @@ public JSON set(String memberName, Object value) {
7781
return this;
7882
}
7983

84+
// get value or array element
85+
public Object get(String memberName, int... indices) {
86+
Object obj = get(memberName);
87+
for (int i = 0; i < indices.length; i++) {
88+
obj = Array.get(obj, indices[i]);
89+
}
90+
return obj;
91+
}
92+
93+
public JSON getJSON(String memberName, int... indices) {
94+
return (JSON) get(memberName, indices);
95+
}
96+
97+
public Number getNumber(String memberName, int... indices) {
98+
return (Number) get(memberName, indices);
99+
}
100+
101+
public String getString(String memberName, int... indices) {
102+
return (String) get(memberName, indices);
103+
}
104+
105+
public Boolean getBoolean(String memberName, int... indices) {
106+
return (Boolean) get(memberName, indices);
107+
}
108+
109+
public Object[] getArray(String memberName, int... indices) {
110+
return (Object[]) get(memberName, indices);
111+
}
112+
113+
public JSON normalize() throws Exception {
114+
return (JSON) JSON.parse(toString()); // :)
115+
}
116+
80117
static class Parser {
81118

82119
private static final char[] WHITESPACES = " \n\r\t".toCharArray();

test/json/JSONTest.java

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.FileReader;
66
import java.math.BigDecimal;
77
import java.util.ArrayList;
8+
import java.util.Arrays;
89
import java.util.Date;
910
import org.miktim.JSON;
1011

@@ -24,19 +25,28 @@ public static void main(String[] args) throws Exception {
2425
path = args[0];
2526
}
2627

27-
log("JSON package test");
28+
log("JSON class test");
2829

2930
log("\n\rTest constructor:");
30-
log(new JSON("One", 1, "Two", 2, 3, "Three"));
31-
31+
JSON json = new JSON("One", 1, "Two", 2, 3, "Three",
32+
"Nested", new JSON("Array",
33+
new float[][]{{1.1f, 2.2f}, {3.3f, 4.4f}}));
34+
log(json);
35+
log(json.getNumber("One").floatValue()); // Number(Integer)
36+
// java.lang.ClassCastException
37+
// log(json.getString("Two"));
38+
log(json.stringify("Two"));
39+
log(json.getString("3"));
40+
log(json.getJSON("Nested").getNumber("Array", 1, 1).intValue());
41+
3242
log("\n\rTest escape/unescape string:");
3343
String unescaped = new String(new char[]{0x8, 0x9, 0xA, 0xC, 0xD, 0x22, 0x2F, 0x5C, 0, '-', 0x1F, 0xd834, 0xdd1e});
3444
String escaped = JSON.escapeString(unescaped);
3545
log(escaped);
3646
logIsOK(unescaped.equals(JSON.unescapeString(escaped)));
3747

38-
log("\n\rTest create/stringify JSON object:");
39-
JSON json = (new JSON())
48+
log("\n\rTest JSON typecast.\r\nBefore normalization:");
49+
json = (new JSON())
4050
.set("Unescaped", unescaped)
4151
.set("EmptyJSON", new JSON())
4252
.set("intArray", new int[][]{{0, 1, 2}, {3, 4, 5, 6}})
@@ -50,69 +60,67 @@ public static void main(String[] args) throws Exception {
5060
.set("Int", 14159265)
5161
.set("Byte", (byte) 0xFF);
5262
log(json);
53-
log("List members: " + JSON.stringify(json.list()));
63+
64+
log("List members: " + json.list());
5465
for (String memberName : json.list()) {
5566
if (json.get(memberName) != null) {
5667
log("\"" + memberName + "\" is instance of: "
5768
+ json.get(memberName).getClass().getSimpleName());
5869
} else {
59-
log("\"" + memberName + "\" is " + json.get(memberName));
70+
log("\"" + memberName + "\" is null");
6071
}
6172
}
6273

63-
log("\n\rTest stringify/parse JSON:");
74+
log("\n\rNormalized:");
75+
json = json.normalize();
6476
log(json);
65-
log("List members: " + JSON.stringify(json.list()));
66-
json = (JSON) JSON.parse(json.toString());
67-
log(json);
68-
log("List members: " + JSON.stringify(json.list()));
77+
log("List members: " + json.list());
6978
for (String memberName : json.list()) {
7079
if (json.get(memberName) != null) {
7180
log("\"" + memberName + "\" is instance of: "
7281
+ json.get(memberName).getClass().getSimpleName());
7382
} else {
74-
log("\"" + memberName + "\" is " + json.get(memberName));
83+
log("\"" + memberName + "\" is null");
7584
}
7685
}
7786

78-
log("\n\rTest nullnamed/nonexistent member:");
79-
log("Remove null/nonexistent member returns: "
80-
+ json.remove(null) + "/" + json.remove("nonexistent"));
81-
log("Get null/nonexistent member returns: "
82-
+ json.get(null) + "/" + json.get("nonexistent"));
83-
log("Exists null/nonexistent member returns: "
84-
+ json.exists(null) + "/" + json.exists("nonexistent"));
85-
86-
log("\n\rTest JSON clone then remove \"BigDecimal\":");
87-
JSON cloned = (JSON) json.clone();
88-
cloned.remove("BigDecimal");
89-
log(json.list());
90-
log(cloned.list());
91-
9287
log("\n\rTest JSON with Java arrays:");
9388
int[][] intArray = new int[][]{{0, 1, 2}, {3, 4, 5, 6}};
94-
log(JSON.stringify(intArray));
95-
cloned.set("Array", intArray);
96-
log(JSON.stringify(cloned.get("Array")));
97-
intArray[0][1] = 6;
98-
log(JSON.stringify(cloned.get("Array")));
99-
Object[] array = intArray;
100-
array[1] = new int[]{7, 8, 9};
101-
log(JSON.stringify(array));
102-
log(JSON.stringify(cloned.get("Array")));
103-
104-
log("\n\rTest generator with other Java objects (ArrayList with Date, File entries):");
89+
json = new JSON("Array", intArray);
90+
log(json.get("Array").getClass().getSimpleName());
91+
// array is instance of int[][]
92+
// java.lang.ClassCastException
93+
// log(json.getArray("Array", 1).getClass().getSimpleName());
94+
log(json.getNumber("Array", 1, 1).floatValue());
95+
96+
log(json = json.normalize());
97+
log(json.get("Array").getClass().getSimpleName());
98+
// array is instance of Object[]
99+
log(json.getNumber("Array", 1, 1).floatValue());
100+
Object[] array;
101+
array = json.getArray("Array");
102+
log(array.length);
103+
array = json.getArray("Array",1);
104+
log(array.length);
105+
// cast array. doesn't make much sense
106+
Number[] na = Arrays.copyOf(array, array.length, Number[].class);
107+
log(JSON.stringify(na));
108+
109+
log("\n\rTest generator with other Java objects (ArrayList with Array, Date and File entries):");
105110
ArrayList<Object> arrayList = new ArrayList<>();
111+
arrayList.add(new int[2]);
106112
arrayList.add(new Date());
107113
arrayList.add(new File(path, "json.json"));
108-
log(JSON.stringify(arrayList));
114+
String s = JSON.stringify(arrayList);
115+
log(s);
116+
Object o = JSON.parse(s);
117+
log("is Array?: " + o.getClass().isArray());
109118

110119
// examples from RFC 8259 https://datatracker.ietf.org/doc/rfc8259/?include_text=1
111120
log("\n\rTest examples from RFC 8259:");
112121
log(JSON.parse("\"\\uD834\\uDD1E\"")); // G-clef
113122
log(JSON.parse("3.141592653589793238462643383279"));
114123
log(JSON.parse("null"));
115-
log(JSON.stringify(JSON.parse("\t[1, null, 3.2, {}, \"some text\" ] ")));
116124

117125
String example1 = "{\n"
118126
+ " \"Image\": {\n"
@@ -129,11 +137,11 @@ public static void main(String[] args) throws Exception {
129137
+ " \"IDs\": [116, 943, 234, 38793]\n"
130138
+ " }\n"
131139
+ " } ";
132-
Object object = JSON.parse(example1);
133-
log(((JSON) object).toString());
134-
log(((JSON) object).get("Image"));
135-
log(((JSON) (((JSON) object).get("Image"))).set("Thumbnail", 256));
136-
log(((JSON) (((JSON) object).get("Image"))).remove("Thumbnail"));
140+
json = (JSON) JSON.parse(example1);
141+
log(json);
142+
log(json.get("Image"));
143+
log(json.getJSON("Image").set("Thumbnail", 256)); // replace JSON object with number
144+
log(json.getJSON("Image").remove("Thumbnail")); // remove member
137145
String example2 = "[\n"
138146
+ " {\n"
139147
+ " \"precision\": \"zip\",\n"
@@ -156,9 +164,9 @@ public static void main(String[] args) throws Exception {
156164
+ " \"Country\": \"US\"\n"
157165
+ " }\n"
158166
+ " ]";
159-
object = JSON.parse(example2);
160-
log(JSON.stringify(object));
161-
log(JSON.stringify(((Object[]) object)[1]));
167+
array = (Object[]) JSON.parse(example2);
168+
log(JSON.stringify(array));
169+
log(JSON.stringify(array[1]));
162170

163171
// Example from https://docs.oracle.com/en/database/oracle/oracle-database/12.2/adjsn/json-data.html#GUID-FBC22D72-AA64-4B0A-92A2-837B32902E2C
164172
log("\n\rTest one more example:");

0 commit comments

Comments
 (0)