下面列出了com.alibaba.fastjson.parser.ParserConfig#getGlobalInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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;
}
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;
}
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;
}
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;
}
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);
}
/**
* @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;
}
@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);
}
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;
}
public JSONPath(String path){
this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance());
}
public JSONPath(String path){
this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance());
}
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();
}
});
}