[json-simple]change key type of JSONObject to String

This commit is contained in:
yushijinhun 2018-04-15 09:17:07 +08:00
parent b31a31c644
commit b160f15420
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
4 changed files with 62 additions and 37 deletions

View file

@ -16,7 +16,7 @@ import java.util.Map;
* *
* @author FangYidong<fangyidong@yahoo.com.cn> * @author FangYidong<fangyidong@yahoo.com.cn>
*/ */
public class JSONObject extends HashMap<Object, Object> implements JSONAware, JSONStreamAware { public class JSONObject extends HashMap<String, Object> implements JSONAware, JSONStreamAware {
private static final long serialVersionUID = -503443796854799292L; private static final long serialVersionUID = -503443796854799292L;
@ -30,7 +30,7 @@ public class JSONObject extends HashMap<Object, Object> implements JSONAware, JS
* *
* @param map * @param map
*/ */
public JSONObject(Map<?, ?> map) { public JSONObject(Map<String, ?> map) {
super(map); super(map);
} }
@ -43,14 +43,14 @@ public class JSONObject extends HashMap<Object, Object> implements JSONAware, JS
* @param map * @param map
* @param out * @param out
*/ */
public static void writeJSONString(Map<?, ?> map, Writer out) throws IOException { public static void writeJSONString(Map<String, ?> map, Writer out) throws IOException {
if (map == null) { if (map == null) {
out.write("null"); out.write("null");
return; return;
} }
boolean first = true; boolean first = true;
Iterator<? extends Map.Entry<?, ?>> iter = map.entrySet().iterator(); Iterator<? extends Map.Entry<String, ?>> iter = map.entrySet().iterator();
out.write('{'); out.write('{');
while (iter.hasNext()) { while (iter.hasNext()) {
@ -58,9 +58,9 @@ public class JSONObject extends HashMap<Object, Object> implements JSONAware, JS
first = false; first = false;
else else
out.write(','); out.write(',');
Map.Entry<?, ?> entry = iter.next(); Map.Entry<String, ?> entry = iter.next();
out.write('\"'); out.write('\"');
out.write(escape(String.valueOf(entry.getKey()))); out.write(escape(entry.getKey()));
out.write('\"'); out.write('\"');
out.write(':'); out.write(':');
JSONValue.writeJSONString(entry.getValue(), out); JSONValue.writeJSONString(entry.getValue(), out);
@ -82,7 +82,7 @@ public class JSONObject extends HashMap<Object, Object> implements JSONAware, JS
* @param map * @param map
* @return JSON text, or "null" if map is null. * @return JSON text, or "null" if map is null.
*/ */
public static String toJSONString(Map<?, ?> map) { public static String toJSONString(Map<String, ?> map) {
final StringWriter writer = new StringWriter(); final StringWriter writer = new StringWriter();
try { try {

View file

@ -166,7 +166,9 @@ public class JSONValue {
} }
if (value instanceof Map) { if (value instanceof Map) {
JSONObject.writeJSONString((Map<?, ?>) value, out); @SuppressWarnings("unchecked")
Map<String, ?> objectValue = (Map<String, ?>) value;
JSONObject.writeJSONString(objectValue, out);
return; return;
} }

View file

@ -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. * @return A Map instance to store JSON object, or null if you want to use org.json.simple.JSONObject.
*/ */
Map<Object, Object> createObjectContainer(); Map<String, Object> createObjectContainer();
/** /**
* @return A List instance to store JSON array, or null if you want to use org.json.simple.JSONArray. * @return A List instance to store JSON array, or null if you want to use org.json.simple.JSONArray.

View file

@ -116,39 +116,44 @@ public class JSONParser {
do { do {
nextToken(); nextToken();
switch (status) { switch (status) {
case S_INIT: case S_INIT: {
switch (token.type) { switch (token.type) {
case Yytoken.TYPE_VALUE: case Yytoken.TYPE_VALUE: {
status = S_IN_FINISHED_VALUE; status = S_IN_FINISHED_VALUE;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(new Integer(status));
valueStack.addFirst(token.value); valueStack.addFirst(token.value);
break; break;
case Yytoken.TYPE_LEFT_BRACE: }
case Yytoken.TYPE_LEFT_BRACE: {
status = S_IN_OBJECT; status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(new Integer(status));
valueStack.addFirst(createObjectContainer(containerFactory)); valueStack.addFirst(createObjectContainer(containerFactory));
break; break;
case Yytoken.TYPE_LEFT_SQUARE: }
case Yytoken.TYPE_LEFT_SQUARE: {
status = S_IN_ARRAY; status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(new Integer(status));
valueStack.addFirst(createArrayContainer(containerFactory)); valueStack.addFirst(createArrayContainer(containerFactory));
break; break;
}
default: default:
status = S_IN_ERROR; status = S_IN_ERROR;
}// inner switch }// inner switch
break; break;
}
case S_IN_FINISHED_VALUE: case S_IN_FINISHED_VALUE: {
if (token.type == Yytoken.TYPE_EOF) if (token.type == Yytoken.TYPE_EOF)
return valueStack.removeFirst(); return valueStack.removeFirst();
else else
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token); throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}
case S_IN_OBJECT: case S_IN_OBJECT: {
switch (token.type) { switch (token.type) {
case Yytoken.TYPE_COMMA: case Yytoken.TYPE_COMMA:
break; break;
case Yytoken.TYPE_VALUE: case Yytoken.TYPE_VALUE: {
if (token.value instanceof String) { if (token.value instanceof String) {
String key = (String) token.value; String key = (String) token.value;
valueStack.addFirst(key); valueStack.addFirst(key);
@ -158,7 +163,8 @@ public class JSONParser {
status = S_IN_ERROR; status = S_IN_ERROR;
} }
break; break;
case Yytoken.TYPE_RIGHT_BRACE: }
case Yytoken.TYPE_RIGHT_BRACE: {
if (valueStack.size() > 1) { if (valueStack.size() > 1) {
statusStack.removeFirst(); statusStack.removeFirst();
valueStack.removeFirst(); valueStack.removeFirst();
@ -167,57 +173,68 @@ public class JSONParser {
status = S_IN_FINISHED_VALUE; status = S_IN_FINISHED_VALUE;
} }
break; break;
}
default: default:
status = S_IN_ERROR; status = S_IN_ERROR;
break; break;
}// inner switch }// inner switch
break; break;
}
case S_PASSED_PAIR_KEY: case S_PASSED_PAIR_KEY: {
switch (token.type) { switch (token.type) {
case Yytoken.TYPE_COLON: case Yytoken.TYPE_COLON:
break; break;
case Yytoken.TYPE_VALUE: case Yytoken.TYPE_VALUE: {
statusStack.removeFirst(); statusStack.removeFirst();
String key = (String) valueStack.removeFirst(); String key = (String) valueStack.removeFirst();
Map<Object, Object> parent = (Map<Object, Object>) valueStack.getFirst(); @SuppressWarnings("unchecked")
Map<String, Object> parent = (Map<String, Object>) valueStack.getFirst();
parent.put(key, token.value); parent.put(key, token.value);
status = peekStatus(statusStack); status = peekStatus(statusStack);
break; break;
case Yytoken.TYPE_LEFT_SQUARE: }
case Yytoken.TYPE_LEFT_SQUARE: {
statusStack.removeFirst(); statusStack.removeFirst();
key = (String) valueStack.removeFirst(); String key = (String) valueStack.removeFirst();
parent = (Map<Object, Object>) valueStack.getFirst(); @SuppressWarnings("unchecked")
Map<String, Object> parent = (Map<String, Object>) valueStack.getFirst();
List<Object> newArray = createArrayContainer(containerFactory); List<Object> newArray = createArrayContainer(containerFactory);
parent.put(key, newArray); parent.put(key, newArray);
status = S_IN_ARRAY; status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(new Integer(status));
valueStack.addFirst(newArray); valueStack.addFirst(newArray);
break; break;
case Yytoken.TYPE_LEFT_BRACE: }
case Yytoken.TYPE_LEFT_BRACE: {
statusStack.removeFirst(); statusStack.removeFirst();
key = (String) valueStack.removeFirst(); String key = (String) valueStack.removeFirst();
parent = (Map<Object, Object>) valueStack.getFirst(); @SuppressWarnings("unchecked")
Map<Object, Object> newObject = createObjectContainer(containerFactory); Map<String, Object> parent = (Map<String, Object>) valueStack.getFirst();
Map<String, Object> newObject = createObjectContainer(containerFactory);
parent.put(key, newObject); parent.put(key, newObject);
status = S_IN_OBJECT; status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(new Integer(status));
valueStack.addFirst(newObject); valueStack.addFirst(newObject);
break; break;
}
default: default:
status = S_IN_ERROR; status = S_IN_ERROR;
} }
break; break;
}
case S_IN_ARRAY: case S_IN_ARRAY: {
switch (token.type) { switch (token.type) {
case Yytoken.TYPE_COMMA: case Yytoken.TYPE_COMMA:
break; break;
case Yytoken.TYPE_VALUE: case Yytoken.TYPE_VALUE: {
@SuppressWarnings("unchecked")
List<Object> val = (List<Object>) valueStack.getFirst(); List<Object> val = (List<Object>) valueStack.getFirst();
val.add(token.value); val.add(token.value);
break; break;
case Yytoken.TYPE_RIGHT_SQUARE: }
case Yytoken.TYPE_RIGHT_SQUARE: {
if (valueStack.size() > 1) { if (valueStack.size() > 1) {
statusStack.removeFirst(); statusStack.removeFirst();
valueStack.removeFirst(); valueStack.removeFirst();
@ -226,26 +243,32 @@ public class JSONParser {
status = S_IN_FINISHED_VALUE; status = S_IN_FINISHED_VALUE;
} }
break; break;
case Yytoken.TYPE_LEFT_BRACE: }
val = (List<Object>) valueStack.getFirst(); case Yytoken.TYPE_LEFT_BRACE: {
Map<Object, Object> newObject = createObjectContainer(containerFactory); @SuppressWarnings("unchecked")
List<Object> val = (List<Object>) valueStack.getFirst();
Map<String, Object> newObject = createObjectContainer(containerFactory);
val.add(newObject); val.add(newObject);
status = S_IN_OBJECT; status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(new Integer(status));
valueStack.addFirst(newObject); valueStack.addFirst(newObject);
break; break;
case Yytoken.TYPE_LEFT_SQUARE: }
val = (List<Object>) valueStack.getFirst(); case Yytoken.TYPE_LEFT_SQUARE: {
@SuppressWarnings("unchecked")
List<Object> val = (List<Object>) valueStack.getFirst();
List<Object> newArray = createArrayContainer(containerFactory); List<Object> newArray = createArrayContainer(containerFactory);
val.add(newArray); val.add(newArray);
status = S_IN_ARRAY; status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(new Integer(status));
valueStack.addFirst(newArray); valueStack.addFirst(newArray);
break; break;
}
default: default:
status = S_IN_ERROR; status = S_IN_ERROR;
}// inner switch }// inner switch
break; break;
}
case S_IN_ERROR: case S_IN_ERROR:
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token); throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
}// switch }// switch
@ -266,10 +289,10 @@ public class JSONParser {
token = new Yytoken(Yytoken.TYPE_EOF, null); token = new Yytoken(Yytoken.TYPE_EOF, null);
} }
private Map<Object, Object> createObjectContainer(ContainerFactory containerFactory) { private Map<String, Object> createObjectContainer(ContainerFactory containerFactory) {
if (containerFactory == null) if (containerFactory == null)
return new JSONObject(); return new JSONObject();
Map<Object, Object> m = containerFactory.createObjectContainer(); Map<String, Object> m = containerFactory.createObjectContainer();
if (m == null) if (m == null)
return new JSONObject(); return new JSONObject();