Java Code Examples for com.alibaba.fastjson.parser.ParserConfig#getGlobalInstance()

The following examples show how to use com.alibaba.fastjson.parser.ParserConfig#getGlobalInstance() . 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 vote down vote up
@SuppressWarnings("unchecked")
public static <T> T parseObject(String input, Type clazz, int featureValues, Feature... features) {
    if (input == null) {
        return null;
    }

    for (Feature feature : features) {
        featureValues = Feature.config(featureValues, feature, true);
    }

    DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues);
    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
}
 
Example 2
Source File: JSON.java    From uavstack with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> T parseObject(char[] input, int length, Type clazz, Feature... features) {
    if (input == null || input.length == 0) {
        return null;
    }

    int featureValues = DEFAULT_PARSER_FEATURE;
    for (Feature feature : features) {
        featureValues = Feature.config(featureValues, feature, true);
    }

    DefaultJSONParser parser = new DefaultJSONParser(input, length, ParserConfig.getGlobalInstance(), featureValues);
    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
}
 
Example 3
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 4
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 5
Source File: JSON.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public static List<Object> parseArray(String text, Type[] types) {
    if (text == null) {
        return null;
    }

    List<Object> list;

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    Object[] objectArray = parser.parseArray(types);
    if (objectArray == null) {
        list = null;
    } else {
        list = Arrays.asList(objectArray);
    }

    parser.handleResovleTask(list);

    parser.close();

    return list;
}
 
Example 6
Source File: FastJson.java    From actframework with Apache License 2.0 6 votes vote down vote up
private void handleForDeserializer(final ObjectDeserializer deserializer, Class targetType) {
    ClassNode node = repo.node(targetType.getName());
    if (null == node) {
        warn("Unknown target type: " + targetType.getName());
        return;
    }
    final ParserConfig config = ParserConfig.getGlobalInstance();
    node.visitSubTree(new Lang.Visitor<ClassNode>() {
        @Override
        public void visit(ClassNode classNode) throws Lang.Break {
            Class type = app.classForName(classNode.name());
            config.putDeserializer(type, deserializer);
        }
    });
    config.putDeserializer(targetType, deserializer);
}
 
Example 7
Source File: JSONArray.java    From uavstack with Apache License 2.0 5 votes vote down vote up
/**
 * @since  1.2.23
 */
public <T> List<T> toJavaList(Class<T> clazz) {
    List<T> list = new ArrayList<T>(this.size());

    ParserConfig config = ParserConfig.getGlobalInstance();

    for (Object item : this) {
        T classItem = (T) TypeUtils.cast(item, clazz, config);
        list.add(classItem);
    }

    return list;
}
 
Example 8
Source File: FastJsonKvCodecTest.java    From actframework with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void prepare() {
    SerializeConfig config = SerializeConfig.getGlobalInstance();
    config.put(KV.class, FastJsonKvCodec.INSTANCE);
    config.put(KVStore.class, FastJsonKvCodec.INSTANCE);

    ParserConfig parserConfig = ParserConfig.getGlobalInstance();
    parserConfig.putDeserializer(KV.class, FastJsonKvCodec.INSTANCE);
    parserConfig.putDeserializer(KVStore.class, FastJsonKvCodec.INSTANCE);

    JSON.DEFAULT_PARSER_FEATURE = Feature.config(JSON.DEFAULT_PARSER_FEATURE, Feature.UseBigDecimal, false);
}
 
Example 9
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 10
Source File: JSONPath_s.java    From coming with MIT License 4 votes vote down vote up
public JSONPath(String path){
    this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance());
}
 
Example 11
Source File: JSONPath_t.java    From coming with MIT License 4 votes vote down vote up
public JSONPath(String path){
    this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance());
}
 
Example 12
Source File: JsonUtilConfig.java    From actframework with Apache License 2.0 4 votes vote down vote up
public static void configure(final App app) {
    SerializeConfig config = SerializeConfig.getGlobalInstance();

    // patch https://github.com/alibaba/fastjson/issues/478
    config.put(FastJsonIterable.class, FastJsonIterableSerializer.instance);

    FastJsonJodaDateCodec jodaDateCodec = new FastJsonJodaDateCodec(app);
    app.registerSingleton(FastJsonJodaDateCodec.class, jodaDateCodec);

    FastJsonValueObjectSerializer valueObjectSerializer = new FastJsonValueObjectSerializer();
    app.registerSingleton(FastJsonValueObjectSerializer.class, valueObjectSerializer);
    FastJsonKeywordCodec keywordCodec = new FastJsonKeywordCodec();
    FastJsonSObjectCodec sObjectCodec = new FastJsonSObjectCodec();

    config.put(DateTime.class, jodaDateCodec);
    config.put(LocalDate.class, jodaDateCodec);
    config.put(LocalTime.class, jodaDateCodec);
    config.put(LocalDateTime.class, jodaDateCodec);
    config.put(ValueObject.class, valueObjectSerializer);
    config.put(Keyword.class, keywordCodec);
    config.put(KV.class, FastJsonKvCodec.INSTANCE);
    config.put(KVStore.class, FastJsonKvCodec.INSTANCE);

    final ParserConfig parserConfig = ParserConfig.getGlobalInstance();
    parserConfig.putDeserializer(DateTime.class, jodaDateCodec);
    parserConfig.putDeserializer(LocalDate.class, jodaDateCodec);
    parserConfig.putDeserializer(LocalTime.class, jodaDateCodec);
    parserConfig.putDeserializer(LocalDateTime.class, jodaDateCodec);
    parserConfig.putDeserializer(Keyword.class, keywordCodec);
    parserConfig.putDeserializer(KV.class, FastJsonKvCodec.INSTANCE);
    parserConfig.putDeserializer(KVStore.class, FastJsonKvCodec.INSTANCE);
    parserConfig.putDeserializer(ISObject.class, sObjectCodec);
    parserConfig.putDeserializer(SObject.class, sObjectCodec);

    MvcConfig.jsonSerializer(new $.Func2<Writer, Object, Void>() {
        @Override
        public Void apply(Writer writer, Object v) throws NotAppliedException, $.Break {
            ActContext ctx = ActContext.Base.currentContext();
            new JsonWriter(v, null, false, ctx).apply(writer);
            return null;
        }
    });

    app.eventBus().bind(CLASS_LOADER_INITIALIZED, new SysEventListenerBase<AppClassLoaderInitialized>() {
        @Override
        public void on(AppClassLoaderInitialized event) {
            parserConfig.setDefaultClassLoader(app.classLoader());
            TypeUtils.clearClassMapping();
        }
    });
}