Java Code Examples for com.alibaba.fastjson.parser.DefaultJSONParser#parse()

The following examples show how to use com.alibaba.fastjson.parser.DefaultJSONParser#parse() . 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: JSONPath_t.java    From coming with MIT License 6 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
    if (context.eval) {
        Object object = parser.parse();
        if (object instanceof List) {
            int[] indexes = new int[this.indexes.length];
            System.arraycopy(this.indexes, 0, indexes, 0, indexes.length);
            boolean noneNegative = indexes[0] >= 0;

            List list = (List) object;
            if (noneNegative) {
                for (int i = list.size() - 1; i >= 0; i--) {
                    if (Arrays.binarySearch(indexes, i) < 0) {
                        list.remove(i);
                    }
                }
                context.object = list;
                return;
            }
        }
    }
    throw new UnsupportedOperationException();
}
 
Example 2
Source File: JSONPath_s.java    From coming with MIT License 6 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
    if (context.eval) {
        Object object = parser.parse();
        if (object instanceof List) {
            int[] indexes = new int[this.indexes.length];
            System.arraycopy(this.indexes, 0, indexes, 0, indexes.length);
            boolean noneNegative = indexes[0] >= 0;

            List list = (List) object;
            if (noneNegative) {
                for (int i = list.size() - 1; i >= 0; i--) {
                    if (Arrays.binarySearch(indexes, i) < 0) {
                        list.remove(i);
                    }
                }
                context.object = list;
                return;
            }
        }
    }
    throw new UnsupportedOperationException();
}
 
Example 3
Source File: BigDecimalCodec.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: PropertyProcessableDeserializer.java    From uavstack with Apache License 2.0 5 votes vote down vote up
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
    PropertyProcessable processable;
    try {
        processable = this.type.newInstance();
    } catch (Exception e) {
        throw new JSONException("craete instance error");
    }

    Object object =parser.parse(processable, fieldName);

    return (T) object;
}
 
Example 5
Source File: JSONPath_t.java    From coming with MIT License 5 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
    JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
    if (lexer.seekArrayToItem(index)
            && context.eval)
    {
        context.object = parser.parse();
    }
}
 
Example 6
Source File: JSONPath_s.java    From coming with MIT License 5 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
    JSONLexerBase lexer = (JSONLexerBase) parser.lexer;
    if (lexer.seekArrayToItem(index)
            && context.eval)
    {
        context.object = parser.parse();
    }
}
 
Example 7
Source File: JSON.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @since 1.2.38
 */
public static Object parse(String text, ParserConfig config, int features) {
    if (text == null) {
        return null;
    }

    DefaultJSONParser parser = new DefaultJSONParser(text, config, features);
    Object value = parser.parse();

    parser.handleResovleTask(value);

    parser.close();

    return value;
}
 
Example 8
Source File: CharacterCodec.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
    Object value = parser.parse();
    return value == null //
        ? null //
        : (T) TypeUtils.castToChar(value);
}
 
Example 9
Source File: LongCodec.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@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 10
Source File: BigIntegerCodec.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@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 11
Source File: JavaObjectDeserializer.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) {
    if (type instanceof GenericArrayType) {
        Type componentType = ((GenericArrayType) type).getGenericComponentType();
        if (componentType instanceof TypeVariable) {
            TypeVariable<?> componentVar = (TypeVariable<?>) componentType;
            componentType = componentVar.getBounds()[0];
        }

        List<Object> list = new ArrayList<Object>();
        parser.parseArray(componentType, list);
        Class<?> componentClass;
        if (componentType instanceof Class) {
            componentClass = (Class<?>) componentType;
            Object[] array = (Object[]) Array.newInstance(componentClass, list.size());
            list.toArray(array);
            return (T) array;
        } else {
            return (T) list.toArray();
        }

    }
    
    if (type instanceof Class
            && type != Object.class
            && type != Serializable.class
            && type != Cloneable.class
            && type != Closeable.class
            && type != Comparable.class) {
        return (T) parser.parseObject(type);    
    }

    return (T) parser.parse(fieldName);
}
 
Example 12
Source File: AbstractResponsePayloadCodec.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 解析应答
 *
 * @param parser   解析器
 * @param lexer    文法
 * @param typeName 名称
 * @return 应答对象
 */
protected Object parseResponse(final DefaultJSONParser parser, final JSONLexer lexer, final String typeName) {
    if (typeName == null || typeName.isEmpty()) {
        return null;
    }
    try {
        return parseObject(parser, lexer, getType(typeName));
    } catch (ClassNotFoundException e) {
        //泛化调用情况下,类可能不存在
        //TODO 需要判断是泛化调用
        return parser.parse();
    }
}
 
Example 13
Source File: BooleanCodec.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;

    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 14
Source File: JSON.java    From uavstack with Apache License 2.0 4 votes vote down vote up
public static Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) {
    charsetDecoder.reset();

    int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte());
    char[] chars = allocateChars(scaleLength);

    ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len);
    CharBuffer charBuf = CharBuffer.wrap(chars);
    IOUtils.decode(charsetDecoder, byteBuf, charBuf);

    int position = charBuf.position();

    DefaultJSONParser parser = new DefaultJSONParser(chars, position, ParserConfig.getGlobalInstance(), features);
    Object value = parser.parse();

    parser.handleResovleTask(value);

    parser.close();

    return value;
}
 
Example 15
Source File: JSONPath_s.java    From coming with MIT License 4 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
            JSONLexerBase lexer = (JSONLexerBase) parser.lexer;

            JSONArray array;
            if (context.object == null) {
                context.object = array = new JSONArray();
            } else {
                array = (JSONArray) context.object;
            }
            for (int i = array.size(); i < propertyNamesHash.length; ++i) {
                array.add(null);
            }

//            if (lexer.token() == JSONToken.LBRACKET) {
//                lexer.nextToken();
//                JSONArray array;
//
//                array = new JSONArray();
//                for (;;) {
//                    if (lexer.token() == JSONToken.LBRACE) {
//                        int index = lexer.seekObjectToField(propertyNamesHash);
//                        int matchStat = lexer.matchStat;
//                        if (matchStat == JSONLexer.VALUE) {
//                            Object value;
//                            switch (lexer.token()) {
//                                case JSONToken.LITERAL_INT:
//                                    value = lexer.integerValue();
//                                    lexer.nextToken();
//                                    break;
//                                case JSONToken.LITERAL_STRING:
//                                    value = lexer.stringVal();
//                                    lexer.nextToken();
//                                    break;
//                                default:
//                                    value = parser.parse();
//                                    break;
//                            }
//
//                            array.add(index, value);
//                            if (lexer.token() == JSONToken.RBRACE) {
//                                lexer.nextToken();
//                                continue;
//                            } else {
//                                lexer.skipObject();
//                            }
//                        } else {
//                            lexer.skipObject();
//                        }
//                    }
//
//                    if (lexer.token() == JSONToken.RBRACKET) {
//                        break;
//                    } else if (lexer.token() == JSONToken.COMMA) {
//                        lexer.nextToken();
//                        continue;
//                    } else {
//                        throw new JSONException("illegal json.");
//                    }
//                }
//
//                context.object = array;
//                return;
//            }

            for_:
            for (;;) {
                int index = lexer.seekObjectToField(propertyNamesHash);
                int matchStat = lexer.matchStat;
                if (matchStat == JSONLexer.VALUE) {
                    Object value;
                    switch (lexer.token()) {
                        case JSONToken.LITERAL_INT:
                            value = lexer.integerValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_FLOAT:
                            value = lexer.decimalValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_STRING:
                            value = lexer.stringVal();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        default:
                            value = parser.parse();
                            break;
                    }

                    array.set(index, value);

                    if (lexer.token() == JSONToken.COMMA) {
                        continue for_;
                    }
                }

                break;
            }
        }
 
Example 16
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 17
Source File: TimeDeserializer.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) {
    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 18
Source File: JSONPath_t.java    From coming with MIT License 4 votes vote down vote up
public void extract(JSONPath path, DefaultJSONParser parser, Context context) {
            JSONLexerBase lexer = (JSONLexerBase) parser.lexer;

            JSONArray array;
            if (context.object == null) {
                context.object = array = new JSONArray();
            } else {
                array = (JSONArray) context.object;
            }
            for (int i = array.size(); i < propertyNamesHash.length; ++i) {
                array.add(null);
            }

//            if (lexer.token() == JSONToken.LBRACKET) {
//                lexer.nextToken();
//                JSONArray array;
//
//                array = new JSONArray();
//                for (;;) {
//                    if (lexer.token() == JSONToken.LBRACE) {
//                        int index = lexer.seekObjectToField(propertyNamesHash);
//                        int matchStat = lexer.matchStat;
//                        if (matchStat == JSONLexer.VALUE) {
//                            Object value;
//                            switch (lexer.token()) {
//                                case JSONToken.LITERAL_INT:
//                                    value = lexer.integerValue();
//                                    lexer.nextToken();
//                                    break;
//                                case JSONToken.LITERAL_STRING:
//                                    value = lexer.stringVal();
//                                    lexer.nextToken();
//                                    break;
//                                default:
//                                    value = parser.parse();
//                                    break;
//                            }
//
//                            array.add(index, value);
//                            if (lexer.token() == JSONToken.RBRACE) {
//                                lexer.nextToken();
//                                continue;
//                            } else {
//                                lexer.skipObject();
//                            }
//                        } else {
//                            lexer.skipObject();
//                        }
//                    }
//
//                    if (lexer.token() == JSONToken.RBRACKET) {
//                        break;
//                    } else if (lexer.token() == JSONToken.COMMA) {
//                        lexer.nextToken();
//                        continue;
//                    } else {
//                        throw new JSONException("illegal json.");
//                    }
//                }
//
//                context.object = array;
//                return;
//            }

            for_:
            for (;;) {
                int index = lexer.seekObjectToField(propertyNamesHash);
                int matchStat = lexer.matchStat;
                if (matchStat == JSONLexer.VALUE) {
                    Object value;
                    switch (lexer.token()) {
                        case JSONToken.LITERAL_INT:
                            value = lexer.integerValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_FLOAT:
                            value = lexer.decimalValue();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        case JSONToken.LITERAL_STRING:
                            value = lexer.stringVal();
                            lexer.nextToken(JSONToken.COMMA);
                            break;
                        default:
                            value = parser.parse();
                            break;
                    }

                    array.set(index, value);

                    if (lexer.token() == JSONToken.COMMA) {
                        continue for_;
                    }
                }

                break;
            }
        }