@SuppressWarnings("unchecked") public static <T> T parseObject( String input, Type clazz, ParseProcess processor, Feature... features) { return (T) parseObject( input, clazz, ParserConfig.getGlobalInstance(), DEFAULT_PARSER_FEATURE, features); }
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_null() throws Exception { DefaultExtJSONParser parser = new DefaultExtJSONParser( "null", ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); Assert.assertNull(new NumberDeserializer().deserialze(parser, null, null)); }
public static JSONArray parseArray(String text) { if (text == null) { return null; } DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance()); JSONArray array; JSONLexer lexer = parser.getLexer(); if (lexer.token() == JSONToken.NULL) { lexer.nextToken(); array = null; } else if (lexer.token() == JSONToken.EOF) { array = null; } else { array = new JSONArray(); parser.parseArray(array); parser.handleResovleTask(array); } parser.close(); return array; }
public void test_null() throws Exception { String input = "null"; DefaultJSONParser parser = new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); MiscCodec deser = new MiscCodec(); Assert.assertNull(deser.deserialze(parser, null, null)); }
@SuppressWarnings("unchecked") public static <T> T parseObject(String text, TypeReference<T> type, Feature... features) { return (T) parseObject( text, type.getType(), ParserConfig.getGlobalInstance(), DEFAULT_PARSER_FEATURE, features); }
public void test_1() throws Exception { String input = "{'map':{}}"; DefaultExtJSONParser parser = new DefaultExtJSONParser( input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); SortedMap<String, SortedMap> map = JSON.parseObject(input, new TypeReference<SortedMap<String, SortedMap>>() {}.getType()); Assert.assertEquals(TreeMap.class, map.get("map").getClass()); }
public class FastjsonCodec implements Codec { private ParserConfig config = ParserConfig.getGlobalInstance(); public String getName() { return "fastjson"; } public <T> T decodeObject(String text, Class<T> clazz) { DefaultExtJSONParser parser = new DefaultExtJSONParser(text, config); parser.config(Feature.DisableCircularReferenceDetect, true); return parser.parseObject(clazz); } public <T> Collection<T> decodeArray(String text, Class<T> clazz) throws Exception { DefaultExtJSONParser parser = new DefaultExtJSONParser(text, config); parser.config(Feature.DisableCircularReferenceDetect, true); return parser.parseArray(clazz); } public final Object decodeObject(String text) { DefaultJSONParser parser = new DefaultJSONParser(text, config); parser.config(Feature.DisableCircularReferenceDetect, true); return parser.parse(); } public final Object decode(String text) { DefaultJSONParser parser = new DefaultJSONParser(text, config); parser.config(Feature.DisableCircularReferenceDetect, true); return parser.parse(); } // private JavaBeanSerializer serializer = new JavaBeanSerializer(Long_100_Entity.class); public String encode(Object object) throws Exception { SerializeWriter out = new SerializeWriter(); out.config(SerializerFeature.DisableCircularReferenceDetect, true); // out.config(SerializerFeature.DisableCheckSpecialChar, true); JSONSerializer serializer = new JSONSerializer(out); serializer.write(object); String text = out.toString(); out.close(); return text; } @SuppressWarnings("unchecked") public <T> T decodeObject(byte[] input, Class<T> clazz) throws Exception { return (T) JSON.parseObject(input, clazz, Feature.DisableCircularReferenceDetect); } }
public void test_6() throws Exception { String input = "{'map':{}}"; DefaultExtJSONParser parser = new DefaultExtJSONParser( input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); DefaultObjectDeserializer deser = new DefaultObjectDeserializer(); Map map = JSON.parseObject(input, new TypeReference<JSONObject>() {}.getType()); Assert.assertEquals(JSONObject.class, map.get("map").getClass()); }
public void test_0() throws Exception { String input = "{'map':{}}"; DefaultExtJSONParser parser = new DefaultExtJSONParser( input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); DefaultObjectDeserializer deser = new DefaultObjectDeserializer(); Map<String, TreeMap> map = JSON.parseObject(input, new TypeReference<Map<String, TreeMap>>() {}.getType()); Assert.assertTrue(map.get("map") instanceof TreeMap); }
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); }
public static 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 void test_error() throws Exception { String input = "{'map':{}}"; DefaultExtJSONParser parser = new DefaultExtJSONParser( input, ParserConfig.getGlobalInstance(), JSON.DEFAULT_PARSER_FEATURE); DefaultObjectDeserializer deser = new DefaultObjectDeserializer(); Exception error = null; try { JSON.parseObject(input, new TypeReference<Map1<String, HashMap>>() {}.getType()); } catch (Exception ex) { error = ex; } Assert.assertNotNull(error); }
@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 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 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", "rawtypes"}) protected boolean setPropertyValue(Object parent, String name, Object value) { if (parent instanceof Map) { ((Map) parent).put(name, value); return true; } if (parent instanceof List) { for (Object element : (List) parent) { if (element == null) { continue; } setPropertyValue(element, name, value); } return true; } ObjectDeserializer derializer = parserConfig.getDeserializer(parent.getClass()); JavaBeanDeserializer beanDerializer = null; if (derializer instanceof JavaBeanDeserializer) { beanDerializer = (JavaBeanDeserializer) derializer; } else if (derializer instanceof ASMJavaBeanDeserializer) { beanDerializer = ((ASMJavaBeanDeserializer) derializer).getInnterSerializer(); } if (beanDerializer != null) { FieldDeserializer fieldDeserializer = beanDerializer.getFieldDeserializer(name); if (fieldDeserializer == null) { return false; } fieldDeserializer.setValue(parent, value); return true; } throw new UnsupportedOperationException(); }
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.getLexer(); if (lexer.token() == JSONToken.NULL) { lexer.nextToken(); list = null; } else { list = new ArrayList<T>(); parser.parseArray(clazz, list); parser.handleResovleTask(list); } parser.close(); return list; }
public static Object parse( byte[] input, int off, int len, CharsetDecoder charsetDecoder, int features) { charsetDecoder.reset(); int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte()); char[] chars = ThreadLocalCache.getChars(scaleLength); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charBuf = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charBuf); int position = charBuf.position(); DefaultJSONParser parser = new DefaultJSONParser(chars, position, ParserConfig.getGlobalInstance(), features); Object value = parser.parse(); parser.handleResovleTask(value); parser.close(); return value; }
public JSONPath(String path) { this(path, SerializeConfig.getGlobalInstance(), ParserConfig.getGlobalInstance()); }
@SuppressWarnings("resource") public void test_0() throws Exception { new DefaultExtJSONParser(""); new DefaultExtJSONParser("", ParserConfig.getGlobalInstance(), 1); new DefaultExtJSONParser("".toCharArray(), 0, ParserConfig.getGlobalInstance(), 1); }
@SuppressWarnings("unchecked") public static <T> T parseObject(String text, Class<T> clazz, Feature... features) { return (T) parseObject( text, (Type) clazz, ParserConfig.getGlobalInstance(), DEFAULT_PARSER_FEATURE, features); }
public static <T> T toJavaObject(JSON json, Class<T> clazz) { return TypeUtils.cast(json, clazz, ParserConfig.getGlobalInstance()); }
@SuppressWarnings("unchecked") public static Object toJSON(Object javaObject, ParserConfig mapping) { if (javaObject == null) { return null; } if (javaObject instanceof JSON) { return javaObject; } if (javaObject instanceof Map) { Map<Object, Object> map = (Map<Object, Object>) javaObject; JSONObject json = new JSONObject(map.size()); for (Map.Entry<Object, Object> entry : map.entrySet()) { Object key = entry.getKey(); String jsonKey = TypeUtils.castToString(key); Object jsonValue = toJSON(entry.getValue()); json.put(jsonKey, jsonValue); } return json; } if (javaObject instanceof Collection) { Collection<Object> collection = (Collection<Object>) javaObject; JSONArray array = new JSONArray(collection.size()); for (Object item : collection) { Object jsonValue = toJSON(item); array.add(jsonValue); } return array; } Class<?> clazz = javaObject.getClass(); if (clazz.isEnum()) { return ((Enum<?>) javaObject).name(); } if (clazz.isArray()) { int len = Array.getLength(javaObject); JSONArray array = new JSONArray(len); for (int i = 0; i < len; ++i) { Object item = Array.get(javaObject, i); Object jsonValue = toJSON(item); array.add(jsonValue); } return array; } if (mapping.isPrimitive(clazz)) { return javaObject; } try { List<FieldInfo> getters = TypeUtils.computeGetters(clazz, null); JSONObject json = new JSONObject(getters.size()); for (FieldInfo field : getters) { Object value = field.get(javaObject); Object jsonValue = toJSON(value); json.put(field.getName(), jsonValue); } return json; } catch (IllegalAccessException e) { throw new JSONException("toJSON error", e); } catch (InvocationTargetException e) { throw new JSONException("toJSON error", e); } }
// /////// public static Object toJSON(Object javaObject) { return toJSON(javaObject, ParserConfig.getGlobalInstance()); }
public LongFieldDeserializer(ParserConfig mapping, Class<?> clazz, FieldInfo fieldInfo) { super(clazz, fieldInfo); fieldValueDeserilizer = mapping.getDeserializer(fieldInfo); }