external.com.alibaba.fastjson.JSON Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sekiro-api Show documentation
Show all versions of sekiro-api Show documentation
ratel api,used for developer on ratel system,an extension for xposed framewrok,ratel api compatable with original xposed framework
/*
* Copyright 1999-2101 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package external.com.alibaba.fastjson;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.Array;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.util.*;
import external.com.alibaba.fastjson.parser.DefaultJSONParser;
import external.com.alibaba.fastjson.parser.Feature;
import external.com.alibaba.fastjson.parser.JSONLexer;
import external.com.alibaba.fastjson.parser.JSONToken;
import external.com.alibaba.fastjson.parser.ParserConfig;
import external.com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
import external.com.alibaba.fastjson.parser.deserializer.ExtraTypeProvider;
import external.com.alibaba.fastjson.parser.deserializer.FieldTypeResolver;
import external.com.alibaba.fastjson.parser.deserializer.ParseProcess;
import external.com.alibaba.fastjson.serializer.AfterFilter;
import external.com.alibaba.fastjson.serializer.BeforeFilter;
import external.com.alibaba.fastjson.serializer.JSONSerializer;
import external.com.alibaba.fastjson.serializer.JavaBeanSerializer;
import external.com.alibaba.fastjson.serializer.NameFilter;
import external.com.alibaba.fastjson.serializer.ObjectSerializer;
import external.com.alibaba.fastjson.serializer.PropertyFilter;
import external.com.alibaba.fastjson.serializer.PropertyPreFilter;
import external.com.alibaba.fastjson.serializer.SerializeConfig;
import external.com.alibaba.fastjson.serializer.SerializeFilter;
import external.com.alibaba.fastjson.serializer.SerializeWriter;
import external.com.alibaba.fastjson.serializer.SerializerFeature;
import external.com.alibaba.fastjson.serializer.ValueFilter;
import external.com.alibaba.fastjson.util.TypeUtils;
/**
* @author wenshao[[email protected]]
*/
public abstract class JSON implements JSONStreamAware, JSONAware {
public static TimeZone defaultTimeZone = TimeZone.getDefault();
public static Locale defaultLocale = Locale.getDefault();
public final static String DEFAULT_TYPE_KEY = "@type";
public static int DEFAULT_PARSER_FEATURE;
static {
int features = 0;
// features |= Feature.AutoCloseSource.mask; always allow
features |= Feature.UseBigDecimal.mask;
// features |= Feature.AllowArbitraryCommas.mask; always allow
features |= Feature.SortFeidFastMatch.mask;
features |= Feature.IgnoreNotMatch.mask;
DEFAULT_PARSER_FEATURE = features;
}
public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static int DEFAULT_GENERATE_FEATURE;
static {
int features = 0;
features |= SerializerFeature.QuoteFieldNames.mask;
features |= SerializerFeature.SkipTransientField.mask;
features |= SerializerFeature.WriteEnumUsingToString.mask;
features |= SerializerFeature.SortField.mask;
// features |=
// external.com.alibaba.fastjson.serializer.SerializerFeature.WriteSlashAsSpecial.getMask();
DEFAULT_GENERATE_FEATURE = features;
}
public static final Object parse(String text) {
return parse(text, DEFAULT_PARSER_FEATURE);
}
public static final Object parse(String text, int features) {
if (text == null) {
return null;
}
DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.global, features);
Object value = parser.parse(null);
parser.handleResovleTask(value);
parser.close();
return value;
}
public static final Object parse(byte[] input, Feature... features) {
try {
return parseObject(new String(input, "UTF-8"), features);
} catch (UnsupportedEncodingException e) {
throw new JSONException("UTF-8 not support", e);
}
}
public static final Object parse(String text, Feature... features) {
int featureValues = DEFAULT_PARSER_FEATURE;
for (Feature feature : features) {
featureValues |= feature.mask;
}
return parse(text, featureValues);
}
public static final JSONObject parseObject(String text, Feature... features) {
Object obj = parse(text, features);
if (obj instanceof JSONObject) {
return (JSONObject) obj;
}
JSONObject jsonObject = (JSONObject) JSON.toJSON(obj);
boolean autoTypeSupport = (JSON.DEFAULT_PARSER_FEATURE & Feature.SupportAutoType.mask) != 0;
if (!autoTypeSupport) {
for (Feature feature : features) {
if (feature == Feature.SupportAutoType) {
autoTypeSupport = true;
}
}
}
if (autoTypeSupport) {
jsonObject.put(JSON.DEFAULT_TYPE_KEY, obj.getClass().getName());
}
return jsonObject;
}
public static final JSONObject parseObject(String text) {
Object obj = parse(text);
if (obj instanceof JSONObject || obj == null) {
return (JSONObject) obj;
}
JSONObject jsonObject = (JSONObject) JSON.toJSON(obj);
boolean autoTypeSupport = (JSON.DEFAULT_PARSER_FEATURE & Feature.SupportAutoType.mask) != 0;
if (autoTypeSupport) {
jsonObject.put(JSON.DEFAULT_TYPE_KEY, obj.getClass().getName());
}
return jsonObject;
}
/**
* @since 1.1.71.android
*/
public static T parseObject(String input, Type clazz, ParserConfig config, Feature... features) {
return parseObject(input, clazz, config, null, DEFAULT_PARSER_FEATURE, features);
}
@SuppressWarnings("unchecked")
public static final T parseObject(String text, TypeReference type, Feature... features) {
return (T) parseObject(text, type.type, ParserConfig.global, DEFAULT_PARSER_FEATURE, features);
}
@SuppressWarnings("unchecked")
public static final T parseObject(String text, Class clazz, Feature... features) {
return (T) parseObject(text, (Type) clazz, ParserConfig.global, DEFAULT_PARSER_FEATURE, features);
}
@SuppressWarnings("unchecked")
public static final T parseObject(String text, Class clazz, ParseProcess processor, Feature... features) {
return (T) parseObject(text, (Type) clazz, ParserConfig.global, processor, DEFAULT_PARSER_FEATURE,
features);
}
@SuppressWarnings("unchecked")
public static final T parseObject(String input, Type clazz, Feature... features) {
return (T) parseObject(input, clazz, ParserConfig.global, DEFAULT_PARSER_FEATURE, features);
}
@SuppressWarnings("unchecked")
public static final T parseObject(String input, Type clazz, ParseProcess processor, Feature... features) {
return (T) parseObject(input, clazz, ParserConfig.global, processor, DEFAULT_PARSER_FEATURE, features);
}
@SuppressWarnings("unchecked")
public static final T parseObject(String input, Type clazz, int featureValues, Feature... features) {
if (input == null) {
return null;
}
for (Feature feature : features) {
featureValues |= feature.mask;
}
DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.global, featureValues);
T value = (T) parser.parseObject(clazz);
parser.handleResovleTask(value);
parser.close();
return (T) value;
}
public static final T parseObject(String input, Type clazz, ParserConfig config, int featureValues,
Feature... features) {
return parseObject(input, clazz, config, null, featureValues, features);
}
@SuppressWarnings("unchecked")
public static final T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor,
int featureValues, Feature... features) {
if (input == null) {
return null;
}
for (Feature feature : features) {
featureValues |= feature.mask;
}
DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);
if (processor instanceof ExtraTypeProvider) {
parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
}
if (processor instanceof ExtraProcessor) {
parser.getExtraProcessors().add((ExtraProcessor) processor);
}
if (processor instanceof FieldTypeResolver) {
parser.fieldTypeResolver = (FieldTypeResolver) processor;
}
T value = (T) parser.parseObject(clazz);
parser.handleResovleTask(value);
parser.close();
return (T) value;
}
@SuppressWarnings("unchecked")
public static final T parseObject(byte[] input, Type clazz, Feature... features) {
try {
return (T) parseObject(new String(input, "UTF-8"), clazz, features);
} catch (UnsupportedEncodingException e) {
throw new JSONException("UTF-8 not support");
}
}
@SuppressWarnings("unchecked")
public static final 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.mask;
}
DefaultJSONParser parser = new DefaultJSONParser(input, length, ParserConfig.global, featureValues);
T value = (T) parser.parseObject(clazz);
parser.handleResovleTask(value);
parser.close();
return (T) value;
}
public static final T parseObject(String text, Class clazz) {
return parseObject(text, clazz, new Feature[0]);
}
public static final JSONArray parseArray(String text) {
return parseArray(text, new Feature[0]);
}
public static final JSONArray parseArray(String text, Feature... features) {
if (text == null) {
return null;
}
int featuresValue = JSON.DEFAULT_PARSER_FEATURE;
for (int i = 0; i < features.length; i++) {
featuresValue |= features[i].mask;
}
DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.global, featuresValue);
JSONArray array;
JSONLexer lexer = parser.lexer;
int token = lexer.token();
if (token == JSONToken.NULL) {
lexer.nextToken();
array = null;
} else if (token == JSONToken.EOF) {
array = null;
} else {
array = new JSONArray();
parser.parseArray(array, null);
parser.handleResovleTask(array);
}
parser.close();
return array;
}
public static final List parseArray(String text, Class clazz) {
if (text == null) {
return null;
}
List list;
DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.global);
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();
parser.parseArray(clazz, list);
parser.handleResovleTask(list);
}
parser.close();
return list;
}
public static final List