[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(',');
Map.Entry<String, ?> entry = iter.next();
out.write('\"');
out.write(escape(entry.getKey()));
out.write(JSONValue.escape(entry.getKey()));
out.write('\"');
out.write(':');
JSONValue.writeJSONString(entry.getValue(), out);
@ -117,17 +117,4 @@ public class JSONObject extends HashMap<String, Object> implements JSONAware, JS
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.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Collection;
@ -19,62 +18,6 @@ import org.to2mbn.authlibinjector.internal.org.json.simple.parser.ParseException
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
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.
*
@ -92,14 +35,12 @@ public class JSONValue {
* @throws IOException
* @throws ParseException
*/
public static Object parseWithException(Reader in) throws IOException, ParseException {
JSONParser parser = new JSONParser();
return parser.parse(in);
public static Object parse(Reader in) throws IOException, ParseException {
return new JSONParser().parse(in);
}
public static Object parseWithException(String s) throws ParseException {
JSONParser parser = new JSONParser();
return parser.parse(s);
public static Object parse(String s) throws ParseException {
return new JSONParser().parse(s);
}
/**

View file

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

View file

@ -250,9 +250,6 @@ class Yylex {
*/
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 */
private int yychar;
@ -313,7 +310,7 @@ class Yylex {
* Refills the input buffer.
*
* @return <code>false</code>, iff there was new input.
*
*
* @exception java.io.IOException
* if any I/O-Error occurs
*/
@ -390,7 +387,7 @@ class Yylex {
zzAtEOF = false;
zzEndRead = zzStartRead = 0;
zzCurrentPos = zzMarkedPos = 0;
yyline = yychar = 0;
yychar = 0;
zzLexicalState = YYINITIAL;
}
@ -421,7 +418,7 @@ class Yylex {
/**
* Returns the character at position <tt>pos</tt> from the
* matched text.
*
*
* It is equivalent to yytext().charAt(pos), but faster
*
* @param pos

View file

@ -7,7 +7,7 @@ package org.to2mbn.authlibinjector.internal.org.json.simple.parser;
/**
* @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_LEFT_BRACE = 1;
public static final int TYPE_RIGHT_BRACE = 2;