From b160f154203cead282c2973d82009bf3c33704b9 Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Sun, 15 Apr 2018 09:17:07 +0800 Subject: [PATCH] [json-simple]change key type of JSONObject to String --- .../internal/org/json/simple/JSONObject.java | 14 ++-- .../internal/org/json/simple/JSONValue.java | 4 +- .../json/simple/parser/ContainerFactory.java | 2 +- .../org/json/simple/parser/JSONParser.java | 79 ++++++++++++------- 4 files changed, 62 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONObject.java b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONObject.java index 12b2df3..d483485 100644 --- a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONObject.java +++ b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONObject.java @@ -16,7 +16,7 @@ import java.util.Map; * * @author FangYidong */ -public class JSONObject extends HashMap implements JSONAware, JSONStreamAware { +public class JSONObject extends HashMap implements JSONAware, JSONStreamAware { private static final long serialVersionUID = -503443796854799292L; @@ -30,7 +30,7 @@ public class JSONObject extends HashMap implements JSONAware, JS * * @param map */ - public JSONObject(Map map) { + public JSONObject(Map map) { super(map); } @@ -43,14 +43,14 @@ public class JSONObject extends HashMap implements JSONAware, JS * @param map * @param out */ - public static void writeJSONString(Map map, Writer out) throws IOException { + public static void writeJSONString(Map map, Writer out) throws IOException { if (map == null) { out.write("null"); return; } boolean first = true; - Iterator> iter = map.entrySet().iterator(); + Iterator> iter = map.entrySet().iterator(); out.write('{'); while (iter.hasNext()) { @@ -58,9 +58,9 @@ public class JSONObject extends HashMap implements JSONAware, JS first = false; else out.write(','); - Map.Entry entry = iter.next(); + Map.Entry entry = iter.next(); out.write('\"'); - out.write(escape(String.valueOf(entry.getKey()))); + out.write(escape(entry.getKey())); out.write('\"'); out.write(':'); JSONValue.writeJSONString(entry.getValue(), out); @@ -82,7 +82,7 @@ public class JSONObject extends HashMap implements JSONAware, JS * @param map * @return JSON text, or "null" if map is null. */ - public static String toJSONString(Map map) { + public static String toJSONString(Map map) { final StringWriter writer = new StringWriter(); try { diff --git a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONValue.java b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONValue.java index 49aa8df..649f1ad 100644 --- a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONValue.java +++ b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/JSONValue.java @@ -166,7 +166,9 @@ public class JSONValue { } if (value instanceof Map) { - JSONObject.writeJSONString((Map) value, out); + @SuppressWarnings("unchecked") + Map objectValue = (Map) value; + JSONObject.writeJSONString(objectValue, out); return; } diff --git a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/ContainerFactory.java b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/ContainerFactory.java index daaf519..b6edb58 100644 --- a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/ContainerFactory.java +++ b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/ContainerFactory.java @@ -14,7 +14,7 @@ public interface ContainerFactory { /** * @return A Map instance to store JSON object, or null if you want to use org.json.simple.JSONObject. */ - Map createObjectContainer(); + Map createObjectContainer(); /** * @return A List instance to store JSON array, or null if you want to use org.json.simple.JSONArray. diff --git a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/JSONParser.java b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/JSONParser.java index dd1dfd3..f68937c 100644 --- a/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/JSONParser.java +++ b/src/main/java/org/to2mbn/authlibinjector/internal/org/json/simple/parser/JSONParser.java @@ -116,39 +116,44 @@ public class JSONParser { do { nextToken(); switch (status) { - case S_INIT: + case S_INIT: { switch (token.type) { - case Yytoken.TYPE_VALUE: + case Yytoken.TYPE_VALUE: { status = S_IN_FINISHED_VALUE; statusStack.addFirst(new Integer(status)); valueStack.addFirst(token.value); break; - case Yytoken.TYPE_LEFT_BRACE: + } + case Yytoken.TYPE_LEFT_BRACE: { status = S_IN_OBJECT; statusStack.addFirst(new Integer(status)); valueStack.addFirst(createObjectContainer(containerFactory)); break; - case Yytoken.TYPE_LEFT_SQUARE: + } + case Yytoken.TYPE_LEFT_SQUARE: { status = S_IN_ARRAY; statusStack.addFirst(new Integer(status)); valueStack.addFirst(createArrayContainer(containerFactory)); break; + } default: status = S_IN_ERROR; }// inner switch break; + } - case S_IN_FINISHED_VALUE: + case S_IN_FINISHED_VALUE: { if (token.type == Yytoken.TYPE_EOF) return valueStack.removeFirst(); else throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token); + } - case S_IN_OBJECT: + case S_IN_OBJECT: { switch (token.type) { case Yytoken.TYPE_COMMA: break; - case Yytoken.TYPE_VALUE: + case Yytoken.TYPE_VALUE: { if (token.value instanceof String) { String key = (String) token.value; valueStack.addFirst(key); @@ -158,7 +163,8 @@ public class JSONParser { status = S_IN_ERROR; } break; - case Yytoken.TYPE_RIGHT_BRACE: + } + case Yytoken.TYPE_RIGHT_BRACE: { if (valueStack.size() > 1) { statusStack.removeFirst(); valueStack.removeFirst(); @@ -167,57 +173,68 @@ public class JSONParser { status = S_IN_FINISHED_VALUE; } break; + } default: status = S_IN_ERROR; break; }// inner switch break; + } - case S_PASSED_PAIR_KEY: + case S_PASSED_PAIR_KEY: { switch (token.type) { case Yytoken.TYPE_COLON: break; - case Yytoken.TYPE_VALUE: + case Yytoken.TYPE_VALUE: { statusStack.removeFirst(); String key = (String) valueStack.removeFirst(); - Map parent = (Map) valueStack.getFirst(); + @SuppressWarnings("unchecked") + Map parent = (Map) valueStack.getFirst(); parent.put(key, token.value); status = peekStatus(statusStack); break; - case Yytoken.TYPE_LEFT_SQUARE: + } + case Yytoken.TYPE_LEFT_SQUARE: { statusStack.removeFirst(); - key = (String) valueStack.removeFirst(); - parent = (Map) valueStack.getFirst(); + String key = (String) valueStack.removeFirst(); + @SuppressWarnings("unchecked") + Map parent = (Map) valueStack.getFirst(); List newArray = createArrayContainer(containerFactory); parent.put(key, newArray); status = S_IN_ARRAY; statusStack.addFirst(new Integer(status)); valueStack.addFirst(newArray); break; - case Yytoken.TYPE_LEFT_BRACE: + } + case Yytoken.TYPE_LEFT_BRACE: { statusStack.removeFirst(); - key = (String) valueStack.removeFirst(); - parent = (Map) valueStack.getFirst(); - Map newObject = createObjectContainer(containerFactory); + String key = (String) valueStack.removeFirst(); + @SuppressWarnings("unchecked") + Map parent = (Map) valueStack.getFirst(); + Map newObject = createObjectContainer(containerFactory); parent.put(key, newObject); status = S_IN_OBJECT; statusStack.addFirst(new Integer(status)); valueStack.addFirst(newObject); break; + } default: status = S_IN_ERROR; } break; + } - case S_IN_ARRAY: + case S_IN_ARRAY: { switch (token.type) { case Yytoken.TYPE_COMMA: break; - case Yytoken.TYPE_VALUE: + case Yytoken.TYPE_VALUE: { + @SuppressWarnings("unchecked") List val = (List) valueStack.getFirst(); val.add(token.value); break; - case Yytoken.TYPE_RIGHT_SQUARE: + } + case Yytoken.TYPE_RIGHT_SQUARE: { if (valueStack.size() > 1) { statusStack.removeFirst(); valueStack.removeFirst(); @@ -226,26 +243,32 @@ public class JSONParser { status = S_IN_FINISHED_VALUE; } break; - case Yytoken.TYPE_LEFT_BRACE: - val = (List) valueStack.getFirst(); - Map newObject = createObjectContainer(containerFactory); + } + case Yytoken.TYPE_LEFT_BRACE: { + @SuppressWarnings("unchecked") + List val = (List) valueStack.getFirst(); + Map newObject = createObjectContainer(containerFactory); val.add(newObject); status = S_IN_OBJECT; statusStack.addFirst(new Integer(status)); valueStack.addFirst(newObject); break; - case Yytoken.TYPE_LEFT_SQUARE: - val = (List) valueStack.getFirst(); + } + case Yytoken.TYPE_LEFT_SQUARE: { + @SuppressWarnings("unchecked") + List val = (List) valueStack.getFirst(); List newArray = createArrayContainer(containerFactory); val.add(newArray); status = S_IN_ARRAY; statusStack.addFirst(new Integer(status)); valueStack.addFirst(newArray); break; + } default: status = S_IN_ERROR; }// inner switch break; + } case S_IN_ERROR: throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token); }// switch @@ -266,10 +289,10 @@ public class JSONParser { token = new Yytoken(Yytoken.TYPE_EOF, null); } - private Map createObjectContainer(ContainerFactory containerFactory) { + private Map createObjectContainer(ContainerFactory containerFactory) { if (containerFactory == null) return new JSONObject(); - Map m = containerFactory.createObjectContainer(); + Map m = containerFactory.createObjectContainer(); if (m == null) return new JSONObject();