Java Code Examples for com.alibaba.fastjson.parser.JSONLexer#nextToken()
The following examples show how to use
com.alibaba.fastjson.parser.JSONLexer#nextToken() .
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 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 2
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 3
Source File: AbstractSerializer.java From joyrpc with Apache License 2.0 | 6 votes |
/** * 读取字符串 * * @param lexer 文法 * @param field 字段 * @param nullable 是否可以null */ protected String parseString(final JSONLexer lexer, final String field, final boolean nullable) { String result = null; switch (lexer.token()) { case JSONToken.LITERAL_STRING: result = lexer.stringVal(); lexer.nextToken(); break; case JSONToken.NULL: if (!nullable) { throw new SerializerException("syntax error: invalid " + field); } lexer.nextToken(); break; default: throw new SerializerException("syntax error: invalid " + field); } return result; }
Example 4
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 5
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 6
Source File: AwtCodec.java From uavstack with Apache License 2.0 | 5 votes |
private Object parseRef(DefaultJSONParser parser, Object fieldName) { JSONLexer lexer = parser.getLexer(); lexer.nextTokenWithColon(JSONToken.LITERAL_STRING); String ref = lexer.stringVal(); parser.setContext(parser.getContext(), fieldName); parser.addResolveTask(new DefaultJSONParser.ResolveTask(parser.getContext(), ref)); parser.popContext(); parser.setResolveStatus(DefaultJSONParser.NeedToResolve); lexer.nextToken(JSONToken.RBRACE); parser.accept(JSONToken.RBRACE); return null; }
Example 7
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 8
Source File: FastJsonKeywordCodec.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) Keyword.of(text); } else { throw new UnsupportedOperationException(); } }
Example 9
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 10
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 11
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 12
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 13
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 14
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 15
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 16
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 17
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 18
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); }
Example 19
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 20
Source File: AbstractInvocationCodec.java From joyrpc with Apache License 2.0 | 4 votes |
/** * 解析Invocation * * @param parser 解析器 * @param lexer 文法 * @return */ protected Call parse(final DefaultJSONParser parser, final JSONLexer lexer) { Call invocation = createInvocation(); String key; int token; try { 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 (classNameKey.equals(key)) { invocation.setClassName(parseString(lexer, classNameKey, false)); } else if (aliasKey.equals(key)) { invocation.setAlias(parseString(lexer, aliasKey, true)); } else if (methodNameKey.equals(key)) { invocation.setMethodName(parseString(lexer, methodNameKey, false)); } else if (argsTypeKey.equals(key)) { invocation.setArgsType(parseStrings(parser, lexer, argsTypeKey)); } else if (argsKey.equals(key)) { invocation.setArgs(parseObjects(parser, lexer, invocation.computeTypes(), argsKey)); } else if (attachmentsKey.equals(key)) { invocation.addAttachments(parseMap(parser, lexer, attachmentsKey)); } if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); break; } } return invocation; } catch (ClassNotFoundException | NoSuchMethodException | MethodOverloadException e) { throw new SerializerException(e.getMessage()); } }