public void parseObject(Object object) { Class<?> clazz = object.getClass(); Map<String, FieldDeserializer> setters = config.getFieldDeserializers(clazz); if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) { throw new JSONException("syntax error, expect {, actual " + lexer.tokenName()); } final Object[] args = new Object[1]; for (; ; ) { // lexer.scanSymbol String key = lexer.scanSymbol(symbolTable); if (key == null) { if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); break; } if (lexer.token() == JSONToken.COMMA) { if (isEnabled(Feature.AllowArbitraryCommas)) { continue; } } } FieldDeserializer fieldDeser = setters.get(key); if (fieldDeser == null) { if (!isEnabled(Feature.IgnoreNotMatch)) { throw new JSONException( "setter not found, class " + clazz.getName() + ", property " + key); } lexer.nextTokenWithColon(); parse(); // skip if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(); return; } continue; } else { Method method = fieldDeser.getMethod(); Class<?> fieldClass = method.getParameterTypes()[0]; Type fieldType = method.getGenericParameterTypes()[0]; if (fieldClass == int.class) { lexer.nextTokenWithColon(JSONToken.LITERAL_INT); args[0] = IntegerDeserializer.instance.deserialze(this, fieldType, null); } else if (fieldClass == String.class) { lexer.nextTokenWithColon(JSONToken.LITERAL_STRING); args[0] = StringDeserializer.deserialze(this); } else if (fieldClass == long.class) { lexer.nextTokenWithColon(JSONToken.LITERAL_INT); args[0] = LongDeserializer.instance.deserialze(this, fieldType, null); } else { ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType); lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken()); args[0] = fieldValueDeserializer.deserialze(this, fieldType, null); } try { method.invoke(object, args); } catch (Exception e) { throw new JSONException("set proprety error, " + method.getName(), e); } } if (lexer.token() == JSONToken.COMMA) { continue; } if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); return; } } }
public Object[] parseArray(Type[] types) { if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); return null; } if (lexer.token() != JSONToken.LBRACKET) { throw new JSONException("syntax error : " + lexer.tokenName()); } Object[] list = new Object[types.length]; if (types.length == 0) { lexer.nextToken(JSONToken.RBRACKET); if (lexer.token() != JSONToken.RBRACKET) { throw new JSONException("syntax error"); } lexer.nextToken(JSONToken.COMMA); return new Object[0]; } lexer.nextToken(JSONToken.LITERAL_INT); for (int i = 0; i < types.length; ++i) { Object value; if (lexer.token() == JSONToken.NULL) { value = null; lexer.nextToken(JSONToken.COMMA); } else { Type type = types[i]; if (type == int.class || type == Integer.class) { if (lexer.token() == JSONToken.LITERAL_INT) { value = Integer.valueOf(lexer.intValue()); lexer.nextToken(JSONToken.COMMA); } else { value = this.parse(); value = TypeUtils.cast(value, type, config); } } else if (type == String.class) { if (lexer.token() == JSONToken.LITERAL_STRING) { value = lexer.stringVal(); lexer.nextToken(JSONToken.COMMA); } else { value = this.parse(); value = TypeUtils.cast(value, type, config); } } else { boolean isArray = false; Class<?> componentType = null; if (i == types.length - 1) { if (type instanceof Class) { Class<?> clazz = (Class<?>) type; isArray = clazz.isArray(); componentType = clazz.getComponentType(); } } // support varArgs if (isArray && lexer.token() != JSONToken.LBRACKET) { List<Object> varList = new ArrayList<Object>(); ObjectDeserializer derializer = config.getDeserializer(componentType); int fastMatch = derializer.getFastMatchToken(); if (lexer.token() != JSONToken.RBRACKET) { for (; ; ) { Object item = derializer.deserialze(this, type, null); varList.add(item); if (lexer.token() == JSONToken.COMMA) { lexer.nextToken(fastMatch); } else if (lexer.token() == JSONToken.RBRACKET) { break; } else { throw new JSONException("syntax error :" + JSONToken.name(lexer.token())); } } } value = TypeUtils.cast(varList, type, config); } else { ObjectDeserializer derializer = config.getDeserializer(type); value = derializer.deserialze(this, type, null); } } } list[i] = value; if (lexer.token() == JSONToken.RBRACKET) { break; } if (lexer.token() != JSONToken.COMMA) { throw new JSONException("syntax error :" + JSONToken.name(lexer.token())); } if (i == types.length - 1) { lexer.nextToken(JSONToken.RBRACKET); } else { lexer.nextToken(JSONToken.LITERAL_INT); } } if (lexer.token() != JSONToken.RBRACKET) { throw new JSONException("syntax error"); } lexer.nextToken(JSONToken.COMMA); return list; }
public Object deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONLexerBase lexer = (JSONLexerBase) parser.getLexer(); if (lexer.isEnabled(Feature.SortFeidFastMatch)) { return super.deserialze(parser, type, fieldName); } if (isSupportArrayToBean(lexer)) { // deserialzeArrayMapping } if (lexer.scanType("Department") == JSONLexerBase.NOT_MATCH) { return super.deserialze(parser, type, fieldName); } ParseContext mark_context = parser.getContext(); int matchedCount = 0; Department instance = new Department(); ParseContext context = parser.getContext(); ParseContext childContext = parser.setContext(context, instance, fieldName); if (lexer.matchStat == JSONLexerBase.END) { return instance; } int matchStat = 0; int _asm_flag_0 = 0; int id_gen = 0; com.alibaba.json.test.codegen.Employee leader_gen = null; java.util.List members_gen = null; String name_gen; if (lexer.isEnabled(Feature.InitStringFieldAsEmpty)) { name_gen = lexer.stringDefaultValue(); _asm_flag_0 |= 8; } else { name_gen = null; } boolean root_gen = false; com.alibaba.json.test.codegen.DepartmentType type_gen = null; boolean endFlag = false, restFlag = false; if ((!endFlag) && (!restFlag)) { id_gen = lexer.scanFieldInt(id_gen_prefix__); if (lexer.matchStat > 0) { _asm_flag_0 |= 1; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { if (lexer.matchField(leader_gen_prefix__)) { _asm_flag_0 |= 2; matchedCount++; if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) { ResolveTask resolveTask = parser.getLastResolveTask(); resolveTask.ownerContext = parser.getContext(); resolveTask.fieldDeserializer = this.getFieldDeserializer("leader"); parser.setResolveStatus(DefaultJSONParser.NONE); } } if (lexer.matchStat > 0) { _asm_flag_0 |= 2; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { if (lexer.matchField(members_gen_prefix__)) { _asm_flag_0 |= 4; if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); } else { if (lexer.token() == JSONToken.LBRACKET) { if (members_gen_list_item_deser__ == null) { members_gen_list_item_deser__ = parser.getConfig().getDeserializer(com.alibaba.json.test.codegen.Employee.class); } final int fastMatchToken = members_gen_list_item_deser__.getFastMatchToken(); lexer.nextToken(fastMatchToken); members_gen = new java.util.ArrayList(); ParseContext listContext = parser.getContext(); parser.setContext(members_gen, "members"); for (int i = 0; ; ++i) { if (lexer.token() == JSONToken.RBRACKET) { break; } com.alibaba.json.test.codegen.Employee itemValue = members_gen_list_item_deser__.deserialze(parser, members_gen_list_item_type__, i); members_gen.add(itemValue); parser.checkListResolve(members_gen); if (lexer.token() == JSONToken.COMMA) { lexer.nextToken(fastMatchToken); } } parser.setContext(listContext); if (lexer.token() != JSONToken.RBRACKET) { restFlag = true; } lexer.nextToken(JSONToken.COMMA); } else { restFlag = true; } } } if (lexer.matchStat > 0) { _asm_flag_0 |= 4; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { name_gen = lexer.scanFieldString(name_gen_prefix__); if (lexer.matchStat > 0) { _asm_flag_0 |= 8; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { root_gen = lexer.scanFieldBoolean(root_gen_prefix__); if (lexer.matchStat > 0) { _asm_flag_0 |= 16; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat == JSONLexerBase.END) { endFlag = true; } } if ((!endFlag) && (!restFlag)) { String type_gen_enum_name = lexer.scanFieldSymbol(type_gen_prefix__, parser.getSymbolTable()); if (type_gen_enum_name == null) { type_gen = com.alibaba.json.test.codegen.DepartmentType.valueOf(type_gen_enum_name); } if (lexer.matchStat > 0) { _asm_flag_0 |= 32; matchedCount++; } if (lexer.matchStat == JSONLexerBase.NOT_MATCH) { restFlag = true; } if (lexer.matchStat != JSONLexerBase.END) { restFlag = true; } } if (restFlag) { return super.parseRest(parser, type, fieldName, instance); } return instance; }
@SuppressWarnings({"unchecked", "rawtypes"}) public void parseArray(Type type, Collection array, Object fieldName) { if (lexer.token() == JSONToken.SET || lexer.token() == JSONToken.TREE_SET) { lexer.nextToken(); } if (lexer.token() != JSONToken.LBRACKET) { throw new JSONException("exepct '[', but " + JSONToken.name(lexer.token())); } ObjectDeserializer deserializer = null; if (int.class == type) { deserializer = IntegerDeserializer.instance; lexer.nextToken(JSONToken.LITERAL_INT); } else if (String.class == type) { deserializer = StringDeserializer.instance; lexer.nextToken(JSONToken.LITERAL_STRING); } else { deserializer = config.getDeserializer(type); lexer.nextToken(deserializer.getFastMatchToken()); } ParseContext context = this.getContext(); this.setContext(array, fieldName); try { for (int i = 0; ; ++i) { if (isEnabled(Feature.AllowArbitraryCommas)) { while (lexer.token() == JSONToken.COMMA) { lexer.nextToken(); continue; } } if (lexer.token() == JSONToken.RBRACKET) { break; } if (int.class == type) { Object val = IntegerDeserializer.instance.deserialze(this, null, null); array.add(val); } else if (String.class == type) { String value; if (lexer.token() == JSONToken.LITERAL_STRING) { value = lexer.stringVal(); lexer.nextToken(JSONToken.COMMA); } else { Object obj = this.parse(); if (obj == null) { value = null; } else { value = obj.toString(); } } array.add(value); } else { Object val; if (lexer.token() == JSONToken.NULL) { lexer.nextToken(); val = null; } else { val = deserializer.deserialze(this, type, i); } array.add(val); checkListResolve(array); } if (lexer.token() == JSONToken.COMMA) { lexer.nextToken(deserializer.getFastMatchToken()); continue; } } } finally { this.setContext(context); } lexer.nextToken(JSONToken.COMMA); }
public void parseObject(Object object) { Class<?> clazz = object.getClass(); JavaBeanDeserializer beanDeser = null; ObjectDeserializer deserizer = config.getDeserializer(clazz); if (deserizer instanceof JavaBeanDeserializer) { beanDeser = (JavaBeanDeserializer) deserizer; } if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) { throw new JSONException("syntax error, expect {, actual " + lexer.tokenName()); } for (; ; ) { // lexer.scanSymbol String key = lexer.scanSymbol(symbolTable); if (key == null) { if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); break; } if (lexer.token() == JSONToken.COMMA) { if (lexer.isEnabled(Feature.AllowArbitraryCommas)) { continue; } } } FieldDeserializer fieldDeser = null; if (beanDeser != null) { fieldDeser = beanDeser.getFieldDeserializer(key); } if (fieldDeser == null) { if (!lexer.isEnabled(Feature.IgnoreNotMatch)) { throw new JSONException( "setter not found, class " + clazz.getName() + ", property " + key); } lexer.nextTokenWithColon(); parse(); // skip if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(); return; } continue; } else { Class<?> fieldClass = fieldDeser.fieldInfo.fieldClass; Type fieldType = fieldDeser.fieldInfo.fieldType; Object fieldValue; if (fieldClass == int.class) { lexer.nextTokenWithColon(JSONToken.LITERAL_INT); fieldValue = IntegerCodec.instance.deserialze(this, fieldType, null); } else if (fieldClass == String.class) { lexer.nextTokenWithColon(JSONToken.LITERAL_STRING); fieldValue = StringCodec.deserialze(this); } else if (fieldClass == long.class) { lexer.nextTokenWithColon(JSONToken.LITERAL_INT); fieldValue = LongCodec.instance.deserialze(this, fieldType, null); } else { ObjectDeserializer fieldValueDeserializer = config.getDeserializer(fieldClass, fieldType); lexer.nextTokenWithColon(fieldValueDeserializer.getFastMatchToken()); fieldValue = fieldValueDeserializer.deserialze(this, fieldType, null); } fieldDeser.setValue(object, fieldValue); } if (lexer.token() == JSONToken.COMMA) { continue; } if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); return; } } }