use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.
the class DefaultJSONParser method parseObject.
@SuppressWarnings({ "unchecked", "rawtypes" })
public final Object parseObject(final Map object, Object fieldName) {
final JSONLexer lexer = this.lexer;
if (lexer.token() == JSONToken.NULL) {
lexer.next();
return null;
}
if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
}
ParseContext context = this.getContext();
try {
boolean setContextFlag = false;
for (; ; ) {
lexer.skipWhitespace();
char ch = lexer.getCurrent();
if (isEnabled(Feature.AllowArbitraryCommas)) {
while (ch == ',') {
lexer.next();
lexer.skipWhitespace();
ch = lexer.getCurrent();
}
}
boolean isObjectKey = false;
Object key;
if (ch == '"') {
key = lexer.scanSymbol(symbolTable, '"');
lexer.skipWhitespace();
ch = lexer.getCurrent();
if (ch != ':') {
throw new JSONException("expect ':' at " + lexer.pos() + ", name " + key);
}
} else if (ch == '}') {
lexer.next();
lexer.resetStringPosition();
lexer.nextToken();
return object;
} else if (ch == '\'') {
if (!isEnabled(Feature.AllowSingleQuotes)) {
throw new JSONException("syntax error");
}
key = lexer.scanSymbol(symbolTable, '\'');
lexer.skipWhitespace();
ch = lexer.getCurrent();
if (ch != ':') {
throw new JSONException("expect ':' at " + lexer.pos());
}
} else if (ch == EOI) {
throw new JSONException("syntax error");
} else if (ch == ',') {
throw new JSONException("syntax error");
} else if ((ch >= '0' && ch <= '9') || ch == '-') {
lexer.resetStringPosition();
lexer.scanNumber();
if (lexer.token() == JSONToken.LITERAL_INT) {
key = lexer.integerValue();
} else {
key = lexer.decimalValue(true);
}
ch = lexer.getCurrent();
if (ch != ':') {
throw new JSONException("expect ':' at " + lexer.pos() + ", name " + key);
}
} else if (ch == '{' || ch == '[') {
lexer.nextToken();
key = parse();
isObjectKey = true;
} else {
if (!isEnabled(Feature.AllowUnQuotedFieldNames)) {
throw new JSONException("syntax error");
}
key = lexer.scanSymbolUnQuoted(symbolTable);
lexer.skipWhitespace();
ch = lexer.getCurrent();
if (ch != ':') {
throw new JSONException("expect ':' at " + lexer.pos() + ", actual " + ch);
}
}
if (!isObjectKey) {
lexer.next();
lexer.skipWhitespace();
}
ch = lexer.getCurrent();
lexer.resetStringPosition();
if (key == JSON.DEFAULT_TYPE_KEY && !isEnabled(Feature.DisableSpecialKeyDetect)) {
String typeName = lexer.scanSymbol(symbolTable, '"');
Class<?> clazz = TypeUtils.loadClass(typeName);
if (clazz == null) {
object.put(JSON.DEFAULT_TYPE_KEY, typeName);
continue;
}
lexer.nextToken(JSONToken.COMMA);
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
try {
Object instance = null;
ObjectDeserializer deserializer = this.config.getDeserializer(clazz);
if (deserializer instanceof ASMJavaBeanDeserializer) {
instance = ((ASMJavaBeanDeserializer) deserializer).createInstance(this, clazz);
} else if (deserializer instanceof JavaBeanDeserializer) {
instance = ((JavaBeanDeserializer) deserializer).createInstance(this, clazz);
}
if (instance == null) {
if (clazz == Cloneable.class) {
instance = new HashMap();
} else {
instance = clazz.newInstance();
}
}
return instance;
} catch (Exception e) {
throw new JSONException("create instance error", e);
}
}
this.setResolveStatus(TypeNameRedirect);
if (this.context != null && !(fieldName instanceof Integer)) {
this.popContext();
}
ObjectDeserializer deserializer = config.getDeserializer(clazz);
return deserializer.deserialze(this, clazz, fieldName);
}
if (key == "$ref" && !isEnabled(Feature.DisableSpecialKeyDetect)) {
lexer.nextToken(JSONToken.LITERAL_STRING);
if (lexer.token() == JSONToken.LITERAL_STRING) {
String ref = lexer.stringVal();
lexer.nextToken(JSONToken.RBRACE);
Object refValue = null;
if ("@".equals(ref)) {
if (this.getContext() != null) {
ParseContext thisContext = this.getContext();
Object thisObj = thisContext.getObject();
if (thisObj instanceof Object[] || thisObj instanceof Collection<?>) {
refValue = thisObj;
} else if (thisContext.getParentContext() != null) {
refValue = thisContext.getParentContext().getObject();
}
}
} else if ("..".equals(ref)) {
ParseContext parentContext = context.getParentContext();
if (parentContext.getObject() != null) {
refValue = parentContext.getObject();
} else {
addResolveTask(new ResolveTask(parentContext, ref));
setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else if ("$".equals(ref)) {
ParseContext rootContext = context;
while (rootContext.getParentContext() != null) {
rootContext = rootContext.getParentContext();
}
if (rootContext.getObject() != null) {
refValue = rootContext.getObject();
} else {
addResolveTask(new ResolveTask(rootContext, ref));
setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else {
addResolveTask(new ResolveTask(context, ref));
setResolveStatus(DefaultJSONParser.NeedToResolve);
}
if (lexer.token() != JSONToken.RBRACE) {
throw new JSONException("syntax error");
}
lexer.nextToken(JSONToken.COMMA);
return refValue;
} else {
throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
}
}
if (!setContextFlag) {
setContext(object, fieldName);
setContextFlag = true;
}
if (object.getClass() == JSONObject.class) {
key = (key == null) ? "null" : key.toString();
}
Object value;
if (ch == '"') {
lexer.scanString();
String strValue = lexer.stringVal();
value = strValue;
if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
JSONScanner iso8601Lexer = new JSONScanner(strValue);
if (iso8601Lexer.scanISO8601DateIfMatch()) {
value = iso8601Lexer.getCalendar().getTime();
}
iso8601Lexer.close();
}
object.put(key, value);
} else if (ch >= '0' && ch <= '9' || ch == '-') {
lexer.scanNumber();
if (lexer.token() == JSONToken.LITERAL_INT) {
value = lexer.integerValue();
} else {
value = lexer.decimalValue(isEnabled(Feature.UseBigDecimal));
}
object.put(key, value);
} else if (ch == '[') {
// 减少嵌套,兼容android
lexer.nextToken();
JSONArray list = new JSONArray();
this.parseArray(list, key);
value = list;
object.put(key, value);
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
return object;
} else if (lexer.token() == JSONToken.COMMA) {
continue;
} else {
throw new JSONException("syntax error");
}
} else if (ch == '{') {
// 减少嵌套,兼容android
lexer.nextToken();
final boolean parentIsArray = fieldName != null && fieldName.getClass() == Integer.class;
JSONObject input = new JSONObject(isEnabled(Feature.OrderedField));
ParseContext ctxLocal = null;
if (!parentIsArray) {
ctxLocal = setContext(context, input, key);
}
Object obj = this.parseObject(input, key);
if (ctxLocal != null && input != obj) {
ctxLocal.setObject(object);
}
checkMapResolve(object, key.toString());
if (object.getClass() == JSONObject.class) {
object.put(key.toString(), obj);
} else {
object.put(key, obj);
}
if (parentIsArray) {
setContext(context, obj, key);
}
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
setContext(context);
return object;
} else if (lexer.token() == JSONToken.COMMA) {
continue;
} else {
throw new JSONException("syntax error, " + lexer.tokenName());
}
} else {
lexer.nextToken();
value = parse();
if (object.getClass() == JSONObject.class) {
key = key.toString();
}
object.put(key, value);
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
return object;
} else if (lexer.token() == JSONToken.COMMA) {
continue;
} else {
throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
}
}
lexer.skipWhitespace();
ch = lexer.getCurrent();
if (ch == ',') {
lexer.next();
continue;
} else if (ch == '}') {
lexer.next();
lexer.resetStringPosition();
lexer.nextToken();
this.setContext(object, fieldName);
return object;
} else {
throw new JSONException("syntax error, position at " + lexer.pos() + ", name " + key);
}
}
} finally {
this.setContext(context);
}
}
use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.
the class JavaBeanDeserializer method createInstance.
public Object createInstance(DefaultJSONParser parser, Type type) {
if (type instanceof Class) {
if (clazz.isInterface()) {
Class<?> clazz = (Class<?>) type;
ClassLoader loader = Thread.currentThread().getContextClassLoader();
final JSONObject obj = new JSONObject();
Object proxy = Proxy.newProxyInstance(loader, new Class<?>[] { clazz }, obj);
return proxy;
}
}
if (beanInfo.getDefaultConstructor() == null) {
return null;
}
Object object;
try {
Constructor<?> constructor = beanInfo.getDefaultConstructor();
if (constructor.getParameterTypes().length == 0) {
object = constructor.newInstance();
} else {
object = constructor.newInstance(parser.getContext().getObject());
}
} catch (Exception e) {
throw new JSONException("create instance error, class " + clazz.getName(), e);
}
if (parser.isEnabled(Feature.InitStringFieldAsEmpty)) {
for (FieldInfo fieldInfo : beanInfo.getFieldList()) {
if (fieldInfo.getFieldClass() == String.class) {
try {
fieldInfo.set(object, "");
} catch (Exception e) {
throw new JSONException("create instance error, class " + clazz.getName(), e);
}
}
}
}
return object;
}
use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.
the class TypeUtils method castToJavaBean.
@SuppressWarnings({ "unchecked" })
public static final <T> T castToJavaBean(Map<String, Object> map, Class<T> clazz, ParserConfig mapping) {
try {
if (clazz == StackTraceElement.class) {
String declaringClass = (String) map.get("className");
String methodName = (String) map.get("methodName");
String fileName = (String) map.get("fileName");
int lineNumber;
{
Number value = (Number) map.get("lineNumber");
if (value == null) {
lineNumber = 0;
} else {
lineNumber = value.intValue();
}
}
return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
}
{
Object iClassObject = map.get(JSON.DEFAULT_TYPE_KEY);
if (iClassObject instanceof String) {
String className = (String) iClassObject;
Class<?> loadClazz = (Class<T>) loadClass(className);
if (loadClazz == null) {
throw new ClassNotFoundException(className + " not found");
}
if (!loadClazz.equals(clazz)) {
return (T) castToJavaBean(map, loadClazz, mapping);
}
}
}
if (clazz.isInterface()) {
JSONObject object;
if (map instanceof JSONObject) {
object = (JSONObject) map;
} else {
object = new JSONObject(map);
}
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { clazz }, object);
}
if (mapping == null) {
mapping = ParserConfig.getGlobalInstance();
}
Map<String, FieldDeserializer> setters = mapping.getFieldDeserializers(clazz);
Constructor<T> constructor = clazz.getDeclaredConstructor();
if (!constructor.isAccessible()) {
constructor.setAccessible(true);
}
T object = constructor.newInstance();
for (Map.Entry<String, FieldDeserializer> entry : setters.entrySet()) {
String key = entry.getKey();
FieldDeserializer fieldDeser = entry.getValue();
if (map.containsKey(key)) {
Object value = map.get(key);
Method method = fieldDeser.getMethod();
if (method != null) {
Type paramType = method.getGenericParameterTypes()[0];
value = cast(value, paramType, mapping);
method.invoke(object, new Object[] { value });
} else {
Field field = fieldDeser.getField();
Type paramType = field.getGenericType();
value = cast(value, paramType, mapping);
field.set(object, value);
}
}
}
return object;
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}
use of com.alibaba.fastjson.JSONObject in project uavstack by uavorg.
the class JSONHelper method convertJO2POJO.
public static Object convertJO2POJO(Object data) {
Class<?> dCls = data.getClass();
if (JSONObject.class.isAssignableFrom(dCls)) {
Map<String, Object> m = new LinkedHashMap<String, Object>();
JSONObject jod = (JSONObject) data;
for (String key : jod.keySet()) {
Object attr = jod.get(key);
Object attrObj = convertJO2POJO(attr);
m.put(key, attrObj);
}
return m;
} else if (JSONArray.class.isAssignableFrom(dCls)) {
List<Object> l = new ArrayList<Object>();
JSONArray joa = (JSONArray) data;
for (Object o : joa) {
Object attrObj = convertJO2POJO(o);
l.add(attrObj);
}
return l;
}
return data;
}
use of com.alibaba.fastjson.JSONObject in project CorePage by lizhangqu.
the class CorePageManager method buildBundle.
/**
* 根据page,从pageParams中获得bundle
*
* @param corePage 页面
* @return 页面的参数
*/
private Bundle buildBundle(CorePage corePage) {
Bundle bundle = new Bundle();
String key = null;
Object value = null;
if (corePage != null && corePage.getParams() != null) {
JSONObject j = JSON.parseObject(corePage.getParams());
if (j != null) {
Set<String> keySet = j.keySet();
if (keySet != null) {
Iterator<String> ite = keySet.iterator();
while (ite.hasNext()) {
key = ite.next();
value = j.get(key);
bundle.putString(key, value.toString());
}
}
}
}
return bundle;
}
Aggregations