Skip to content

Commit 43535a9

Browse files
committed
1.0.5 code cleared
1 parent 51e75b6 commit 43535a9

File tree

5 files changed

+24
-47
lines changed

5 files changed

+24
-47
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020-2021 miktim@mail.ru
3+
Copyright (c) 2020-2022 miktim@mail.ru
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README

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

33
Release notes:
44
- Java SE 7+, Android compatible;
@@ -19,7 +19,7 @@ Overview:
1919

2020
Class JSON extends LinkedHashMap<String, Object>.
2121
Constructor:
22-
JSON();
22+
JSON(Object... members) throws IndexOutOfBoundsException; // name,value pairs
2323

2424
Methods:
2525
static Object parse(String json) throws IOException, ParseException;
@@ -30,7 +30,7 @@ Overview:
3030
List<String> list(); // returns list of member names
3131
boolean exists(String memberName);
3232
Object get(String memberName); // inherited
33-
JSON set(String memberName, Object value);
34-
Object remove(String memberName); // inherited, returns the previous value
33+
JSON set(String memberName, Object value); // create or replace member
34+
Object remove(String memberName); // inherited, returns the current value or null
3535

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

dist/json-1.0.5.jar

-222 Bytes
Binary file not shown.

src/org/miktim/JSON.java

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Java JSON parser/generator, MIT (c) 2020-2021 miktim@mail.ru
2+
* Java JSON parser/generator, MIT (c) 2020-2022 miktim@mail.ru
33
*
44
* Release notes:
55
* - Java 7+, Android compatible;
@@ -47,12 +47,14 @@ public static String stringify(Object object) throws IllegalArgumentException {
4747
return stringifyObject(object);
4848
}
4949

50-
// private LinkedHashMap<String, Object> members = new LinkedHashMap<>();
51-
52-
public JSON() {
53-
50+
// Memebers: name,value pairs
51+
public JSON(Object... members) throws IndexOutOfBoundsException {
52+
super();
53+
for (int i = 0; i < members.length;) {
54+
this.put(String.valueOf(members[i++]), members[i++]);
55+
}
5456
}
55-
57+
5658
public String stringify() {
5759
return stringify(this);
5860
}
@@ -61,40 +63,17 @@ public String stringify() {
6163
public String toString() {
6264
return stringify();
6365
}
64-
/*
65-
@SuppressWarnings("unchecked")
66-
@Override
67-
public JSON clone() {//throws CloneNotSupportedException {
68-
// JSON clone = (JSON) super.clone();
69-
JSON clone = new JSON();
70-
clone.members = (LinkedHashMap<String, Object>) this.members.clone();
71-
return clone;
72-
}
73-
*/
66+
7467
public List<String> list() {
7568
return new ArrayList<>(this.keySet());
7669
}
7770

7871
public boolean exists(String memberName) {
79-
return getMembers().containsKey(memberName);
80-
}
81-
82-
public Object get(String memberName) {
83-
return getMembers().get(memberName);
72+
return this.containsKey(memberName);
8473
}
8574

8675
public JSON set(String memberName, Object value) {
87-
// throws NullPointerException, IllegalArgumentException {
88-
// getMembers().put(checkPropName(memberName), checkObjectType(value));
89-
getMembers().put(memberName, value);
90-
return this;
91-
}
92-
93-
public Object remove(String memberName) {
94-
return this.getMembers().remove(memberName);
95-
}
96-
97-
private LinkedHashMap<String, Object> getMembers() {
76+
this.put(memberName, value);
9877
return this;
9978
}
10079

@@ -250,7 +229,7 @@ Object parse(Reader reader) throws IOException, ParseException {
250229
}
251230
}
252231

253-
// Java Lists converts to JSON arrays [V[0],...,V[n]], Maps - to object {"K":V,...}
232+
// Java Lists, Sets converts to JSON arrays [V[0],...,V[n]], Maps - to object {"K":V,...}
254233
@SuppressWarnings("unchecked")
255234
static String stringifyObject(Object value) {
256235
if (value == null) {
@@ -259,10 +238,6 @@ static String stringifyObject(Object value) {
259238
return "\"" + escapeString((String) value) + "\"";
260239
} else if (value instanceof Number || value instanceof Boolean) {
261240
return value.toString(); // Number, Boolean
262-
// } else if (value instanceof Character) {
263-
// return stringifyObject(value.toString());
264-
// } else if (value instanceof JSON) {
265-
// return stringifyObject((JSON) value);
266241
} else if (value.getClass().isArray()) {
267242
StringBuilder sb = new StringBuilder("[");
268243
String separator = "";
@@ -289,8 +264,6 @@ static String stringifyObject(Object value) {
289264
return stringifyObject(((Set) value).toArray());
290265
}
291266
return stringifyObject(String.valueOf(value));
292-
// throw new IllegalArgumentException("Unsupported object: "
293-
// + value.getClass().getSimpleName());
294267
}
295268

296269
private static final char[] ESCAPED_CHARS = {'"', '/', '\\', 'b', 'f', 'n', 'r', 't'};

test/json/JSONTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* JSONTest, MIT (c) 2020 miktim@mail.ru
2+
* JSONTest, MIT (c) 2020-2022 miktim@mail.ru
33
*/
44
import java.io.File;
55
import java.io.FileReader;
@@ -25,6 +25,10 @@ public static void main(String[] args) throws Exception {
2525
}
2626

2727
log("JSON package test");
28+
29+
log("\n\rTest constructor:");
30+
log(new JSON("One", 1, "Two", 2, 3, "Three"));
31+
2832
log("\n\rTest escape/unescape string:");
2933
String unescaped = new String(new char[]{0x8, 0x9, 0xA, 0xC, 0xD, 0x22, 0x2F, 0x5C, 0, '-', 0x1F, 0xd834, 0xdd1e});
3034
String escaped = JSON.escapeString(unescaped);
@@ -80,13 +84,13 @@ public static void main(String[] args) throws Exception {
8084
+ json.exists(null) + "/" + json.exists("nonexistent"));
8185

8286
log("\n\rTest JSON clone then remove \"BigDecimal\":");
83-
JSON cloned = (JSON)json.clone();
87+
JSON cloned = (JSON) json.clone();
8488
cloned.remove("BigDecimal");
8589
log(json.list());
8690
log(cloned.list());
8791

8892
log("\n\rTest JSON with Java arrays:");
89-
int[][] intArray = new int[][]{{0, 1, 2}, {3, 4, 5}};
93+
int[][] intArray = new int[][]{{0, 1, 2}, {3, 4, 5, 6}};
9094
log(JSON.stringify(intArray));
9195
cloned.set("Array", intArray);
9296
log(JSON.stringify(cloned.get("Array")));

0 commit comments

Comments
 (0)