44 * Release notes:
55 * - Java 7+, Android compatible;
66 * - in accordance with RFC 8259: https://datatracker.ietf.org/doc/rfc8259/?include_text=1
7- * - supported Java objects:
8- * JSON object, String, Number, Boolean, null, Object[] array of listed types;
9- * - parser applies BigDecimal for numbers ;
10- * - JSON properties are stored in creation/appearance order.
7+ * - parser converts JSON text to Java objects:
8+ * JSON object, String, Number (BigDecimal) , Boolean, null, Object[] array of listed types;
9+ * - JSON object members (name/value pairs) are stored in creation/appearance order ;
10+ * - when the names within an object are not unique, parser stores the last value only;
1111 *
1212 * Created: 2020-03-07
1313 */
@@ -41,7 +41,7 @@ public static String stringify(Object object) throws IllegalArgumentException {
4141 return stringifyObject (object );
4242 }
4343
44- private LinkedHashMap <String , Object > properties = new LinkedHashMap <>();
44+ private LinkedHashMap <String , Object > members = new LinkedHashMap <>();
4545
4646 public JSON () {
4747
@@ -58,43 +58,38 @@ public String toString() {
5858
5959 @ SuppressWarnings ("unchecked" )
6060 @ Override
61- public JSON clone () throws CloneNotSupportedException {
62- JSON clone = (JSON ) super .clone ();
63- clone .properties = (LinkedHashMap <String , Object >) this .properties .clone ();
61+ public JSON clone () {//throws CloneNotSupportedException {
62+ // JSON clone = (JSON) super.clone();
63+ JSON clone = new JSON ();
64+ clone .members = (LinkedHashMap <String , Object >) this .members .clone ();
6465 return clone ;
6566 }
6667
6768 public List <String > list () {
68- return new ArrayList <>(this .properties .keySet ());
69+ return new ArrayList <>(this .members .keySet ());
6970 }
7071
71- private String checkPropName (String propName ) {
72- if (propName == null ) {
73- throw new NullPointerException ("Illegal property name" );
74- }
75- return propName ;
76- }
77-
78- public boolean exists (String propName ) {
79- return listProperties ().containsKey (propName );
72+ public boolean exists (String memberName ) {
73+ return getMembers ().containsKey (memberName );
8074 }
8175
82- public Object get (String propName ) {
83- return listProperties ().get (propName );
76+ public Object get (String memberName ) {
77+ return getMembers ().get (memberName );
8478 }
8579
86- public JSON set (String propName , Object value )
87- throws NullPointerException , IllegalArgumentException {
88- listProperties ().put (checkPropName (propName ), checkObjectType (value ));
80+ public JSON set (String memberName , Object value ) {
81+ // throws NullPointerException, IllegalArgumentException {
82+ // getMembers().put(checkPropName(memberName), checkObjectType(value));
83+ getMembers ().put (memberName , value );
8984 return this ;
9085 }
9186
92- public Object remove (String propName ) {
93- return this .listProperties ().remove (propName );
87+ public Object remove (String memberName ) {
88+ return this .getMembers ().remove (memberName );
9489 }
9590
96- private LinkedHashMap <String , Object > listProperties () {
97- return this .properties ;
91+ private LinkedHashMap <String , Object > getMembers () {
92+ return this .members ;
9893 }
9994
10095 static class Parser {
@@ -171,11 +166,11 @@ private Object parseObject() throws IOException, ParseException {
171166 object = new JSON ();
172167 if (!expectedChar ('}' )) { // empty object
173168 do {
174- Object propName = parseObject ();
175- if ((propName instanceof String ) && expectedChar (':' )) {
176- ((JSON ) object ).set ((String ) propName , parseObject ());
169+ Object memberName = parseObject ();
170+ if ((memberName instanceof String ) && expectedChar (':' )) {
171+ ((JSON ) object ).set ((String ) memberName , parseObject ());
177172 } else {
178- throw newParseException ("Property name expected" ,
173+ throw newParseException ("Name expected" ,
179174 null , offset );
180175 }
181176 } while (expectedChar (',' ));
@@ -224,7 +219,7 @@ private Object parseObject() throws IOException, ParseException {
224219 } else if (charIn (NUMBERS , lastChar ())) {
225220 String number = nextChars (NUMBERS );
226221 try {
227- object = ( Number ) new BigDecimal (number );
222+ object = new BigDecimal (number );
228223 } catch (NumberFormatException e ) {
229224 throw newParseException ("Unparseable number:" ,
230225 number , offset );
@@ -261,7 +256,7 @@ static String stringifyObject(Object value) {
261256// } else if (value instanceof Character) {
262257// return stringifyObject(value.toString());
263258 } else if (value instanceof JSON ) {
264- return stringifyObject (((JSON ) value ).properties );
259+ return stringifyObject (((JSON ) value ).members );
265260 } else if (value .getClass ().isArray ()) {
266261 StringBuilder sb = new StringBuilder ("[" );
267262 String separator = "" ;
@@ -277,7 +272,7 @@ static String stringifyObject(Object value) {
277272 String separator = "" ;
278273 for (Map .Entry <Object , Object > entry : ((Map <Object , Object >) value ).entrySet ()) {
279274 sb .append (separator )
280- // null key (property name) is converted to "null"
275+ // null key (member name) is converted to "null"
281276 .append (stringifyObject (String .valueOf (entry .getKey ())))
282277 .append (": " )
283278 .append (stringifyObject (entry .getValue ()));
@@ -290,24 +285,6 @@ static String stringifyObject(Object value) {
290285// + value.getClass().getSimpleName());
291286 }
292287
293- static Object checkObjectType (Object obj ) throws IllegalArgumentException {
294- if (obj == null
295- || (obj instanceof String )
296- || (obj instanceof Number )
297- || (obj instanceof Boolean )
298- || (obj instanceof JSON )
299- || (obj instanceof Character )) {
300- } else if (obj .getClass ().isArray ()) {
301- for (int i = 0 ; i < Array .getLength (obj ); i ++) {
302- checkObjectType (Array .get (obj , i ));
303- }
304- } else {
305- throw new IllegalArgumentException ("Unsupported object: "
306- + obj .getClass ().getSimpleName ());
307- }
308- return obj ;
309- }
310-
311288 private static final char [] ESCAPED_CHARS = {'"' , '/' , '\\' , 'b' , 'f' , 'n' , 'r' , 't' };
312289 private static final char [] CHARS_UNESCAPED = {0x22 , 0x2F , 0x5C , 0x8 , 0xC , 0xA , 0xD , 0x9 };
313290
0 commit comments