public DefaultObjectDeserializer(){
}
public Object parseMap(DefaultJSONParser parser, Map<Object, Object> map, Type keyType, Type valueType,
Object fieldName) {
JSONScanner lexer = (JSONScanner) parser.getLexer();
if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) {
throw new JSONException("syntax error, expect {, actual " + lexer.tokenName());
}
ObjectDeserializer keyDeserializer = parser.getConfig().getDeserializer(keyType);
ObjectDeserializer valueDeserializer = parser.getConfig().getDeserializer(valueType);
lexer.nextToken(keyDeserializer.getFastMatchToken());
ParseContext context = parser.getContext();
try {
for (;;) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
if (lexer.token() == JSONToken.LITERAL_STRING && lexer.isRef()) {
Object object = null;
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, map, fieldName);
// parser.setContext(context);
return object;
}
if (map.size() == 0 //
&& lexer.token() == JSONToken.LITERAL_STRING //
&& JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal())) {
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
lexer.nextToken(JSONToken.COMMA);
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
return map;
}
lexer.nextToken(keyDeserializer.getFastMatchToken());
}
Object key = keyDeserializer.deserialze(parser, keyType, null);
if (lexer.token() != JSONToken.COLON) {
throw new JSONException("syntax error, expect :, actual " + lexer.token());
}
lexer.nextToken(valueDeserializer.getFastMatchToken());
Object value = valueDeserializer.deserialze(parser, valueType, key);
if (map.size() == 0 && context != null && context.getObject() != map) {
parser.setContext(context, map, fieldName);
}
map.put(key, value);
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(keyDeserializer.getFastMatchToken());
}
}
} finally {
parser.setContext(context);
}