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); } }
@SuppressWarnings("unchecked") public static <T> T parseObject( String input, Type clazz, ParserConfig config, ParseProcess processor, int featureValues, Feature... features) { if (input == null) { return null; } for (Feature feature : features) { featureValues = Feature.config(featureValues, feature, 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_0() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{\"id\":3, \"name\":\"xx\"}", ParserConfig.getGlobalInstance()); Entity entity = new Entity(); parser.parseObject(entity); }
public void test_error_4() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{\"a\"3}"); parser.parseObject(A.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public void test_error_6() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{'a':3}"); parser.config(Feature.AllowSingleQuotes, false); parser.parseObject(A.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public void test_error_3() throws Exception { JSONException error = null; try { DefaultJSONParser parser = new DefaultJSONParser("{"); parser.config(Feature.AllowUnQuotedFieldNames, true); parser.parseObject(A.class); } catch (JSONException e) { error = e; } Assert.assertNotNull(error); }
public void test_getInput() { String text = "{,,}"; char[] chars = text.toCharArray(); DefaultJSONParser parser = new DefaultJSONParser(chars, chars.length, ParserConfig.getGlobalInstance(), 0); JSONException error = null; try { parser.parseObject(); } catch (JSONException ex) { error = ex; } Assert.assertNotNull(error); }
@SuppressWarnings({"rawtypes", "unchecked"}) protected Object deserialze(DefaultJSONParser parser, Type type, Object fieldName, Map map) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type keyType = parameterizedType.getActualTypeArguments()[0]; Type valueType = parameterizedType.getActualTypeArguments()[1]; if (String.class == keyType) { return parseMap(parser, (Map<String, Object>) map, valueType, fieldName); } else { return parseMap(parser, map, keyType, valueType, fieldName); } } else { return parser.parseObject(map, fieldName); } }
@SuppressWarnings("unchecked") public static <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 feature : features) { featureValues = Feature.config(featureValues, feature, true); } DefaultJSONParser parser = new DefaultJSONParser(input, length, ParserConfig.getGlobalInstance(), featureValues); T value = (T) parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return (T) value; }
@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 void test_2() throws Exception { A res = new A(); res.setA(1); res.setB(2); res.setC(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); A extRes = ext.parseObject(A.class); Assert.assertEquals(res, extRes); } }
public <T> T decodeObject(String text, Class<T> clazz) { ParserConfig config = new ParserConfig(); DefaultJSONParser parser = new DefaultJSONParser(text, config); parser.config(Feature.DisableCircularReferenceDetect, true); return parser.parseObject(clazz); }
@SuppressWarnings("rawtypes") public static Map parseMap( DefaultJSONParser parser, Map<String, Object> map, Type valueType, Object fieldName) { JSONLexer lexer = parser.getLexer(); if (lexer.token() != JSONToken.LBRACE) { throw new JSONException("syntax error, expect {, actual " + lexer.token()); } ParseContext context = parser.getContext(); try { for (; ; ) { lexer.skipWhitespace(); char ch = lexer.getCurrent(); if (parser.isEnabled(Feature.AllowArbitraryCommas)) { while (ch == ',') { lexer.next(); lexer.skipWhitespace(); ch = lexer.getCurrent(); } } String key; if (ch == '"') { key = lexer.scanSymbol(parser.getSymbolTable(), '"'); lexer.skipWhitespace(); ch = lexer.getCurrent(); if (ch != ':') { throw new JSONException("expect ':' at " + lexer.pos()); } } else if (ch == '}') { lexer.next(); lexer.resetStringPosition(); lexer.nextToken(JSONToken.COMMA); return map; } else if (ch == '\'') { if (!parser.isEnabled(Feature.AllowSingleQuotes)) { throw new JSONException("syntax error"); } key = lexer.scanSymbol(parser.getSymbolTable(), '\''); lexer.skipWhitespace(); ch = lexer.getCurrent(); if (ch != ':') { throw new JSONException("expect ':' at " + lexer.pos()); } } else { if (!parser.isEnabled(Feature.AllowUnQuotedFieldNames)) { throw new JSONException("syntax error"); } key = lexer.scanSymbolUnQuoted(parser.getSymbolTable()); lexer.skipWhitespace(); ch = lexer.getCurrent(); if (ch != ':') { throw new JSONException("expect ':' at " + lexer.pos() + ", actual " + ch); } } lexer.next(); lexer.skipWhitespace(); ch = lexer.getCurrent(); lexer.resetStringPosition(); if (key == JSON.DEFAULT_TYPE_KEY) { String typeName = lexer.scanSymbol(parser.getSymbolTable(), '"'); Class<?> clazz = TypeUtils.loadClass(typeName); if (clazz == map.getClass()) { lexer.nextToken(JSONToken.COMMA); if (lexer.token() == JSONToken.RBRACE) { lexer.nextToken(JSONToken.COMMA); return map; } continue; } ObjectDeserializer deserializer = parser.getConfig().getDeserializer(clazz); lexer.nextToken(JSONToken.COMMA); parser.setResolveStatus(DefaultJSONParser.TypeNameRedirect); if (context != null && !(fieldName instanceof Integer)) { parser.popContext(); } return (Map) deserializer.deserialze(parser, clazz, fieldName); } Object value; lexer.nextToken(); if (lexer.token() == JSONToken.NULL) { value = null; lexer.nextToken(); } else { value = parser.parseObject(valueType); } map.put(key, value); parser.checkMapResolve(map, key); parser.setContext(context, value, key); final int tok = lexer.token(); if (tok == JSONToken.EOF || tok == JSONToken.RBRACKET) { return map; } if (tok == JSONToken.RBRACE) { lexer.nextToken(); return map; } } } finally { parser.setContext(context); } }
public void test_4() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{a:3}"); parser.config(Feature.AllowUnQuotedFieldNames, true); LinkedHashMap a = parser.parseObject(LinkedHashMap.class); Assert.assertEquals(3, a.get("a")); }
public void test_1() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{a:3}"); parser.config(Feature.AllowUnQuotedFieldNames, true); A a = parser.parseObject(A.class); Assert.assertEquals(3, a.getA()); }
public void test_0() throws Exception { DefaultJSONParser parser = new DefaultJSONParser("{'a':3}"); parser.config(Feature.AllowSingleQuotes, true); A a = parser.parseObject(A.class); Assert.assertEquals(3, a.getA()); }
@SuppressWarnings("unchecked") public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) { type = TypeUtils.unwrapOptional(type); Object value = parser.parseObject(type); return (T) Optional.of(value); }
public JSONObject parseObject() { JSONObject object = new JSONObject(); parseObject(object); return object; }