[json-simple]reduce code

This commit is contained in:
yushijinhun 2018-04-15 09:27:45 +08:00
parent b160f15420
commit 48a6919c6a
No known key found for this signature in database
GPG Key ID: 5BC167F73EA558E4
5 changed files with 28 additions and 112 deletions

View File

@ -60,7 +60,7 @@ public class JSONObject extends HashMap<String, Object> implements JSONAware, JS
out.write(','); out.write(',');
Map.Entry<String, ?> entry = iter.next(); Map.Entry<String, ?> entry = iter.next();
out.write('\"'); out.write('\"');
out.write(escape(entry.getKey())); out.write(JSONValue.escape(entry.getKey()));
out.write('\"'); out.write('\"');
out.write(':'); out.write(':');
JSONValue.writeJSONString(entry.getValue(), out); JSONValue.writeJSONString(entry.getValue(), out);
@ -117,17 +117,4 @@ public class JSONObject extends HashMap<String, Object> implements JSONAware, JS
return sb.toString(); return sb.toString();
} }
/**
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
* It's the same as JSONValue.escape() only for compatibility here.
*
* @see org.to2mbn.authlibinjector.internal.org.json.simple.JSONValue#escape(String)
*
* @param s
* @return
*/
public static String escape(String s) {
return JSONValue.escape(s);
}
} }

View File

@ -6,7 +6,6 @@ package org.to2mbn.authlibinjector.internal.org.json.simple;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.util.Collection; import java.util.Collection;
@ -19,62 +18,6 @@ import org.to2mbn.authlibinjector.internal.org.json.simple.parser.ParseException
* @author FangYidong<fangyidong@yahoo.com.cn> * @author FangYidong<fangyidong@yahoo.com.cn>
*/ */
public class JSONValue { public class JSONValue {
/**
* Parse JSON text into java object from the input source.
* Please use parseWithException() if you don't want to ignore the exception.
*
* @see org.to2mbn.authlibinjector.internal.org.json.simple.parser.JSONParser#parse(Reader)
* @see #parseWithException(Reader)
*
* @param in
* @return Instance of the following:
* org.json.simple.JSONObject,
* org.json.simple.JSONArray,
* java.lang.String,
* java.lang.Number,
* java.lang.Boolean,
* null
*
* @deprecated this method may throw an {@code Error} instead of returning
* {@code null}; please use {@link JSONValue#parseWithException(Reader)}
* instead
*/
@Deprecated
public static Object parse(Reader in) {
try {
JSONParser parser = new JSONParser();
return parser.parse(in);
} catch (Exception e) {
return null;
}
}
/**
* Parse JSON text into java object from the given string.
* Please use parseWithException() if you don't want to ignore the exception.
*
* @see org.to2mbn.authlibinjector.internal.org.json.simple.parser.JSONParser#parse(Reader)
* @see #parseWithException(Reader)
*
* @param s
* @return Instance of the following:
* org.json.simple.JSONObject,
* org.json.simple.JSONArray,
* java.lang.String,
* java.lang.Number,
* java.lang.Boolean,
* null
*
* @deprecated this method may throw an {@code Error} instead of returning
* {@code null}; please use {@link JSONValue#parseWithException(String)}
* instead
*/
@Deprecated
public static Object parse(String s) {
StringReader in = new StringReader(s);
return parse(in);
}
/** /**
* Parse JSON text into java object from the input source. * Parse JSON text into java object from the input source.
* *
@ -92,14 +35,12 @@ public class JSONValue {
* @throws IOException * @throws IOException
* @throws ParseException * @throws ParseException
*/ */
public static Object parseWithException(Reader in) throws IOException, ParseException { public static Object parse(Reader in) throws IOException, ParseException {
JSONParser parser = new JSONParser(); return new JSONParser().parse(in);
return parser.parse(in);
} }
public static Object parseWithException(String s) throws ParseException { public static Object parse(String s) throws ParseException {
JSONParser parser = new JSONParser(); return new JSONParser().parse(s);
return parser.parse(s);
} }
/** /**

View File

@ -120,19 +120,19 @@ public class JSONParser {
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(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(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(status);
valueStack.addFirst(createArrayContainer(containerFactory)); valueStack.addFirst(createArrayContainer(containerFactory));
break; break;
} }
@ -158,7 +158,7 @@ public class JSONParser {
String key = (String) token.value; String key = (String) token.value;
valueStack.addFirst(key); valueStack.addFirst(key);
status = S_PASSED_PAIR_KEY; status = S_PASSED_PAIR_KEY;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(status);
} else { } else {
status = S_IN_ERROR; status = S_IN_ERROR;
} }
@ -202,7 +202,7 @@ public class JSONParser {
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(status);
valueStack.addFirst(newArray); valueStack.addFirst(newArray);
break; break;
} }
@ -214,7 +214,7 @@ public class JSONParser {
Map<String, Object> newObject = createObjectContainer(containerFactory); 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(status);
valueStack.addFirst(newObject); valueStack.addFirst(newObject);
break; break;
} }
@ -250,7 +250,7 @@ public class JSONParser {
Map<String, Object> newObject = createObjectContainer(containerFactory); 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(status);
valueStack.addFirst(newObject); valueStack.addFirst(newObject);
break; break;
} }
@ -260,7 +260,7 @@ public class JSONParser {
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(status);
valueStack.addFirst(newArray); valueStack.addFirst(newArray);
break; break;
} }
@ -367,19 +367,19 @@ public class JSONParser {
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(status);
if (!contentHandler.primitive(token.value)) if (!contentHandler.primitive(token.value))
return; return;
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(status);
if (!contentHandler.startObject()) if (!contentHandler.startObject())
return; return;
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(status);
if (!contentHandler.startArray()) if (!contentHandler.startArray())
return; return;
break; break;
@ -408,7 +408,7 @@ public class JSONParser {
if (token.value instanceof String) { if (token.value instanceof String) {
String key = (String) token.value; String key = (String) token.value;
status = S_PASSED_PAIR_KEY; status = S_PASSED_PAIR_KEY;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(status);
if (!contentHandler.startObjectEntry(key)) if (!contentHandler.startObjectEntry(key))
return; return;
} else { } else {
@ -446,17 +446,17 @@ public class JSONParser {
break; break;
case Yytoken.TYPE_LEFT_SQUARE: case Yytoken.TYPE_LEFT_SQUARE:
statusStack.removeFirst(); statusStack.removeFirst();
statusStack.addFirst(new Integer(S_IN_PAIR_VALUE)); statusStack.addFirst(S_IN_PAIR_VALUE);
status = S_IN_ARRAY; status = S_IN_ARRAY;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(status);
if (!contentHandler.startArray()) if (!contentHandler.startArray())
return; return;
break; break;
case Yytoken.TYPE_LEFT_BRACE: case Yytoken.TYPE_LEFT_BRACE:
statusStack.removeFirst(); statusStack.removeFirst();
statusStack.addFirst(new Integer(S_IN_PAIR_VALUE)); statusStack.addFirst(S_IN_PAIR_VALUE);
status = S_IN_OBJECT; status = S_IN_OBJECT;
statusStack.addFirst(new Integer(status)); statusStack.addFirst(status);
if (!contentHandler.startObject()) if (!contentHandler.startObject())
return; return;
break; break;
@ -497,13 +497,13 @@ public class JSONParser {
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(status);
if (!contentHandler.startObject()) if (!contentHandler.startObject())
return; return;
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(status);
if (!contentHandler.startArray()) if (!contentHandler.startArray())
return; return;
break; break;
@ -522,16 +522,7 @@ public class JSONParser {
throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token); throw new ParseException(getPosition(), ParseException.ERROR_UNEXPECTED_TOKEN, token);
} }
} while (token.type != Yytoken.TYPE_EOF); } while (token.type != Yytoken.TYPE_EOF);
} catch (IOException ie) { } catch (IOException | ParseException e) {
status = S_IN_ERROR;
throw ie;
} catch (ParseException pe) {
status = S_IN_ERROR;
throw pe;
} catch (RuntimeException re) {
status = S_IN_ERROR;
throw re;
} catch (Error e) {
status = S_IN_ERROR; status = S_IN_ERROR;
throw e; throw e;
} }

View File

@ -250,9 +250,6 @@ class Yylex {
*/ */
private int zzEndRead; private int zzEndRead;
/** number of newlines encountered up to the start of the matched text */
private int yyline;
/** the number of characters up to the start of the matched text */ /** the number of characters up to the start of the matched text */
private int yychar; private int yychar;
@ -390,7 +387,7 @@ class Yylex {
zzAtEOF = false; zzAtEOF = false;
zzEndRead = zzStartRead = 0; zzEndRead = zzStartRead = 0;
zzCurrentPos = zzMarkedPos = 0; zzCurrentPos = zzMarkedPos = 0;
yyline = yychar = 0; yychar = 0;
zzLexicalState = YYINITIAL; zzLexicalState = YYINITIAL;
} }

View File

@ -7,7 +7,7 @@ package org.to2mbn.authlibinjector.internal.org.json.simple.parser;
/** /**
* @author FangYidong<fangyidong@yahoo.com.cn> * @author FangYidong<fangyidong@yahoo.com.cn>
*/ */
public class Yytoken { class Yytoken {
public static final int TYPE_VALUE = 0;// JSON primitive value: string,number,boolean,null public static final int TYPE_VALUE = 0;// JSON primitive value: string,number,boolean,null
public static final int TYPE_LEFT_BRACE = 1; public static final int TYPE_LEFT_BRACE = 1;
public static final int TYPE_RIGHT_BRACE = 2; public static final int TYPE_RIGHT_BRACE = 2;