return (T) object;
}
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, Object object) {
final JSONLexer lexer = parser.getLexer(); // xxx
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
ParseContext context = parser.getContext();
if (object != null) {
context = context.getParentContext();
}
ParseContext childContext = null;
try {
Map<String, Object> fieldValues = null;
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
if (object == null) {
object = createInstance(parser, type);
}
return (T) object;
}
if (lexer.token() == JSONToken.LBRACKET && lexer.isEnabled(Feature.SupportArrayToBean)) {
return deserialzeArrayMapping(parser, type, fieldName, object);
}
if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
StringBuffer buf = (new StringBuffer()) //
.append("syntax error, expect {, actual ") //
.append(lexer.tokenName()) //
.append(", pos ") //
.append(lexer.pos()) //
;
if (fieldName instanceof String) {
buf //
.append(", fieldName ") //
.append(fieldName);
}
throw new JSONException(buf.toString());
}
if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
parser.setResolveStatus(DefaultJSONParser.NONE);
}
for (;;) {
String key = lexer.scanSymbol(parser.getSymbolTable());
if (key == null) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
if (lexer.token() == JSONToken.COMMA) {
if (parser.isEnabled(Feature.AllowArbitraryCommas)) {
continue;
}
}
}
if ("$ref" == key) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
if (lexer.token() == JSONToken.LITERAL_STRING) {
String ref = lexer.stringVal();
if ("@".equals(ref)) {
object = context.getObject();
} else if ("..".equals(ref)) {
ParseContext parentContext = context.getParentContext();
if (parentContext.getObject() != null) {
object = parentContext.getObject();
} else {
parser.addResolveTask(new ResolveTask(parentContext, ref));
parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else if ("$".equals(ref)) {
ParseContext rootContext = context;
while (rootContext.getParentContext() != null) {
rootContext = rootContext.getParentContext();
}
if (rootContext.getObject() != null) {
object = rootContext.getObject();
} else {
parser.addResolveTask(new ResolveTask(rootContext, ref));
parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else {
parser.addResolveTask(new ResolveTask(context, ref));
parser.setResolveStatus(DefaultJSONParser.NeedToResolve);
}
} else {
throw new JSONException("illegal ref, " + JSONToken.name(lexer.token()));
}
lexer.nextToken(JSONToken.RBRACE);
if (lexer.token() != JSONToken.RBRACE) {
throw new JSONException("illegal ref");
}
lexer.nextToken(JSONToken.COMMA);
parser.setContext(context, object, fieldName);
return (T) object;
}
if (JSON.DEFAULT_TYPE_KEY == key) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
if (lexer.token() == JSONToken.LITERAL_STRING) {
String typeName = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
if (type instanceof Class && typeName.equals(((Class<?>) type).getName())) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}
continue;
}
Class<?> userType = TypeUtils.loadClass(typeName);
ObjectDeserializer deserizer = parser.getConfig().getDeserializer(userType);
return (T) deserizer.deserialze(parser, userType, fieldName);
} else {
throw new JSONException("syntax error");
}
}
if (object == null && fieldValues == null) {
object = createInstance(parser, type);
if (object == null) {
fieldValues = new HashMap<String, Object>(this.fieldDeserializers.size());
}
childContext = parser.setContext(context, object, fieldName);
}
boolean match = parseField(parser, key, object, type, fieldValues);
if (!match) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}
continue;
}
if (lexer.token() == JSONToken.COMMA) {
continue;
}
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
if (lexer.token() == JSONToken.IDENTIFIER || lexer.token() == JSONToken.ERROR) {
throw new JSONException("syntax error, unexpect token " + JSONToken.name(lexer.token()));
}
}
if (object == null) {
if (fieldValues == null) {