public Object bind(Object input) { return this.bind(input, null); }
public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) { if (value instanceof Collection) { return context.bindIntoCollection((Collection) value, new ArrayList(), targetType); } else { ArrayList<Object> set= new ArrayList<Object>(); set.add(context.bind(value)); return set; } } }
public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) { if (value instanceof Collection) { return context.bindIntoCollection((Collection) value, new HashSet(), targetType); } else { HashSet<Object> set= new HashSet<Object>(); set.add(context.bind(value)); return set; } } }
public Object bindIntoMap(Map input, Map<Object, Object> result, Type keyType, Type valueType) { jsonStack.add(input); objectStack.add(result); for (Object inputKey : input.keySet()) { currentPath.enqueue("keys"); Object key= bind(inputKey, keyType); currentPath.pop(); currentPath.enqueue("values"); Object value= bind(input.get(inputKey), valueType); currentPath.pop(); result.put(key, value); } objectStack.pop(); jsonStack.pop(); return result; }
public class ListDocumentFactory implements ObjectFactory { public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) { if( value instanceof Collection ) { return context.bindIntoCollection((Collection)value, new ArrayList(), targetType); } else { List collection = new ArrayList(); if( targetType instanceof ParameterizedType ) { ParameterizedType ptype = (ParameterizedType) targetType; collection.add( context.bind(value, ptype.getActualTypeArguments()[0]) ); } else { collection.add( context.bind( value ) ); return collection; } } } }
public <T extends Collection<Object>> T bindIntoCollection(Collection value, T target, Type targetType) { Type valueType= null; if (targetType instanceof ParameterizedType) { valueType= ((ParameterizedType) targetType).getActualTypeArguments()[0]; } jsonStack.add(value); objectStack.add(target); getCurrentPath().enqueue("values"); for (Object obj : value) { target.add(bind(obj, valueType)); } getCurrentPath().pop(); objectStack.pop(); jsonStack.pop(); return target; }
/** * Deserialize the given json formatted input into a Java object. * * @param input a json formatted string. * @return an Java instance deserialized from the json input. */ public T deserialize(String input) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }
/** * Same as {@link #deserialize(String)}, but uses an instance of * java.io.Reader as json input. * * @param input the stream where the json input is coming from. * @return an Java instance deserialized from the java.io.Reader's input. */ public T deserialize(Reader input) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }
/** * Same as {@link #deserialize(java.io.Reader, Class)}, but uses an instance of * java.io.Reader as json input. * * @param input the stream where the json input is coming from. * @param root a Class used to create the initial object. * @return an Java instance deserialized from the java.io.Reader's input. */ public T deserialize(Reader input, Class root) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue(), root); }
/** * Deserialize the given json input, and use the given Class as * the type of the initial object to deserialize into. This object * must implement a no-arg constructor. * * @param input a json formatted string. * @param root a Class used to create the initial object. * @return the object created from the given json input. */ public T deserialize(String input, Class root) { ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue(), root); }
public Object instantiate(ObjectBinder context, Object value, Type targetType, Class targetClass) { List list= (List) value; context.getCurrentPath().enqueue("values"); try { Class memberClass= targetClass.getComponentType() != null ? targetClass.getComponentType() : context.findClassAtPath(context.getCurrentPath()); if (memberClass == null) throw new JSONException("Missing concrete class for array. You might require a use() method."); Object array= Array.newInstance(memberClass, list.size()); for (int i= 0; i < list.size(); i++) { Object v= context.bind(list.get(i), memberClass); Array.set(array, i, v); } return array; } catch (ClassNotFoundException ex) { throw new JSONException(String.format("%s: Could not find class %s", context.getCurrentPath(), ex.getMessage()), ex); } finally { context.getCurrentPath().pop(); } } }
/** * Deserialize the given json input, and use the given ObjectFactory to * create the initial object to deserialize into. * * @param input a json formatted string. * @param factory an ObjectFactory used to create the initial object. * @return the object created from the given json input. */ public T deserialize(String input, ObjectFactory factory) { use((String) null, factory); ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }
/** * Same as {@link #deserialize(String, ObjectFactory)}, but uses an instance of * java.io.Reader as json input. * * @param input the stream where the json input is coming from. * @param factory an ObjectFactory used to create the initial object. * @return an Java instance deserialized from the java.io.Reader's input. */ public T deserialize(Reader input, ObjectFactory factory) { use((String) null, factory); ObjectBinder binder= createObjectBinder(); return (T) binder.bind(new JSONTokener(input).nextValue()); }