@SuppressWarnings("rawtypes") @Override public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) { JSONLexer lexer = parser.lexer; final int token = lexer.token(); if (token == JSONToken.NULL || (token == JSONToken.LITERAL_STRING && lexer.stringVal().length() == 0)) { setValue(object, null); return; } ArrayList list = new ArrayList(); ParseContext context = parser.getContext(); parser.setContext(context, object, fieldInfo.name); parseArray(parser, objectType, list); parser.setContext(context); if (object == null) { fieldValues.put(fieldInfo.name, list); } else { setValue(object, list); } }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { ParseContext cxt = parser.getContext(); Object object = parser.parse(fieldName); if (object == null) { return null; } String moneyCentStr = null; if (object instanceof JSONObject) {//历史数据兼容 JSONObject jsonObject = (JSONObject) object; moneyCentStr = jsonObject.getString("cent"); } else if (object instanceof String) { moneyCentStr = (String) object; } else { throw new RuntimeException("money属性反序列化失败,不支持的类型:" + object.getClass().getName()); } if (moneyCentStr.length() != 0) { Money m = new Money(); m.cent = Long.valueOf(moneyCentStr); return (T) m; } return null; }
public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) { if (parser.getLexer().token() == 8) { setValue(object, null); return; } ArrayList list = new ArrayList(); ParseContext context = parser.getContext(); parser.setContext(context, object, this.fieldInfo.getName()); parseArray(parser, objectType, list); parser.setContext(context); if (object == null) { fieldValues.put(this.fieldInfo.getName(), list); } else { setValue(object, (Object) list); } }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { final JSONLexer lexer = parser.getLexer(); if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); return null; } Map<Object, Object> map = createMap(type); ParseContext context = parser.getContext(); try { parser.setContext(context, map, fieldName); return (T) deserialze(parser, type, fieldName, map); } finally { parser.setContext(context); } }
@Override public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) { if (fieldValueDeserilizer == null) { fieldValueDeserilizer = parser.getConfig().getDeserializer(fieldInfo); } if (objectType instanceof ParameterizedType) { ParseContext objContext = parser.getContext(); objContext.setType(objectType); } Object value = fieldValueDeserilizer.deserialze(parser, getFieldType(), fieldInfo.getName()); if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) { ResolveTask task = parser.getLastResolveTask(); task.setFieldDeserializer(this); task.setOwnerContext(parser.getContext()); parser.setResolveStatus(DefaultJSONParser.NONE); } else { if (object == null) { fieldValues.put(fieldInfo.getName(), value); } else { setValue(object, value); } } }
@SuppressWarnings("rawtypes") @Override public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) { if (parser.getLexer().token() == JSONToken.NULL) { setValue(object, null); return; } ArrayList list = new ArrayList(); ParseContext context = parser.getContext(); parser.setContext(context, object, fieldInfo.getName()); parseArray(parser, objectType, list); parser.setContext(context); if (object == null) { fieldValues.put(fieldInfo.getName(), list); } else { setValue(object, list); } }
private void _setContext(Context context, MethodVisitor mw) { mw.visitVarInsn(ALOAD, 1); // parser mw.visitVarInsn(ALOAD, context.var("context")); mw.visitMethodInsn(INVOKEVIRTUAL, getType(DefaultJSONParser.class), "setContext", "(" + ASMUtils.getDesc(ParseContext.class) + ")V"); Label endIf_ = new Label(); mw.visitVarInsn(ALOAD, context.var("childContext")); mw.visitJumpInsn(IFNULL, endIf_); mw.visitVarInsn(ALOAD, context.var("childContext")); mw.visitVarInsn(ALOAD, context.var("instance")); mw.visitMethodInsn(INVOKEVIRTUAL, getType(ParseContext.class), "setObject", "(Ljava/lang/Object;)V"); mw.visitLabel(endIf_); }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { final JSONScanner lexer = parser.getLexer(); if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); return null; } Map<Object, Object> map = createMap(type); ParseContext context = parser.getContext(); try { parser.setContext(context, map, fieldName); if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); return (T) map; } return (T) deserialze(parser, type, fieldName, map); } finally { parser.setContext(context); } }
private void _setContext(Context context, MethodVisitor mw) { mw.visitVarInsn(ALOAD, 1); // parser mw.visitVarInsn(ALOAD, context.var("context")); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "setContext", "(" + desc(ParseContext.class) + ")V"); Label endIf_ = new Label(); mw.visitVarInsn(ALOAD, context.var("childContext")); mw.visitJumpInsn(IFNULL, endIf_); mw.visitVarInsn(ALOAD, context.var("childContext")); mw.visitVarInsn(ALOAD, context.var("instance")); mw.visitFieldInsn(PUTFIELD, type(ParseContext.class), "object", "Ljava/lang/Object;"); mw.visitLabel(endIf_); }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONLexer lexer = parser.lexer; if (lexer.token() == JSONToken.NULL) { lexer.nextToken(JSONToken.COMMA); return null; } if (lexer.token() != JSONToken.LBRACE && lexer.token() != JSONToken.COMMA) { throw new JSONException("syntax error"); } lexer.nextToken(); T obj; if (type == Point.class) { obj = (T) parsePoint(parser, fieldName); } else if (type == Rectangle.class) { obj = (T) parseRectangle(parser); } else if (type == Color.class) { obj = (T) parseColor(parser); } else if (type == Font.class) { obj = (T) parseFont(parser); } else { throw new JSONException("not support awt class : " + type); } ParseContext context = parser.getContext(); parser.setContext(obj, fieldName); parser.setContext(context); return obj; }
private void _deserialze_obj(Context context, MethodVisitor mw, Label reset_, FieldInfo fieldInfo, Class<?> fieldClass, int i) { Label matched_ = new Label(); Label _end_if = new Label(); mw.visitVarInsn(ALOAD, context.var("lexer")); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_prefix__", "[C"); mw.visitMethodInsn(INVOKEVIRTUAL, JSONLexerBase, "matchField", "([C)Z"); mw.visitJumpInsn(IFNE, matched_); mw.visitInsn(ACONST_NULL); mw.visitVarInsn(ASTORE, context.var(fieldInfo.name + "_asm")); mw.visitJumpInsn(GOTO, _end_if); mw.visitLabel(matched_); _setFlag(mw, context, i); // increment matchedCount mw.visitVarInsn(ILOAD, context.var("matchedCount")); mw.visitInsn(ICONST_1); mw.visitInsn(IADD); mw.visitVarInsn(ISTORE, context.var("matchedCount")); _deserObject(context, mw, fieldInfo, fieldClass, i); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getResolveStatus", "()I"); mw.visitLdcInsn(com.alibaba.fastjson.parser.DefaultJSONParser.NeedToResolve); mw.visitJumpInsn(IF_ICMPNE, _end_if); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getLastResolveTask", "()" + desc(ResolveTask.class)); mw.visitVarInsn(ASTORE, context.var("resolveTask")); mw.visitVarInsn(ALOAD, context.var("resolveTask")); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getContext", "()" + desc(ParseContext.class)); mw.visitFieldInsn(PUTFIELD, type(ResolveTask.class), "ownerContext", desc(ParseContext.class)); mw.visitVarInsn(ALOAD, context.var("resolveTask")); mw.visitVarInsn(ALOAD, 0); mw.visitLdcInsn(fieldInfo.name); mw.visitMethodInsn(INVOKEVIRTUAL, type(JavaBeanDeserializer.class), "getFieldDeserializer", "(Ljava/lang/String;)" + desc(FieldDeserializer.class)); mw.visitFieldInsn(PUTFIELD, type(ResolveTask.class), "fieldDeserializer", desc(FieldDeserializer.class)); mw.visitVarInsn(ALOAD, 1); mw.visitLdcInsn(com.alibaba.fastjson.parser.DefaultJSONParser.NONE); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "setResolveStatus", "(I)V"); mw.visitLabel(_end_if); }
@Override public void parseField(DefaultJSONParser parser, Object object, Type objectType, Map<String, Object> fieldValues) { if (fieldValueDeserilizer == null) { getFieldValueDeserilizer(parser.getConfig()); } Type fieldType = fieldInfo.fieldType; if (objectType instanceof ParameterizedType) { ParseContext objContext = parser.getContext(); objContext.type = objectType; fieldType = FieldInfo.getFieldType(this.clazz, objectType, fieldType); } // ContextObjectDeserializer Object value; if (fieldValueDeserilizer instanceof JavaBeanDeserializer) { JavaBeanDeserializer javaBeanDeser = (JavaBeanDeserializer) fieldValueDeserilizer; value = javaBeanDeser.deserialze(parser, fieldType, fieldInfo.name, fieldInfo.parserFeatures); } else { if (this.fieldInfo.format != null && fieldValueDeserilizer instanceof ContextObjectDeserializer) { value = ((ContextObjectDeserializer) fieldValueDeserilizer) // .deserialze(parser, fieldType, fieldInfo.name, fieldInfo.format, fieldInfo.parserFeatures); } else { value = fieldValueDeserilizer.deserialze(parser, fieldType, fieldInfo.name); } } if (parser.getResolveStatus() == DefaultJSONParser.NeedToResolve) { DefaultJSONParser.ResolveTask task = parser.getLastResolveTask(); task.fieldDeserializer = this; task.ownerContext = parser.getContext(); parser.setResolveStatus(DefaultJSONParser.NONE); } else { if (object == null) { fieldValues.put(fieldInfo.name, value); } else { setValue(object, value); } } }
public void test_toString() throws Exception { Assert.assertEquals("$", new ParseContext(null, new Object(), "id").toString()); }
public void test_getDescs() throws Exception { Assert.assertEquals("Lcom/alibaba/fastjson/parser/ParseContext;", ASMUtils.desc(ParseContext.class)); }
public void test_getType_null() throws Exception { Assert.assertNull(ASMUtils.getMethodType(ParseContext.class, "XX")); }
public static Object parseMap(DefaultJSONParser parser, Map<Object, Object> map, Type keyType, Type valueType, Object fieldName) { JSONLexer lexer = parser.getLexer(); if (lexer.token() == 12 || lexer.token() == 16) { ObjectDeserializer keyDeserializer = parser.getConfig().getDeserializer(keyType); ObjectDeserializer valueDeserializer = parser.getConfig().getDeserializer(valueType); lexer.nextToken(keyDeserializer.getFastMatchToken()); ParseContext context = parser.getContext(); while (lexer.token() != 13) { try { if (lexer.token() == 4 && lexer.isRef()) { Map<Object, Object> object = null; lexer.nextTokenWithColon(4); if (lexer.token() == 4) { String ref = lexer.stringVal(); if ("..".equals(ref)) { object = context.getParentContext().getObject(); } else if ("$".equals(ref)) { ParseContext rootContext = context; while (rootContext.getParentContext() != null) { rootContext = rootContext.getParentContext(); } object = rootContext.getObject(); } else { parser.addResolveTask(new ResolveTask(context, ref)); parser.setResolveStatus(1); } lexer.nextToken(13); if (lexer.token() != 13) { throw new JSONException("illegal ref"); } lexer.nextToken(16); parser.setContext(context); return object; } throw new JSONException("illegal ref, " + JSONToken.name(lexer.token())); } if (map.size() == 0 && lexer.token() == 4 && JSON.DEFAULT_TYPE_KEY.equals(lexer.stringVal())) { lexer.nextTokenWithColon(4); lexer.nextToken(16); if (lexer.token() == 13) { lexer.nextToken(); return map; } lexer.nextToken(keyDeserializer.getFastMatchToken()); } Object key = keyDeserializer.deserialze(parser, keyType, null); if (lexer.token() != 17) { throw new JSONException("syntax error, expect :, actual " + lexer.token()); } lexer.nextToken(valueDeserializer.getFastMatchToken()); map.put(key, valueDeserializer.deserialze(parser, valueType, key)); if (lexer.token() == 16) { lexer.nextToken(keyDeserializer.getFastMatchToken()); } } finally { parser.setContext(context); } } lexer.nextToken(16); parser.setContext(context); return map; } throw new JSONException("syntax error, expect {, actual " + lexer.tokenName()); }
private void _deserialze_obj(Context context, MethodVisitor mw, Label reset_, FieldInfo fieldInfo, Class<?> fieldClass, int i) { Label matched_ = new Label(); Label _end_if = new Label(); mw.visitVarInsn(ALOAD, context.var("lexer")); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.getClassName(), fieldInfo.getName() + "_asm_prefix__", "[C"); mw.visitMethodInsn(INVOKEVIRTUAL, getType(JSONLexerBase.class), "matchField", "([C)Z"); mw.visitJumpInsn(IFNE, matched_); mw.visitInsn(ACONST_NULL); mw.visitVarInsn(ASTORE, context.var(fieldInfo.getName() + "_asm")); mw.visitJumpInsn(GOTO, _end_if); mw.visitLabel(matched_); _setFlag(mw, context, i); // increment matchedCount mw.visitVarInsn(ILOAD, context.var("matchedCount")); mw.visitInsn(ICONST_1); mw.visitInsn(IADD); mw.visitVarInsn(ISTORE, context.var("matchedCount")); _deserObject(context, mw, fieldInfo, fieldClass); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, getType(DefaultJSONParser.class), "getResolveStatus", "()I"); mw.visitFieldInsn(GETSTATIC, getType(DefaultJSONParser.class), "NeedToResolve", "I"); mw.visitJumpInsn(IF_ICMPNE, _end_if); // ResolveTask task = parser.getLastResolveTask(); // task.setFieldDeserializer(this); // task.setOwnerContext(parser.getContext()); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, getType(DefaultJSONParser.class), "getLastResolveTask", "()" + getDesc(ResolveTask.class)); mw.visitVarInsn(ASTORE, context.var("resolveTask")); mw.visitVarInsn(ALOAD, context.var("resolveTask")); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, getType(DefaultJSONParser.class), "getContext", "()" + getDesc(ParseContext.class)); mw.visitMethodInsn(INVOKEVIRTUAL, getType(ResolveTask.class), "setOwnerContext", "(" + getDesc(ParseContext.class) + ")V"); mw.visitVarInsn(ALOAD, context.var("resolveTask")); mw.visitVarInsn(ALOAD, 0); mw.visitLdcInsn(fieldInfo.getName()); mw.visitMethodInsn(INVOKEVIRTUAL, getType(ASMJavaBeanDeserializer.class), "getFieldDeserializer", "(Ljava/lang/String;)" + getDesc(FieldDeserializer.class)); mw.visitMethodInsn(INVOKEVIRTUAL, getType(ResolveTask.class), "setFieldDeserializer", "(" + getDesc(FieldDeserializer.class) + ")V"); mw.visitVarInsn(ALOAD, 1); mw.visitFieldInsn(GETSTATIC, getType(DefaultJSONParser.class), "NONE", "I"); mw.visitMethodInsn(INVOKEVIRTUAL, getType(DefaultJSONParser.class), "setResolveStatus", "(I)V"); mw.visitLabel(_end_if); }