private void _getFieldDeser(Context context, MethodVisitor mw, FieldInfo fieldInfo) { Label notNull_ = new Label(); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_deser__", desc(ObjectDeserializer.class)); mw.visitJumpInsn(IFNONNULL, notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitVarInsn(ALOAD, 1); mw.visitMethodInsn(INVOKEVIRTUAL, DefaultJSONParser, "getConfig", "()" + desc(ParserConfig.class)); mw.visitLdcInsn(com.alibaba.fastjson.asm.Type.getType(desc(fieldInfo.fieldClass))); mw.visitMethodInsn(INVOKEVIRTUAL, type(ParserConfig.class), "getDeserializer", "(Ljava/lang/reflect/Type;)" + desc(ObjectDeserializer.class)); mw.visitFieldInsn(PUTFIELD, context.className, fieldInfo.name + "_asm_deser__", desc(ObjectDeserializer.class)); mw.visitLabel(notNull_); mw.visitVarInsn(ALOAD, 0); mw.visitFieldInsn(GETFIELD, context.className, fieldInfo.name + "_asm_deser__", desc(ObjectDeserializer.class)); }
@SuppressWarnings("unchecked") public static final <T> T parseObject(String input, Type clazz, int featureValues, Feature... features) { if (input == null) { return null; } for (Feature featrue : features) { featureValues = Feature.config(featureValues, featrue, true); } DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues); T value = (T) parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return (T) value; }
public boolean resolveJsonString(Class className) { boolean re = false; try { //先调用不带参数的方法解析HttpJson的内部成员变量 re = resolveJsonString(); //将对于JodaTime的逆序列化支持加载进来 ParserConfig.getGlobalInstance().putDeserializer(LocalDate.class, JodaTimeDeserializer.instance); //将附加传递的classObjectString 先从org.json中解析出来 this.classObjectString = jsonObject.getString("classObjectString"); //确认有类传过来才执行类解析 if (!(classObjectString.equals("") || classObjectString.equals("{}"))) { this.classObject = JSON.parseObject(classObjectString, className); } } catch (Exception e) { e.printStackTrace(); } return re; }
@SuppressWarnings("unchecked") public static <T> T parseObject(String input, Type clazz, int featureValues, Feature... features) { if (input == null) { return null; } for (Feature feature : features) { featureValues = Feature.config(featureValues, feature, true); } DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues); T value = (T) parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return (T) value; }
public static <T> List<T> parseArray(String text, Class<T> clazz) { if (text == null) { return null; } List<T> list; DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance()); JSONLexer lexer = parser.lexer; int token = lexer.token(); if (token == JSONToken.NULL) { lexer.nextToken(); list = null; } else if (token == JSONToken.EOF && lexer.isBlankInput()) { list = null; } else { list = new ArrayList<T>(); parser.parseArray(clazz, list); parser.handleResovleTask(list); } parser.close(); return list; }
public static final List<Object> parseArray(String text, Type[] types) { if (text == null) { return null; } List<Object> list; DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance()); Object[] objectArray = parser.parseArray(types); if (objectArray == null) { list = null; } else { list = Arrays.asList(objectArray); } parser.handleResovleTask(list); parser.close(); return list; }
@SuppressWarnings("unchecked") public static <T> T cast(Object obj, Type type, ParserConfig mapping){ if(obj == null){ return null; } if(type instanceof Class){ return (T) cast(obj, (Class<T>) type, mapping); } if(type instanceof ParameterizedType){ return (T) cast(obj, (ParameterizedType) type, mapping); } if(obj instanceof String){ String strVal = (String) obj; if(strVal.length() == 0 // || "null".equals(strVal) // || "NULL".equals(strVal)){ return null; } } if(type instanceof TypeVariable){ return (T) obj; } throw new JSONException("can not cast to : " + type); }
public void test_abc() throws Exception { Field field = ParserConfig.class.getDeclaredField("denyList"); field.setAccessible(true); String[] denyList = (String[]) field.get(ParserConfig.getGlobalInstance()); Arrays.sort(denyList); for (int i = 0; i < denyList.length; ++i) { if (i != 0) { System.out.print(","); } System.out.print(denyList[i]); } for (int i = 0; i < denyList.length; ++i) { // System.out.println("\"" + denyList[i] + "\","); System.out.println(denyList[i]); } System.out.println(); System.out.println(Base64.encodeToString("\"@type".getBytes(), true)); }
public FieldDeserializer createFieldDeserializer(ParserConfig mapping, // JavaBeanInfo beanInfo, // FieldInfo fieldInfo) { Class<?> clazz = beanInfo.clazz; Class<?> fieldClass = fieldInfo.fieldClass; Class<?> deserializeUsing = null; JSONField annotation = fieldInfo.getAnnotation(); if (annotation != null) { deserializeUsing = annotation.deserializeUsing(); if (deserializeUsing == Void.class) { deserializeUsing = null; } } if (deserializeUsing == null && (fieldClass == List.class || fieldClass == ArrayList.class)) { return new ArrayListTypeFieldDeserializer(mapping, clazz, fieldInfo); } return new DefaultFieldDeserializerBug569(mapping, clazz, fieldInfo); }
public void test_camel() throws Exception { SerializeConfig config = new SerializeConfig(); config.propertyNamingStrategy = PropertyNamingStrategy.CamelCase; Model model = new Model(); model.personId = 1001; String text = JSON.toJSONString(model, config); Assert.assertEquals("{\"personId\":1001}", text); ParserConfig parserConfig = new ParserConfig(); parserConfig.propertyNamingStrategy = PropertyNamingStrategy.CamelCase; Model model2 = JSON.parseObject(text, Model.class, parserConfig); Assert.assertEquals(model.personId, model2.personId); Model model3 = JSON.parseObject(text, Model.class); Assert.assertEquals(model.personId, model3.personId); }
public void test_parseObject() { new DefaultJSONParser("".toCharArray(), 0, ParserConfig.getGlobalInstance(), 0).close(); User user = new User(); user.setName("校长"); user.setAge(3); user.setSalary(new BigDecimal("123456789.0123")); String jsonString = JSON.toJSONString(user); System.out.println(jsonString); JSON.parseObject(jsonString); DefaultJSONParser parser = new DefaultJSONParser(jsonString); User user1 = new User(); parser.parseObject(user1); Assert.assertEquals(user.getAge(), user1.getAge()); Assert.assertEquals(user.getName(), user1.getName()); Assert.assertEquals(user.getSalary(), user1.getSalary()); }
public void test_list() throws Exception { A a = new A(); Set<B> set = new LinkedHashSet<B>(); set.add(new B()); set.add(new B1()); a.setList(set); String text = JSON.toJSONString(a, SerializerFeature.WriteClassName); System.out.println(text); // Assert.assertEquals("{\"@type\":\"com.alibaba.json.bvt.writeClassName.WriteClassNameTest_Set2$A\",\"list\":[{},{\"@type\":\"com.alibaba.json.bvt.writeClassName.WriteClassNameTest_Set2$B1\"}]}", // text); ParserConfig parserConfig = new ParserConfig(); parserConfig.addAccept("com.alibaba.json.bvt"); A a1 = (A) JSON.parseObject(text, Object.class, parserConfig); Assert.assertEquals(2, a1.getList().size()); Assert.assertTrue("B", new ArrayList<B>(a1.getList()).get(0) instanceof B || new ArrayList<B>(a1.getList()).get(0) instanceof B1); Assert.assertTrue("B1", new ArrayList<B>(a1.getList()).get(1) instanceof B || new ArrayList<B>(a1.getList()).get(1) instanceof B1); }
public static final <T> T parseObject(String input, Type clazz, Feature... features) { if (input == null) { return null; } int featureValues = 0; for (Feature feature : features) { featureValues = Feature.config(featureValues, feature, true); } DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues); T value = (T) parser.parseObject(clazz); if (clazz != JSONArray.class) { parser.close(); } return (T) value; }
public void test_0() throws Exception { String text = "{}"; ParserConfig config = new ParserConfig(); Properties properties = new Properties(); properties.put(ParserConfig.DENY_PROPERTY, "com.alibaba.json.bvtVO.deny,,aa"); config.configFromPropety(properties); Exception error = null; try { JSON.parseObject("{\"@type\":\"com.alibaba.json.bvtVO.deny$A\"}", Object.class, config, JSON.DEFAULT_PARSER_FEATURE); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); JSON.parseObject(text, B.class, config, JSON.DEFAULT_PARSER_FEATURE); }
public void test_autoTypeDeny() throws Exception { ParserConfig config = new ParserConfig(); assertFalse(config.isAutoTypeSupport()); config.setAutoTypeSupport(true); assertTrue(config.isAutoTypeSupport()); Properties properties = new Properties(); properties.put(ParserConfig.AUTOTYPE_SUPPORT_PROPERTY, "false"); config.configFromPropety(properties); assertFalse(config.isAutoTypeSupport()); Exception error = null; try { Object obj = JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.deser.deny.DenyTest7$Model\"}", Object.class, config); System.out.println(obj.getClass()); } catch (JSONException ex) { error = ex; } assertNotNull(error); }
public void test_autoTypeDeny() throws Exception { ParserConfig config = new ParserConfig(); assertFalse(config.isAutoTypeSupport()); config.setAutoTypeSupport(true); assertTrue(config.isAutoTypeSupport()); config.addDeny("com.alibaba.json.bvt.parser.deser.deny.DenyTest6"); config.setAutoTypeSupport(false); Exception error = null; try { Object obj = JSON.parseObject("{\"@type\":\"com.alibaba.json.bvt.parser.deser.deny.DenyTest6$Model\"}", Object.class, config); System.out.println(obj.getClass()); } catch (JSONException ex) { error = ex; } assertNotNull(error); }
public void test_pattern() throws Exception { Assert.assertEquals(Pattern.compile("abc").pattern(), JSON.parseObject("'abc'", Pattern.class).pattern()); Assert.assertEquals(null, JSON.parseObject("null", Pattern.class)); DefaultJSONParser parser = new DefaultJSONParser("null", ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); Assert.assertEquals(null, MiscCodec.instance.deserialze(parser, null, null)); Assert.assertEquals(JSONToken.LITERAL_STRING, MiscCodec.instance.getFastMatchToken()); }
public ArrayListTypeFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo){ super(clazz, fieldInfo); Type fieldType = fieldInfo.fieldType; if (fieldType instanceof ParameterizedType) { Type argType = ((ParameterizedType) fieldInfo.fieldType).getActualTypeArguments()[0]; if (argType instanceof WildcardType) { WildcardType wildcardType = (WildcardType) argType; Type[] upperBounds = wildcardType.getUpperBounds(); if (upperBounds.length == 1) { argType = upperBounds[0]; } } this.itemType = argType; } else { this.itemType = Object.class; } }
public static final Object parse(String text, int features) { if (text == null) { return null; } DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance(), features); Object value = parser.parse(); parser.handleResovleTask(value); parser.close(); return value; }
public Context(String className, ParserConfig config, JavaBeanInfo beanInfo, int initVariantIndex){ this.className = className; this.clazz = beanInfo.clazz; this.variantIndex = initVariantIndex; this.beanInfo = beanInfo; fieldInfoList = beanInfo.fields; }
public <T> T getObject(String key, TypeReference typeReference) { Object obj = map.get(key); if (typeReference == null) { return (T) obj; } return TypeUtils.cast(obj, typeReference.getType(), ParserConfig.getGlobalInstance()); }
public void test_7() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("123"); ParserConfig mapping = new ParserConfig(); parser.setConfig(mapping); Assert.assertEquals(mapping, parser.getConfig()); }
public void test_no_asm() throws Exception { ParserConfig mapping = new ParserConfig(); mapping.setAsmEnable(false); Assert.assertEquals(false, mapping.isAsmEnable()); mapping.setAsmEnable(true); Assert.assertEquals(true, mapping.isAsmEnable()); }
protected void setUp() throws Exception { assertFalse(config.isAutoTypeSupport()); Properties properties = new Properties(); properties.put(ParserConfig.AUTOTYPE_SUPPORT_PROPERTY, "false"); config.addAccept("com.alibaba.json.bvt.parser.deser.deny.DenyTest11.Model"); // -ea -Dfastjson.parser.autoTypeAccept=com.alibaba.json.bvt.parser.deser.deny.DenyTest9 config.configFromPropety(properties); assertFalse(config.isAutoTypeSupport()); }
/** * config default type key * @since 1.2.14 */ public static void setDefaultTypeKey(String typeKey) { DEFAULT_TYPE_KEY = typeKey; ParserConfig.global.symbolTable.addSymbol(typeKey, 0, typeKey.length(), typeKey.hashCode(), true); }
public static final Object parse(byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) { charsetDecoder.reset(); char[] chars = ThreadLocalCache.getChars((int) (((double) len) * ((double) charsetDecoder.maxCharsPerByte()))); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charBuf = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charBuf); DefaultJSONParser parser = new DefaultJSONParser(chars, charBuf.position(), ParserConfig.getGlobalInstance(), features); Object value = parser.parse(); parser.handleResovleTask(value); parser.close(); return value; }
/** * * @since 1.2.38 */ public static Object parse(String text, ParserConfig config, int features) { if (text == null) { return null; } DefaultJSONParser parser = new DefaultJSONParser(text, config, features); Object value = parser.parse(); parser.handleResovleTask(value); parser.close(); return value; }
public static void testAutoTypeDeny(String rootPath) throws Exception { ParserConfig config = new ParserConfig(); final String fileSeparator = System.getProperty("file.separator"); final String evilClassPath = rootPath + fileSeparator + "WEB-INF" + fileSeparator + "classes" + fileSeparator + "person" + fileSeparator + "Test.class"; String evilCode = readClass(evilClassPath); final String nastyClass = "com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl"; String text1 = "{\"@type\":\"" + nastyClass + "\",\"_bytecodes\":[\"" + evilCode + "\"],'_name':'a.b','_tfactory':{ },\"_outputProperties\":{ }," + "\"_name\":\"a\",\"_version\":\"1.0\",\"allowedProtocols\":\"all\"}\n"; System.out.println(text1); Object obj = JSON.parseObject(text1, Object.class, config, Feature.SupportNonPublicField); }
public final Object decodeObject(String text) { ParserConfig config = ParserConfig.global; DefaultJSONParser parser = new DefaultJSONParser(text, config); parser.config(Feature.DisableCircularReferenceDetect, true); parser.config(Feature.SupportArrayToBean, true); return parser.parse(); }
public static final <T> T parseObject(char[] input, int length, Type clazz, Feature... features) { if (input == null || input.length == 0) { return null; } int featureValues = DEFAULT_PARSER_FEATURE; for (Feature featrue : features) { featureValues = Feature.config(featureValues, featrue, true); } DefaultJSONParser parser = new DefaultJSONParser(input, length, ParserConfig.getGlobalInstance(), featureValues); T value = parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return value; }
public <T> Collection<T> decodeArray(String text, Class<T> clazz) throws Exception { ParserConfig config = ParserConfig.global; DefaultJSONParser parser = new DefaultJSONParser(text, config); parser.config(Feature.DisableCircularReferenceDetect, true); parser.config(Feature.SupportArrayToBean, true); return parser.parseArray(clazz); }
public JSONPath(String path, SerializeConfig serializeConfig, ParserConfig parserConfig){ if (path == null || path.length() == 0) { throw new JSONPathException("json-path can not be null or empty"); } this.path = path; this.serializeConfig = serializeConfig; this.parserConfig = parserConfig; }
public void test_date() { String text = "{\"date\":\"2011-01-09T13:49:53.254\"}"; char[] chars = text.toCharArray(); DefaultJSONParser parser = new DefaultJSONParser(chars, chars.length, ParserConfig.getGlobalInstance(), 0); parser.config(Feature.AllowISO8601DateFormat, true); JSONObject json = parser.parseObject(); Assert.assertEquals(new Date(1294552193254L), json.get("date")); }
public Context(String className, ParserConfig config, DeserializeBeanInfo beanInfo, int initVariantIndex){ this.className = className; this.clazz = beanInfo.getClazz(); this.variantIndex = initVariantIndex; this.beanInfo = beanInfo; fieldInfoList = new ArrayList<FieldInfo>(beanInfo.getFieldList()); }
public void test_date4() { String text = "{\"1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst\\t\":\"xxxxx\"}"; char[] chars = text.toCharArray(); DefaultJSONParser parser = new DefaultJSONParser(chars, chars.length, ParserConfig.getGlobalInstance(), 0); parser.config(Feature.AllowISO8601DateFormat, true); JSONObject json = parser.parseObject(); Assert.assertEquals("xxxxx", json.get("1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst1234567890abcdefghijklmnopqrst\t")); }
public void test_for_error_2() throws Exception { Exception error = null; try { JSON.parseObject("{\"value\":{'child1':{\"id\":123}}}", Model.class, ParserConfig.getGlobalInstance(), 0); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
public void test_for_error_5() throws Exception { Exception error = null; try { JSON.parseObject("{\"value\":{child1,{\"id\":123}}}", Model.class, ParserConfig.getGlobalInstance(), 0, Feature.AllowUnQuotedFieldNames); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
public JavaBeanDeserializer(ParserConfig config, Class<?> clazz, Type type) { this.feildDeserializerMap = new IdentityHashMap(); this.fieldDeserializers = new ArrayList(); this.sortedFieldDeserializers = new ArrayList(); this.clazz = clazz; this.beanInfo = DeserializeBeanInfo.computeSetters(clazz, type); for (FieldInfo fieldInfo : this.beanInfo.getFieldList()) { addFieldDeserializer(config, clazz, fieldInfo); } for (FieldInfo fieldInfo2 : this.beanInfo.getSortedFieldList()) { this.sortedFieldDeserializers.add((FieldDeserializer) this.feildDeserializerMap.get(fieldInfo2.getName().intern())); } }
public void test_for_ae() throws Exception { ParserConfig.getGlobalInstance().putDeserializer(Area.class, new ObjectDeserializer() { public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { JSONObject jsonObject = (JSONObject) parser.parse(); String areaType; if (jsonObject.get("type") instanceof String) { areaType = (String) jsonObject.get("type"); } else { return null; } if (Area.TYPE_FLOOR.equals(areaType)) { return (T) JSON.toJavaObject(jsonObject, Floor.class); } else if (Area.TYPE_ITEM.equals(areaType)) { return (T) JSON.toJavaObject(jsonObject, Item.class); } return null; } public int getFastMatchToken() { return JSONToken.LBRACE; } }); Data data = JSON.parseObject(jsonData, Data.class); Item item = (Item) ((Floor)(data.areaList.get(0))).children.get(0); }
public void test_for_issue() throws Exception { ParserConfig config = new ParserConfig(); config.setAutoTypeSupport(true); Map<Long, Bean> map = new HashMap<Long, Bean>(); map.put(null, new Bean()); Map<Long, Bean> rmap = (Map<Long, Bean>) JSON.parse(JSON.toJSONString(map, SerializerFeature.WriteClassName), config); System.out.println(rmap); }