Java Code Examples for com.alibaba.fastjson.parser.JSONLexer#token()
The following examples show how to use
com.alibaba.fastjson.parser.JSONLexer#token() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSON.java From uavstack with Apache License 2.0 | 6 votes |
public static <T> List<T> parseArray(String text, Class<T> clazz) { if (text == null) { return null; } List<T> list; DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance()); JSONLexer lexer = parser.lexer; int token = lexer.token(); if (token == JSONToken.NULL) { lexer.nextToken(); list = null; } else if (token == JSONToken.EOF && lexer.isBlankInput()) { list = null; } else { list = new ArrayList<T>(); parser.parseArray(clazz, list); parser.handleResovleTask(list); } parser.close(); return list; }
Example 2
Source File: BigDecimalCodec.java From uavstack with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") public static <T> T deserialze(DefaultJSONParser parser) { final JSONLexer lexer = parser.lexer; if (lexer.token() == JSONToken.LITERAL_INT) { BigDecimal decimalValue = lexer.decimalValue(); lexer.nextToken(JSONToken.COMMA); return (T) decimalValue; } if (lexer.token() == JSONToken.LITERAL_FLOAT) { BigDecimal val = lexer.decimalValue(); lexer.nextToken(JSONToken.COMMA); return (T) val; } Object value = parser.parse(); return value == null // ? null // : (T) TypeUtils.castToBigDecimal(value); }
Example 3
Source File: JSONReader.java From uavstack with Apache License 2.0 | 6 votes |
public String readString() { Object object; if (context == null) { object = parser.parse(); } else { readBefore(); JSONLexer lexer = parser.lexer; if (context.state == JSONStreamContext.StartObject && lexer.token() == JSONToken.IDENTIFIER) { object = lexer.stringVal(); lexer.nextToken(); } else { object = parser.parse(); } readAfter(); } return TypeUtils.castToString(object); }
Example 4
Source File: ArrayListTypeFieldDeserializer.java From uavstack with Apache License 2.0 | 6 votes |
@SuppressWarnings("rawtypes") @Override public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) { JSONLexer lexer = parser.lexer; final int token = lexer.token(); if (token == JSONToken.NULL || (token == JSONToken.LITERAL_STRING && lexer.stringVal().length() == 0)) { setValue(object, null); return; } ArrayList list = new ArrayList(); ParseContext context = parser.getContext(); parser.setContext(context, object, fieldInfo.name); parseArray(parser, objectType, list); parser.setContext(context); if (object == null) { fieldValues.put(fieldInfo.name, list); } else { setValue(object, list); } }
Example 5
Source File: JSON.java From uavstack with Apache License 2.0 | 6 votes |
public static JSONArray parseArray(String text) { if (text == null) { return null; } DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance()); JSONArray array; JSONLexer lexer = parser.lexer; if (lexer.token() == JSONToken.NULL) { lexer.nextToken(); array = null; } else if (lexer.token() == JSONToken.EOF) { array = null; } else { array = new JSONArray(); parser.parseArray(array); parser.handleResovleTask(array); } parser.close(); return array; }
Example 6
Source File: AbstractSerializer.java From joyrpc with Apache License 2.0 | 5 votes |
/** * 读取MAP * * @param parser 解析器 * @param lexer 文法 * @param field 字段 */ protected Map<String, Object> parseMap(final DefaultJSONParser parser, final JSONLexer lexer, final String field) { Map<String, Object> result = null; switch (lexer.token()) { case JSONToken.LBRACE: result = parser.parseObject(); break; case JSONToken.NULL: lexer.nextToken(); break; default: throw new SerializerException("syntax error: invalid " + field); } return result; }
Example 7
Source File: AbstractSerializer.java From joyrpc with Apache License 2.0 | 5 votes |
/** * 读取字符串数组 * * @param parser 解析器 * @param lexer 文法 * @param field 字段 */ protected String[] parseStrings(final DefaultJSONParser parser, final JSONLexer lexer, final String field) { String result[] = null; switch (lexer.token()) { case JSONToken.LBRACKET: result = parser.parseObject(String[].class); break; case JSONToken.NULL: lexer.nextToken(); break; default: throw new SerializerException("syntax error: invalid " + field); } return result; }
Example 8
Source File: FastJsonSObjectCodec.java From actframework with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONLexer lexer = parser.getLexer(); if (lexer.token() == JSONToken.LITERAL_STRING) { String text = lexer.stringVal(); lexer.nextToken(JSONToken.COMMA); return (T) resolver.resolve(text); } else { throw new UnsupportedOperationException(); } }
Example 9
Source File: AbstractSerializer.java From joyrpc with Apache License 2.0 | 5 votes |
/** * 解析对象数组 * * @param parser 解析器 * @param lexer 语法 * @param types 类型 * @param field 字段 */ protected Object[] parseObjects(final DefaultJSONParser parser, final JSONLexer lexer, final Type[] types, final String field) { Object[] result = null; //空数组 if (lexer.token() == JSONToken.NULL) { if (types.length == 0) { lexer.nextToken(); } else { throw new SerializerException("syntax error: invalid " + field); } } else { //解析参数 JSONReader reader = new JSONReader(parser); reader.startArray(); int i = 0; result = new Object[types.length]; while (reader.hasNext()) { if (i >= result.length) { throw new SerializerException("syntax error: invalid " + field); } result[i] = reader.readObject(types[i]); i++; } reader.endArray(); } return result; }
Example 10
Source File: AbstractResponsePayloadCodec.java From joyrpc with Apache License 2.0 | 5 votes |
/** * 解析应答 * * @param parser 解析器 * @param lexer 文法 * @return 应答 */ protected ResponsePayload parse(final DefaultJSONParser parser, final JSONLexer lexer) { ResponsePayload payload = new ResponsePayload(); String key; int token; try { String typeName = null; for (; ; ) { // lexer.scanSymbol key = lexer.scanSymbol(parser.getSymbolTable()); if (key == null) { token = lexer.token(); if (token == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); break; } else if (token == JSONToken.COMMA) { if (lexer.isEnabled(Feature.AllowArbitraryCommas)) { continue; } } } lexer.nextTokenWithColon(JSONToken.LITERAL_STRING); if (RES_CLASS.equals(key)) { typeName = parseString(lexer, RES_CLASS, false); } else if (RESPONSE.equals(key)) { payload.setResponse(parseResponse(parser, lexer, typeName)); } else if (EXCEPTION.equals(key)) { payload.setException((Throwable) parseObject(parser, lexer, getThrowableType(typeName))); } if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); break; } } return payload; } catch (ClassNotFoundException e) { throw new SerializerException(e.getMessage()); } }
Example 11
Source File: AbstractResponsePayloadCodec.java From joyrpc with Apache License 2.0 | 5 votes |
@Override public <T> T deserialze(final DefaultJSONParser parser, final Type type, final Object fieldName) { JSONLexer lexer = parser.getLexer(); switch (lexer.token()) { case JSONToken.NULL: lexer.nextToken(); return null; case JSONToken.LBRACE: return (T) parse(parser, lexer); default: return null; } }
Example 12
Source File: BigIntegerCodec.java From uavstack with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public static <T> T deserialze(DefaultJSONParser parser) { final JSONLexer lexer = parser.lexer; if (lexer.token() == JSONToken.LITERAL_INT) { String val = lexer.numberString(); lexer.nextToken(JSONToken.COMMA); return (T) new BigInteger(val); } Object value = parser.parse(); return value == null // ? null // : (T) TypeUtils.castToBigInteger(value); }
Example 13
Source File: LongCodec.java From uavstack with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { final JSONLexer lexer = parser.lexer; Long longObject; try { final int token = lexer.token(); if (token == JSONToken.LITERAL_INT) { long longValue = lexer.longValue(); lexer.nextToken(JSONToken.COMMA); longObject = Long.valueOf(longValue); } else if (token == JSONToken.LITERAL_FLOAT) { BigDecimal number = lexer.decimalValue(); longObject = TypeUtils.longValue(number); lexer.nextToken(JSONToken.COMMA); } else { if (token == JSONToken.LBRACE) { JSONObject jsonObject = new JSONObject(true); parser.parseObject(jsonObject); longObject = TypeUtils.castToLong(jsonObject); } else { Object value = parser.parse(); longObject = TypeUtils.castToLong(value); } if (longObject == null) { return null; } } } catch (Exception ex) { throw new JSONException("parseLong error, field : " + fieldName, ex); } return clazz == AtomicLong.class // ? (T) new AtomicLong(longObject.longValue()) // : (T) longObject; }
Example 14
Source File: AbstractInvocationCodec.java From joyrpc with Apache License 2.0 | 5 votes |
@Override public <T> T deserialze(final DefaultJSONParser parser, final Type type, final Object fieldName) { JSONLexer lexer = parser.getLexer(); switch (lexer.token()) { case JSONToken.NULL: lexer.nextToken(); return null; case JSONToken.LBRACE: return (T) parse(parser, lexer); default: return null; } }
Example 15
Source File: AwtCodec.java From uavstack with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONLexer lexer = parser.lexer; if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); return null; } if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) { throw new JSONException("syntax error"); } lexer.nextToken(); T obj; if (type == Point.class) { obj = (T) parsePoint(parser, fieldName); } else if (type == Rectangle.class) { obj = (T) parseRectangle(parser); } else if (type == Color.class) { obj = (T) parseColor(parser); } else if (type == Font.class) { obj = (T) parseFont(parser); } else { throw new JSONException("not support awt class : " + type); } ParseContext context = parser.getContext(); parser.setContext(obj, fieldName); parser.setContext(context); return obj; }
Example 16
Source File: TimeDeserializer.java From uavstack with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { JSONLexer lexer = parser.lexer; if (lexer.token() == JSONToken.COMMA) { lexer.nextToken(JSONToken.LITERAL_STRING); if (lexer.token() != JSONToken.LITERAL_STRING) { throw new JSONException("syntax error"); } lexer.nextTokenWithColon(JSONToken.LITERAL_INT); if (lexer.token() != JSONToken.LITERAL_INT) { throw new JSONException("syntax error"); } long time = lexer.longValue(); lexer.nextToken(JSONToken.RBRACE); if (lexer.token() != JSONToken.RBRACE) { throw new JSONException("syntax error"); } lexer.nextToken(JSONToken.COMMA); return (T) new java.sql.Time(time); } Object val = parser.parse(); if (val == null) { return null; } if (val instanceof java.sql.Time) { return (T) val; } else if (val instanceof BigDecimal) { return (T) new java.sql.Time(TypeUtils.longValue((BigDecimal) val)); } else if (val instanceof Number) { return (T) new java.sql.Time(((Number) val).longValue()); } else if (val instanceof String) { String strVal = (String) val; if (strVal.length() == 0) { return null; } long longVal; JSONScanner dateLexer = new JSONScanner(strVal); if (dateLexer.scanISO8601DateIfMatch()) { longVal = dateLexer.getCalendar().getTimeInMillis(); } else { boolean isDigit = true; for (int i = 0; i< strVal.length(); ++i) { char ch = strVal.charAt(i); if (ch < '0' || ch > '9') { isDigit = false; break; } } if (!isDigit) { dateLexer.close(); return (T) java.sql.Time.valueOf(strVal); } longVal = Long.parseLong(strVal); } dateLexer.close(); return (T) new java.sql.Time(longVal); } throw new JSONException("parse error"); }
Example 17
Source File: BooleanCodec.java From uavstack with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { final JSONLexer lexer = parser.lexer; Boolean boolObj; try { if (lexer.token() == JSONToken.TRUE) { lexer.nextToken(JSONToken.COMMA); boolObj = Boolean.TRUE; } else if (lexer.token() == JSONToken.FALSE) { lexer.nextToken(JSONToken.COMMA); boolObj = Boolean.FALSE; } else if (lexer.token() == JSONToken.LITERAL_INT) { int intValue = lexer.intValue(); lexer.nextToken(JSONToken.COMMA); if (intValue == 1) { boolObj = Boolean.TRUE; } else { boolObj = Boolean.FALSE; } } else { Object value = parser.parse(); if (value == null) { return null; } boolObj = TypeUtils.castToBoolean(value); } } catch (Exception ex) { throw new JSONException("parseBoolean error, field : " + fieldName, ex); } if (clazz == AtomicBoolean.class) { return (T) new AtomicBoolean(boolObj.booleanValue()); } return (T) boolObj; }
Example 18
Source File: IntegerCodec.java From uavstack with Apache License 2.0 | 4 votes |
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) { final JSONLexer lexer = parser.lexer; final int token = lexer.token(); if (token == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); return null; } Integer intObj; try { if (token == JSONToken.LITERAL_INT) { int val = lexer.intValue(); lexer.nextToken(JSONToken.COMMA); intObj = Integer.valueOf(val); } else if (token == JSONToken.LITERAL_FLOAT) { BigDecimal number = lexer.decimalValue(); intObj = TypeUtils.intValue(number); lexer.nextToken(JSONToken.COMMA); } else { if (token == JSONToken.LBRACE) { JSONObject jsonObject = new JSONObject(true); parser.parseObject(jsonObject); intObj = TypeUtils.castToInt(jsonObject); } else { Object value = parser.parse(); intObj = TypeUtils.castToInt(value); } } } catch (Exception ex) { throw new JSONException("parseInt error, field : " + fieldName, ex); } if (clazz == AtomicInteger.class) { return (T) new AtomicInteger(intObj.intValue()); } return (T) intObj; }
Example 19
Source File: AwtCodec.java From uavstack with Apache License 2.0 | 4 votes |
protected Font parseFont(DefaultJSONParser parser) { JSONLexer lexer = parser.lexer; int size = 0, style = 0; String name = null; for (;;) { if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(); break; } String key; if (lexer.token() == JSONToken.LITERAL_STRING) { key = lexer.stringVal(); lexer.nextTokenWithColon(JSONToken.LITERAL_INT); } else { throw new JSONException("syntax error"); } if (key.equalsIgnoreCase("name")) { if (lexer.token() == JSONToken.LITERAL_STRING) { name = lexer.stringVal(); lexer.nextToken(); } else { throw new JSONException("syntax error"); } } else if (key.equalsIgnoreCase("style")) { if (lexer.token() == JSONToken.LITERAL_INT) { style = lexer.intValue(); lexer.nextToken(); } else { throw new JSONException("syntax error"); } } else if (key.equalsIgnoreCase("size")) { if (lexer.token() == JSONToken.LITERAL_INT) { size = lexer.intValue(); lexer.nextToken(); } else { throw new JSONException("syntax error"); } } else { throw new JSONException("syntax error, " + key); } if (lexer.token() == JSONToken.COMMA) { lexer.nextToken(JSONToken.LITERAL_STRING); } } return new Font(name, style, size); }
Example 20
Source File: AwtCodec.java From uavstack with Apache License 2.0 | 4 votes |
protected Color parseColor(DefaultJSONParser parser) { JSONLexer lexer = parser.lexer; int r = 0, g = 0, b = 0, alpha = 0; for (;;) { if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(); break; } String key; if (lexer.token() == JSONToken.LITERAL_STRING) { key = lexer.stringVal(); lexer.nextTokenWithColon(JSONToken.LITERAL_INT); } else { throw new JSONException("syntax error"); } int val; if (lexer.token() == JSONToken.LITERAL_INT) { val = lexer.intValue(); lexer.nextToken(); } else { throw new JSONException("syntax error"); } if (key.equalsIgnoreCase("r")) { r = val; } else if (key.equalsIgnoreCase("g")) { g = val; } else if (key.equalsIgnoreCase("b")) { b = val; } else if (key.equalsIgnoreCase("alpha")) { alpha = val; } else { throw new JSONException("syntax error, " + key); } if (lexer.token() == JSONToken.COMMA) { lexer.nextToken(JSONToken.LITERAL_STRING); } } return new Color(r, g, b, alpha); }