public void test_array_mapping() throws Exception { String json = "[-0.012671709,0.6042485,0.13231707,0.80090785,0.6192943]"; String json2 = "[\"-0.012671709\",\"0.6042485\",\"0.13231707\",\"0.80090785\",\"0.6192943\"]"; Model m1 = JSON.parseObject(json, Model.class, Feature.SupportArrayToBean); Model m2 = JSON.parseObject(json2, Model.class, Feature.SupportArrayToBean); assertNotNull(m1); assertNotNull(m2); assertEquals(-0.012671709f, m1.v1); assertEquals(0.6042485f, m1.v2); assertEquals(0.13231707f, m1.v3); assertEquals(0.80090785f, m1.v4); assertEquals(0.6192943f, m1.v5); assertEquals(-0.012671709f, m2.v1); assertEquals(0.6042485f, m2.v2); assertEquals(0.13231707f, m2.v3); assertEquals(0.80090785f, m2.v4); assertEquals(0.6192943f, m2.v5); }
public void test_1() throws Exception { JSONObject res = new JSONObject(); res.put("a", 1); res.put("b", 2); res.put("c", 3); String[] tests = { "{ 'a':1, 'b':2, 'c':3 }", "{ 'a':1,,'b':2, 'c':3 }", "{,'a':1, 'b':2, 'c':3 }", "{'a':1, 'b':2, 'c':3,,}", "{,,'a':1,,,,'b':2,'c':3,,,,,}", }; for (String t : tests) { DefaultJSONParser ext = new DefaultJSONParser(t); ext.config(Feature.AllowArbitraryCommas, true); JSONObject extRes = ext.parseObject(); Assert.assertEquals(res, extRes); DefaultJSONParser basic = new DefaultJSONParser(t); basic.config(Feature.AllowArbitraryCommas, true); JSONObject basicRes = basic.parseObject(); Assert.assertEquals(res, basicRes); } }
void parseExtra(DefaultJSONParser parser, Object object, String key) { final JSONLexer lexer = parser.getLexer(); // xxx if (!lexer.isEnabled(Feature.IgnoreNotMatch)) { throw new JSONException("setter not found, class " + clazz.getName() + ", property " + key); } lexer.nextTokenWithColon(); Type type = FilterUtils.getExtratype(parser, object, key); Object value; if (type == null) { value = parser.parse(); // skip } else { value = parser.parseObject(type); } FilterUtils.processExtra(parser, object, key, value); }
public void test_0() throws Exception { List<?> res = Arrays.asList(1, 2, 3); String[] tests = { "[1,2,3]", "[1,,2,3]", "[1,2,,,3]", "[1 2,,,3]", "[1 2 3]", "[1, 2, 3,,]", "[,,1, 2, 3,,]", }; for (String t : tests) { DefaultJSONParser ext = new DefaultJSONParser(t); ext.config(Feature.AllowArbitraryCommas, true); List<Object> extRes = ext.parseArray(Object.class); Assert.assertEquals(res, extRes); DefaultJSONParser basic = new DefaultJSONParser(t); basic.config(Feature.AllowArbitraryCommas, true); List<Object> basicRes = new ArrayList<Object>(); basic.parseArray(basicRes); Assert.assertEquals(res, basicRes); } }
public void test_2() throws Exception { String json = "[-1883391953414482124,-3019416596934963650,6497525620823745793,2136224289077142499,-2090575024006307745]"; String json2 = "[\"-1883391953414482124\",\"-3019416596934963650\",\"6497525620823745793\",\"2136224289077142499\",\"-2090575024006307745\"]"; Model m1 = new JSONReader(new StringReader(json), Feature.SupportArrayToBean).readObject(Model.class); Model m2 = new JSONReader(new StringReader(json2), Feature.SupportArrayToBean).readObject(Model.class); assertNotNull(m1); assertNotNull(m2); assertEquals(-1883391953414482124L, m1.v1.longValue()); assertEquals(-3019416596934963650L, m1.v2.longValue()); assertEquals(6497525620823745793L, m1.v3.longValue()); assertEquals(2136224289077142499L, m1.v4.longValue()); assertEquals(-2090575024006307745L, m1.v5.longValue()); assertEquals(-1883391953414482124L, m2.v1.longValue()); assertEquals(-3019416596934963650L, m2.v2.longValue()); assertEquals(6497525620823745793L, m2.v3.longValue()); assertEquals(2136224289077142499L, m2.v4.longValue()); assertEquals(-2090575024006307745L, m2.v5.longValue()); }
public void test_array_mapping() throws Exception { String json = "[-0.012671709,0.22676692048907365,0.13231707,0.80090785,0.6192943]"; String json2 = "[\"-0.012671709\",\"0.22676692048907365\",\"0.13231707\",\"0.80090785\",\"0.6192943\"]"; Model m1 = JSON.parseObject(json, Model.class, Feature.SupportArrayToBean); Model m2 = JSON.parseObject(json2, Model.class, Feature.SupportArrayToBean); assertNotNull(m1); assertNotNull(m2); assertEquals(-0.012671709D, m1.v1); assertEquals(0.22676692048907365D, m1.v2); assertEquals(0.13231707D, m1.v3); assertEquals(0.80090785D, m1.v4); assertEquals(0.6192943D, m1.v5); assertEquals(-0.012671709D, m2.v1); assertEquals(0.22676692048907365D, m2.v2); assertEquals(0.13231707D, m2.v3); assertEquals(0.80090785D, m2.v4); assertEquals(0.6192943D, m2.v5); }
public void test_list() throws Exception { Model model = new Model(); LinkedHashSet tables = new LinkedHashSet(); tables.add(new ExtTable(1001)); tables.add(new Table()); model.setTables(tables); String json = JSON.toJSONString(model); assertEquals("{\"tables\":[{\"@type\":\"com.alibaba.json.bvt.writeClassName.WriteClassNameTest_Set5$ExtTable\",\"id\":1001},{}]}", json); Model model2 = JSON.parseObject(json, Model.class); assertEquals(ExtTable.class, model2.getTables().iterator().next().getClass()); JSONObject jsonObject = JSON.parseObject(json, Feature.IgnoreAutoType); assertEquals("{\"tables\":[{\"id\":1001},{}]}", jsonObject.toJSONString()); }
public static final <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) { if (input == null) { return null; } for (Feature featrue : features) { featureValues = Feature.config(featureValues, featrue, true); } DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues); if (processor instanceof ExtraTypeProvider) { parser.getExtraTypeProviders().add((ExtraTypeProvider) processor); } if (processor instanceof ExtraProcessor) { parser.getExtraProcessors().add((ExtraProcessor) processor); } T value = parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return value; }
public void test_0 () throws Exception { Image image = new Image(); image.setHeight(123); image.setSize(Size.LARGE); image.setTitle("xx"); String text = JSON.toJSONString(image, SerializerFeature.BeanToArray); System.out.println(text); Image image2 = JSON.parseObject(text, Image.class, Feature.SupportArrayToBean); Assert.assertEquals(image.getHeight(), image2.getHeight()); Assert.assertEquals(image.getWidth(), image2.getWidth()); Assert.assertEquals(image.getSize(), image2.getSize()); Assert.assertEquals(image.getTitle(), image2.getTitle()); Assert.assertEquals(image.getUri(), image2.getUri()); }
@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 void test_AllowSingleQuotes_1() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{'a':'3'}"); parser.config(Feature.AllowSingleQuotes, true); JSONObject json = (JSONObject) parser.parse(); Assert.assertEquals(1, json.size()); Assert.assertEquals("3", (String) json.get("a")); }
public void test_1() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("['1','2']"); parser.config(Feature.AllowISO8601DateFormat, false); List<Object> list = new ArrayList<Object>(); parser.parseArray(Integer.class, list); Assert.assertEquals(new Integer(1), list.get(0)); Assert.assertEquals(new Integer(2), list.get(1)); }
public void test_default() throws Exception { DefaultJSONParser parser = new DefaultJSONParser(""); Assert.assertEquals(false, parser.isEnabled(Feature.AllowComment)); Assert.assertEquals(true, parser.isEnabled(Feature.AllowSingleQuotes)); Assert.assertEquals(true, parser.isEnabled(Feature.AllowUnQuotedFieldNames)); Assert.assertEquals(true, parser.isEnabled(Feature.AutoCloseSource)); Assert.assertEquals(true, parser.isEnabled(Feature.InternFieldNames)); }
public static Object parse(byte[] input, Feature... features) { char[] chars = allocateChars(input.length); int len = IOUtils.decodeUTF8(input, 0, input.length, chars); if (len < 0) { return null; } return parse(new String(chars, 0, len), features); }
public static void perf2() { long start = System.currentTimeMillis(); for (int i = 0; i < 1000 * 1000; ++i) { JSON.parseObject(json2, Model.class, Feature.SupportArrayToBean); } long millis = System.currentTimeMillis() - start; System.out.println("millis : " + millis); }
public void test_parseArray_error() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("[1,2}"); parser.config(Feature.AllowArbitraryCommas, false); List<String> list = new ArrayList<String>(); Exception error = null; try { parser.parseArray(String.class, list); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
public static void perf() { long start = System.currentTimeMillis(); for (int i = 0; i < 1000 * 1000 * 10; ++i) { JSON.parseObject(json, Model.class, Feature.SupportArrayToBean); } long millis = System.currentTimeMillis() - start; System.out.println("millis : " + millis); }
@SuppressWarnings("unchecked") public static final <T> T parseObject(String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) { if (input == null) { return null; } for (Feature featrue : features) { featureValues = Feature.config(featureValues, featrue, true); } DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues); if (processor instanceof ExtraTypeProvider) { parser.getExtraTypeProviders().add((ExtraTypeProvider) processor); } if (processor instanceof ExtraProcessor) { parser.getExtraProcessors().add((ExtraProcessor) processor); } T value = (T) parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return (T) value; }
public void test_1() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{v1:'3'}"); parser.config(Feature.AllowUnQuotedFieldNames, true); parser.config(Feature.AllowSingleQuotes, true); A a = parser.parseObject(A.class); Assert.assertEquals(3, a.getV1()); }
public void test_1() throws Exception { JSONReader read = new JSONReader(new StringReader("[\"A\",\"B\"]")); read.config(Feature.SupportArrayToBean, true); Model model = read.readObject(Model.class); Assert.assertEquals(Type.A, model.value); Assert.assertEquals(Type.B, model.value1); read.close(); }
public void test_2() throws Exception { String text = "{\"@v1\":\"v1\",\"@type\":\"v2\", \"@\":\"v3\",\"$\":\"v4\",\"$ref\":\"v5\"}"; Map<String,String> map = JSON.parseObject(text, new TypeReference<Map<String,String>>(){}, Feature.DisableSpecialKeyDetect); Assert.assertEquals("v1", map.get("@v1")); Assert.assertEquals("v2", map.get("@type")); Assert.assertEquals("v3", map.get("@")); Assert.assertEquals("v4", map.get("$")); Assert.assertEquals("v5", map.get("$ref")); }
public void test_error_nu() throws Exception { Exception error = null; try { JSONReader read = new JSONReader(new StringReader("[nu")); read.config(Feature.SupportArrayToBean, true); Model model = read.readObject(Model.class); read.readObject(Model.class); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
/** * @since 1.2.11 */ @SuppressWarnings("unchecked") public static <T> T parseObject(InputStream is, // Type type, // Feature... features) throws IOException { return (T) parseObject(is, IOUtils.UTF8, type, features); }
public void test_error_4() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("['1','2'}"); parser.config(Feature.AllowISO8601DateFormat, false); Exception error = null; try { parser.parseArray(new Type[] {}); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public void test_0() throws Exception { VO vo = new VO(); vo.setId(123); vo.setName("wenshao"); String text = JSON.toJSONString(vo, SerializerFeature.BeanToArray); Assert.assertEquals("[123,\"wenshao\"]", text); VO vo2 = JSON.parseObject(text, VO.class, Feature.SupportArrayToBean); Assert.assertEquals(vo.getId(), vo2.getId()); Assert.assertEquals(vo.getName(), vo2.getName()); }
public void test_1() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{\"value\":3,\"id\":1}"); parser.config(Feature.AllowArbitraryCommas, false); Entity entity = new Entity(); parser.parseObject(entity); Assert.assertEquals(3, entity.getValue()); }
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_error_1() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{\"a\":'3'}"); parser.config(Feature.AllowSingleQuotes, false); parser.parse(); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public void test_long_error() throws Exception { Exception error = null; try { JSON.parseObject("[-", Model.class, Feature.SupportArrayToBean); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
public void test_error_2() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("[new X()]"); parser.config(Feature.AllowISO8601DateFormat, false); List list = new ArrayList(); Exception error = null; try { parser.parseArray(list); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public void test_for_issue_one() throws Exception { Model model = new Model(); model.id = 123; model.name = null; model.modelName = null; model.isFlay = false; model.persons = new ArrayList<Person>(); model.persons.add(new Person()); String str = JSON.toJSONString(model, SerializerFeature.BeanToArray); // System.out.println(str); JSON.parseObject(str, Model.class, Feature.SupportArrayToBean); }
public void test_error_overflow() throws Exception { String text = "[2147483649:\"wenshao\"]"; Exception error = null; try { JSON.parseObject(text, VO.class, Feature.SupportArrayToBean); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
public void test_read() throws Exception { JSONReader reader = new JSONReader(new StringReader("{}")); reader.config(Feature.AllowArbitraryCommas, true); JSONObject object = (JSONObject) reader.readObject(); Assert.assertNotNull(object); reader.close(); }
public void test_null_array_reader() throws Exception { JSONReader reader = new JSONReader(new StringReader("[\"null\" ,\"null\"]"), Feature.SupportArrayToBean); Model model = reader.readObject(Model.class); assertNotNull(model); assertNull(model.v1); assertNull(model.v2); }
public void test_date_1() throws Exception { int features = JSON.DEFAULT_PARSER_FEATURE; features = Feature.config(features, Feature.AllowISO8601DateFormat, true); DefaultJSONParser parser = new DefaultJSONParser("\"2011-01-09T13:49:53.254\"", ParserConfig.getGlobalInstance(), features); java.sql.Timestamp date = parser.parseObject(java.sql.Timestamp.class); Assert.assertEquals(new java.sql.Timestamp(1294552193254L), date); parser.close(); }
public void test_error_1() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{'a'3}"); parser.config(Feature.AllowSingleQuotes, true); parser.parseObject(A.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public static int getParserFeatures(Class<?> clazz) { JSONType annotation = (JSONType) clazz.getAnnotation(JSONType.class); if (annotation == null) { return 0; } return Feature.of(annotation.parseFeatures()); }
public void test_error_3() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("[] a"); parser.config(Feature.AllowISO8601DateFormat, false); List list = new ArrayList(); Exception error = null; try { parser.parseArray(list); parser.close(); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
public void test_error_5() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("[1,null }"); ArrayList list = new ArrayList(); parser.config(Feature.AllowISO8601DateFormat, false); Exception error = null; try { parser.parseArray(String.class, list); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }