[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>
*/
public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
public class JSONArray extends ArrayList<Object> implements JSONAware, JSONStreamAware {
private static final long serialVersionUID = 3957988303675231981L;
/**
@ -33,27 +33,27 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
* @param c
* the collection whose elements are to be placed into this JSONArray
*/
public JSONArray(Collection c) {
public JSONArray(Collection<?> c) {
super(c);
}
/**
* 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.
*
*
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONValue#writeJSONString(Object, Writer)
*
*
* @param collection
* @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) {
out.write("null");
return;
}
boolean first = true;
Iterator iter = collection.iterator();
Iterator<?> iter = collection.iterator();
out.write('[');
while (iter.hasNext()) {
@ -87,7 +87,7 @@ public class JSONArray extends ArrayList implements JSONAware, JSONStreamAware {
* @param collection
* @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();
try {

View file

@ -16,7 +16,7 @@ import java.util.Map;
*
* @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;
@ -30,27 +30,27 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
*
* @param map
*/
public JSONObject(Map map) {
public JSONObject(Map<?, ?> map) {
super(map);
}
/**
* 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.
*
*
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONValue#writeJSONString(Object, Writer)
*
*
* @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<? extends Map.Entry<?, ?>> iter = map.entrySet().iterator();
out.write('{');
while (iter.hasNext()) {
@ -58,7 +58,7 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
first = false;
else
out.write(',');
Map.Entry entry = (Map.Entry) iter.next();
Map.Entry<?, ?> entry = iter.next();
out.write('\"');
out.write(escape(String.valueOf(entry.getKey())));
out.write('\"');
@ -82,7 +82,7 @@ public class JSONObject extends HashMap implements Map, JSONAware, JSONStreamAwa
* @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 {

View file

@ -109,10 +109,10 @@ public class JSONValue {
* <p>
* 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.
*
*
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONObject#writeJSONString(Map, Writer)
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONArray#writeJSONString(List, Writer)
*
*
* @param value
* @param writer
*/
@ -166,12 +166,12 @@ public class JSONValue {
}
if (value instanceof Map) {
JSONObject.writeJSONString((Map) value, out);
JSONObject.writeJSONString((Map<?, ?>) value, out);
return;
}
if (value instanceof Collection) {
JSONArray.writeJSONString((Collection) value, out);
JSONArray.writeJSONString((Collection<?>) value, out);
return;
}
@ -251,7 +251,7 @@ public class JSONValue {
/**
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
*
*
* @param s
* @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.
*/
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.
*/
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_IN_ERROR = -1;
private LinkedList handlerStatusStack;
private LinkedList<Integer> handlerStatusStack;
private Yylex lexer = new Yylex((Reader) null);
private Yytoken token = null;
private int status = S_INIT;
private int peekStatus(LinkedList statusStack) {
private int peekStatus(LinkedList<Integer> statusStack) {
if (statusStack.size() == 0)
return -1;
Integer status = (Integer) statusStack.getFirst();
Integer status = statusStack.getFirst();
return status.intValue();
}
@ -52,7 +52,7 @@ public class JSONParser {
/**
* Reset the parser to the initial state with a new character reader.
*
*
* @param in
* - The new character reader.
* @throws IOException
@ -92,7 +92,7 @@ public class JSONParser {
/**
* Parse JSON text into java object from the input source.
*
*
* @param in
* @param containerFactory
* - 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 {
reset(in);
LinkedList statusStack = new LinkedList();
LinkedList valueStack = new LinkedList();
LinkedList<Integer> statusStack = new LinkedList<>();
LinkedList<Object> valueStack = new LinkedList<>();
try {
do {
@ -180,15 +180,15 @@ public class JSONParser {
case Yytoken.TYPE_VALUE:
statusStack.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);
status = peekStatus(statusStack);
break;
case Yytoken.TYPE_LEFT_SQUARE:
statusStack.removeFirst();
key = (String) valueStack.removeFirst();
parent = (Map) valueStack.getFirst();
List newArray = createArrayContainer(containerFactory);
parent = (Map<Object, Object>) valueStack.getFirst();
List<Object> newArray = createArrayContainer(containerFactory);
parent.put(key, newArray);
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
@ -197,8 +197,8 @@ public class JSONParser {
case Yytoken.TYPE_LEFT_BRACE:
statusStack.removeFirst();
key = (String) valueStack.removeFirst();
parent = (Map) valueStack.getFirst();
Map newObject = createObjectContainer(containerFactory);
parent = (Map<Object, Object>) valueStack.getFirst();
Map<Object, Object> newObject = createObjectContainer(containerFactory);
parent.put(key, newObject);
status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status));
@ -214,7 +214,7 @@ public class JSONParser {
case Yytoken.TYPE_COMMA:
break;
case Yytoken.TYPE_VALUE:
List val = (List) valueStack.getFirst();
List<Object> val = (List<Object>) valueStack.getFirst();
val.add(token.value);
break;
case Yytoken.TYPE_RIGHT_SQUARE:
@ -227,16 +227,16 @@ public class JSONParser {
}
break;
case Yytoken.TYPE_LEFT_BRACE:
val = (List) valueStack.getFirst();
Map newObject = createObjectContainer(containerFactory);
val = (List<Object>) valueStack.getFirst();
Map<Object, Object> 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();
List newArray = createArrayContainer(containerFactory);
val = (List<Object>) valueStack.getFirst();
List<Object> newArray = createArrayContainer(containerFactory);
val.add(newArray);
status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status));
@ -266,20 +266,20 @@ public class JSONParser {
token = new Yytoken(Yytoken.TYPE_EOF, null);
}
private Map createObjectContainer(ContainerFactory containerFactory) {
private Map<Object, Object> createObjectContainer(ContainerFactory containerFactory) {
if (containerFactory == null)
return new JSONObject();
Map m = containerFactory.createObjectContainer();
Map<Object, Object> m = containerFactory.createObjectContainer();
if (m == null)
return new JSONObject();
return m;
}
private List createArrayContainer(ContainerFactory containerFactory) {
private List<Object> createArrayContainer(ContainerFactory containerFactory) {
if (containerFactory == null)
return new JSONArray();
List l = containerFactory.creatArrayContainer();
List<Object> l = containerFactory.creatArrayContainer();
if (l == null)
return new JSONArray();
@ -324,16 +324,16 @@ public class JSONParser {
public void parse(Reader in, ContentHandler contentHandler, boolean isResume) throws IOException, ParseException {
if (!isResume) {
reset(in);
handlerStatusStack = new LinkedList();
handlerStatusStack = new LinkedList<>();
} else {
if (handlerStatusStack == null) {
isResume = false;
reset(in);
handlerStatusStack = new LinkedList();
handlerStatusStack = new LinkedList<>();
}
}
LinkedList statusStack = handlerStatusStack;
LinkedList<Integer> statusStack = handlerStatusStack;
try {
do {