[json-simple]add generics

This commit is contained in:
yushijinhun 2018-04-15 09:06:20 +08:00
parent 92c7ee06be
commit b31a31c644
No known key found for this signature in database
GPG key ID: 5BC167F73EA558E4
5 changed files with 46 additions and 46 deletions

View file

@ -16,7 +16,7 @@ import java.util.Iterator;
* *
* @author FangYidong<fangyidong@yahoo.com.cn> * @author FangYidong<fangyidong@yahoo.com.cn>
*/ */
public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware { public class JSONArray extends ArrayList<Object> implements JSONAware, JSONStreamAware {
private static final long serialVersionUID = 3957988303675231981L; private static final long serialVersionUID = 3957988303675231981L;
/** /**
@ -33,27 +33,27 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
* @param c * @param c
* the collection whose elements are to be placed into this JSONArray * the collection whose elements are to be placed into this JSONArray
*/ */
public JSONArray(Collection c) { public JSONArray(Collection<?> c) {
super(c); super(c);
} }
/** /**
* Encode a list into JSON text and write it to out. * Encode a list into JSON text and write it to out.
* If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level. * If this list is also a JSONStreamAware or a JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
* *
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONValue#writeJSONString(Object, Writer) * @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONValue#writeJSONString(Object, Writer)
* *
* @param collection * @param collection
* @param out * @param out
*/ */
public static void writeJSONString(Collection collection, Writer out) throws IOException { public static void writeJSONString(Collection<?> collection, Writer out) throws IOException {
if (collection == null) { if (collection == null) {
out.write("null"); out.write("null");
return; return;
} }
boolean first = true; boolean first = true;
Iterator iter = collection.iterator(); Iterator<?> iter = collection.iterator();
out.write('['); out.write('[');
while (iter.hasNext()) { while (iter.hasNext()) {
@ -87,7 +87,7 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
* @param collection * @param collection
* @return JSON text, or "null" if list is null. * @return JSON text, or "null" if list is null.
*/ */
public static String toJSONString(Collection collection) { public static String toJSONString(Collection<?> collection) {
final StringWriter writer = new StringWriter(); final StringWriter writer = new StringWriter();
try { try {

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 implements Map, JSONAware, JSONStreamAware { public class JSONObject extends HashMap<Object, Object> implements JSONAware, JSONStreamAware {
private static final long serialVersionUID = -503443796854799292L; private static final long serialVersionUID = -503443796854799292L;
@ -30,27 +30,27 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
* *
* @param map * @param map
*/ */
public JSONObject(Map map) { public JSONObject(Map<?, ?> map) {
super(map); super(map);
} }
/** /**
* Encode a map into JSON text and write it to out. * Encode a map into JSON text and write it to out.
* If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level. * If this map is also a JSONAware or JSONStreamAware, JSONAware or JSONStreamAware specific behaviours will be ignored at this top level.
* *
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONValue#writeJSONString(Object, Writer) * @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONValue#writeJSONString(Object, Writer)
* *
* @param map * @param map
* @param out * @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) { if (map == null) {
out.write("null"); out.write("null");
return; return;
} }
boolean first = true; boolean first = true;
Iterator iter = map.entrySet().iterator(); Iterator<? extends Map.Entry<?, ?>> iter = map.entrySet().iterator();
out.write('{'); out.write('{');
while (iter.hasNext()) { while (iter.hasNext()) {
@ -58,7 +58,7 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
first = false; first = false;
else else
out.write(','); out.write(',');
Map.Entry entry = (Map.Entry) iter.next(); Map.Entry<?, ?> entry = iter.next();
out.write('\"'); out.write('\"');
out.write(escape(String.valueOf(entry.getKey()))); out.write(escape(String.valueOf(entry.getKey())));
out.write('\"'); out.write('\"');
@ -82,7 +82,7 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
* @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<?, ?> map) {
final StringWriter writer = new StringWriter(); final StringWriter writer = new StringWriter();
try { try {

View file

@ -109,10 +109,10 @@ public class JSONValue {
* <p> * <p>
* DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with * DO NOT call this method from writeJSONString(Writer) of a class that implements both JSONStreamAware and (Map or List) with
* "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead. * "this" as the first parameter, use JSONObject.writeJSONString(Map, Writer) or JSONArray.writeJSONString(List, Writer) instead.
* *
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONObject#writeJSONString(Map, Writer) * @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONObject#writeJSONString(Map, Writer)
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONArray#writeJSONString(List, Writer) * @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONArray#writeJSONString(List, Writer)
* *
* @param value * @param value
* @param writer * @param writer
*/ */
@ -166,12 +166,12 @@ public class JSONValue {
} }
if (value instanceof Map) { if (value instanceof Map) {
JSONObject.writeJSONString((Map) value, out); JSONObject.writeJSONString((Map<?, ?>) value, out);
return; return;
} }
if (value instanceof Collection) { if (value instanceof Collection) {
JSONArray.writeJSONString((Collection) value, out); JSONArray.writeJSONString((Collection<?>) value, out);
return; return;
} }
@ -251,7 +251,7 @@ public class JSONValue {
/** /**
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F). * Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
* *
* @param s * @param s
* @return * @return
*/ */

View file

@ -14,10 +14,10 @@ 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 createObjectContainer(); Map<Object, 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.
*/ */
List creatArrayContainer(); List<Object> creatArrayContainer();
} }

View file

@ -28,15 +28,15 @@ public class JSONParser {
public static final int S_END = 6; public static final int S_END = 6;
public static final int S_IN_ERROR = -1; public static final int S_IN_ERROR = -1;
private LinkedList handlerStatusStack; private LinkedList<Integer> handlerStatusStack;
private Yylex lexer = new Yylex((Reader) null); private Yylex lexer = new Yylex((Reader) null);
private Yytoken token = null; private Yytoken token = null;
private int status = S_INIT; private int status = S_INIT;
private int peekStatus(LinkedList statusStack) { private int peekStatus(LinkedList<Integer> statusStack) {
if (statusStack.size() == 0) if (statusStack.size() == 0)
return -1; return -1;
Integer status = (Integer) statusStack.getFirst(); Integer status = statusStack.getFirst();
return status.intValue(); return status.intValue();
} }
@ -52,7 +52,7 @@ public class JSONParser {
/** /**
* Reset the parser to the initial state with a new character reader. * Reset the parser to the initial state with a new character reader.
* *
* @param in * @param in
* - The new character reader. * - The new character reader.
* @throws IOException * @throws IOException
@ -92,7 +92,7 @@ public class JSONParser {
/** /**
* Parse JSON text into java object from the input source. * Parse JSON text into java object from the input source.
* *
* @param in * @param in
* @param containerFactory * @param containerFactory
* - Use this factory to createyour own JSON object and JSON array containers. * - Use this factory to createyour own JSON object and JSON array containers.
@ -109,8 +109,8 @@ public class JSONParser {
*/ */
public Object parse(Reader in, ContainerFactory containerFactory) throws IOException, ParseException { public Object parse(Reader in, ContainerFactory containerFactory) throws IOException, ParseException {
reset(in); reset(in);
LinkedList statusStack = new LinkedList(); LinkedList<Integer> statusStack = new LinkedList<>();
LinkedList valueStack = new LinkedList(); LinkedList<Object> valueStack = new LinkedList<>();
try { try {
do { do {
@ -180,15 +180,15 @@ public class JSONParser {
case Yytoken.TYPE_VALUE: case Yytoken.TYPE_VALUE:
statusStack.removeFirst(); statusStack.removeFirst();
String key = (String) valueStack.removeFirst(); String key = (String) valueStack.removeFirst();
Map parent = (Map) valueStack.getFirst(); Map<Object, Object> parent = (Map<Object, 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(); key = (String) valueStack.removeFirst();
parent = (Map) valueStack.getFirst(); parent = (Map<Object, Object>) valueStack.getFirst();
List 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));
@ -197,8 +197,8 @@ public class JSONParser {
case Yytoken.TYPE_LEFT_BRACE: case Yytoken.TYPE_LEFT_BRACE:
statusStack.removeFirst(); statusStack.removeFirst();
key = (String) valueStack.removeFirst(); key = (String) valueStack.removeFirst();
parent = (Map) valueStack.getFirst(); parent = (Map<Object, Object>) valueStack.getFirst();
Map newObject = createObjectContainer(containerFactory); Map<Object, 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));
@ -214,7 +214,7 @@ public class JSONParser {
case Yytoken.TYPE_COMMA: case Yytoken.TYPE_COMMA:
break; break;
case Yytoken.TYPE_VALUE: case Yytoken.TYPE_VALUE:
List val = (List) 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:
@ -227,16 +227,16 @@ public class JSONParser {
} }
break; break;
case Yytoken.TYPE_LEFT_BRACE: case Yytoken.TYPE_LEFT_BRACE:
val = (List) valueStack.getFirst(); val = (List<Object>) valueStack.getFirst();
Map newObject = createObjectContainer(containerFactory); Map<Object, 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: case Yytoken.TYPE_LEFT_SQUARE:
val = (List) valueStack.getFirst(); val = (List<Object>) valueStack.getFirst();
List 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));
@ -266,20 +266,20 @@ public class JSONParser {
token = new Yytoken(Yytoken.TYPE_EOF, null); token = new Yytoken(Yytoken.TYPE_EOF, null);
} }
private Map createObjectContainer(ContainerFactory containerFactory) { private Map<Object, Object> createObjectContainer(ContainerFactory containerFactory) {
if (containerFactory == null) if (containerFactory == null)
return new JSONObject(); return new JSONObject();
Map m = containerFactory.createObjectContainer(); Map<Object, Object> m = containerFactory.createObjectContainer();
if (m == null) if (m == null)
return new JSONObject(); return new JSONObject();
return m; return m;
} }
private List createArrayContainer(ContainerFactory containerFactory) { private List<Object> createArrayContainer(ContainerFactory containerFactory) {
if (containerFactory == null) if (containerFactory == null)
return new JSONArray(); return new JSONArray();
List l = containerFactory.creatArrayContainer(); List<Object> l = containerFactory.creatArrayContainer();
if (l == null) if (l == null)
return new JSONArray(); return new JSONArray();
@ -324,16 +324,16 @@ public class JSONParser {
public void parse(Reader in, ContentHandler contentHandler, boolean isResume) throws IOException, ParseException { public void parse(Reader in, ContentHandler contentHandler, boolean isResume) throws IOException, ParseException {
if (!isResume) { if (!isResume) {
reset(in); reset(in);
handlerStatusStack = new LinkedList(); handlerStatusStack = new LinkedList<>();
} else { } else {
if (handlerStatusStack == null) { if (handlerStatusStack == null) {
isResume = false; isResume = false;
reset(in); reset(in);
handlerStatusStack = new LinkedList(); handlerStatusStack = new LinkedList<>();
} }
} }
LinkedList statusStack = handlerStatusStack; LinkedList<Integer> statusStack = handlerStatusStack;
try { try {
do { do {