Java Code Examples for com.alibaba.fastjson.parser.JSONToken#NULL

The following examples show how to use com.alibaba.fastjson.parser.JSONToken#NULL . 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: AbstractSerializer.java    From joyrpc with Apache License 2.0 6 votes vote down vote up
/**
 * 读取字符串
 *
 * @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 2
Source File: ArrayListTypeFieldDeserializer.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: CollectionCodec.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    if (parser.lexer.token() == JSONToken.NULL) {
        parser.lexer.nextToken(JSONToken.COMMA);
        return null;
    }
    
    if (type == JSONArray.class) {
        JSONArray array = new JSONArray();
        parser.parseArray(array);
        return (T) array;
    }

    Collection list = TypeUtils.createCollection(type);

    Type itemType = TypeUtils.getCollectionItemType(type);
    parser.parseArray(itemType, list, fieldName);

    return (T) list;
}
 
Example 4
Source File: JSON.java    From uavstack with Apache License 2.0 6 votes vote down vote up
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 5
Source File: JSON.java    From uavstack with Apache License 2.0 6 votes vote down vote up
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 6
Source File: PageDeserializer.java    From phone with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked"})
    public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {

    	if (parser.getLexer().token() == JSONToken.NULL) {
            parser.getLexer().nextToken(JSONToken.COMMA);
            return null;
        }
        String input = parser.getInput();
//        final String inputValue = input.replaceAll("\\{.+list:\\[(.++)\\]\\}", "$1");
//        ReflectionUtil.setField(ReflectionUtil.findField(parser.getClass(), "input"), parser,inputValue );
        Page<T> list = null;
        if (null==parser.getInput()||"null".equals(input)) {
			return null;
		}
        JSONObject jo = parser.parseObject();
        JSONArray ja = jo.getJSONArray("list");
        if (ja!=null) {
            List<T> vList = (List<T>) ja;
			list = new Page<>();
			list.addAll(vList);
	        list.setTotal(jo.getIntValue("total"));
		}

        return (T) list;
    }
 
Example 7
Source File: AbstractInvocationCodec.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@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 8
Source File: AbstractResponsePayloadCodec.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
@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 9
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 读取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 10
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 读取字符串数组
 *
 * @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 11
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 解析对象数组
 *
 * @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: AwtCodec.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@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 13
Source File: JavaBeanDeserializer.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
    protected static void parseArray(Collection collection, //
                              ObjectDeserializer deser, //
                              DefaultJSONParser parser, //
                              Type type, //
                              Object fieldName) {

        final JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
        int token = lexer.token();
        if (token == JSONToken.NULL) {
            lexer.nextToken(JSONToken.COMMA);
            token = lexer.token();
            return;
        }

        if (token != JSONToken.LBRACKET) {
            parser.throwException(token);
        }
        char ch = lexer.getCurrent();
        if (ch == '[') {
            lexer.next();
            lexer.setToken(JSONToken.LBRACKET);
        } else {
            lexer.nextToken(JSONToken.LBRACKET);
        }
        
        if (lexer.token() == JSONToken.RBRACKET) {
            lexer.nextToken();
            return;
        }

        int index = 0;
        for (;;) {
            Object item = deser.deserialze(parser, type, index);
            collection.add(item);
            index++;
            if (lexer.token() == JSONToken.COMMA) {
                ch = lexer.getCurrent();
                if (ch == '[') {
                    lexer.next();
                    lexer.setToken(JSONToken.LBRACKET);
                } else {
                    lexer.nextToken(JSONToken.LBRACKET);
                }
            } else {
                break;
            }
        }
        
        token = lexer.token();
        if (token != JSONToken.RBRACKET) {
            parser.throwException(token);
        }
        
        ch = lexer.getCurrent();
        if (ch == ',') {
            lexer.next();
            lexer.setToken(JSONToken.COMMA);
        } else {
            lexer.nextToken(JSONToken.COMMA);
        }
//        parser.accept(JSONToken.RBRACKET, JSONToken.COMMA);
    }
 
Example 14
Source File: IntegerCodec.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@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 15
Source File: AbstractSerializer.java    From joyrpc with Apache License 2.0 2 votes vote down vote up
/**
 * 解析对象
 *
 * @param parser 解析器
 * @param lexer  语法
 * @param type   类型
 * @return
 */
protected Object parseObject(final DefaultJSONParser parser, final JSONLexer lexer, final Type type) {
    return lexer.token() != JSONToken.NULL ? parser.parseObject(type) : null;
}