/** * Creates a new {@code JSONArray} by copying all values from the given collection. * * @param copyFrom a collection whose values are of supported types. Unsupported values are not * permitted and will yield an array in an inconsistent state. */ /* Accept a raw type for API compatibility */ public JSONArray(Collection<?> copyFrom) { this(); if (copyFrom != null) { for (Object aCopyFrom : copyFrom) { put(JSONObject.wrap(aCopyFrom)); } } }
/** * Construct a JSONArray from a Collection. * * @param collection * A Collection. */ public JSONArray(Collection<Object> collection) { this.myArrayList = new ArrayList<Object>(); if (collection != null) { Iterator<Object> iter = collection.iterator(); while (iter.hasNext()) { this.myArrayList.add(JSONObject.wrap(iter.next())); } } }
private Object wrap(Object thingy) { if (thingy instanceof Array) { Object[] array=(Object[])thingy; JSONArray jsonArray=new JSONArray(); for (Object o : array) { jsonArray.put(wrap(o)); } return(jsonArray); } if (thingy instanceof CharSequence) { return(JSONObject.wrap(thingy.toString())); } return(JSONObject.wrap(thingy)); }
/** * Creates a new {@code JSONArray} by copying all values from the given * collection. * * @param copyFrom a collection whose values are of supported types. * Unsupported values are not permitted and will yield an array in an * inconsistent state. */ /* Accept a raw type for API compatibility */ public JSONArray(Collection copyFrom) { this(); if (copyFrom != null) { for (Iterator it = copyFrom.iterator(); it.hasNext();) { put(JSONObject.wrap(it.next())); } } }
/** * Construct a JSONArray from a Collection. * * @param collection * A Collection. */ public JSONArray(Collection<?> collection) { if (collection == null) { this.myArrayList = new ArrayList<Object>(); } else { this.myArrayList = new ArrayList<Object>(collection.size()); for (Object o: collection){ this.myArrayList.add(JSONObject.wrap(o)); } } }
/** * Construct a JSONObject from a Map. * * @param map * A map object that can be used to initialize the contents of * the JSONObject. * @throws JSONException */ public JSONObject(Map<String, Object> map) { this.map = new HashMap<String, Object>(); if (map != null) { Iterator<Entry<String, Object>> i = map.entrySet().iterator(); while (i.hasNext()) { Entry<String, Object> entry = i.next(); Object value = entry.getValue(); if (value != null) { this.map.put(entry.getKey(), wrap(value)); } } } }
/** * Creates a new {@code JSONArray} with values from the given primitive array. */ public JSONArray(Object array) throws JSONException { if (!array.getClass().isArray()) { throw new JSONException("Not a primitive array: " + array.getClass()); } final int length = Array.getLength(array); values = new ArrayList<Object>(length); for (int i = 0; i < length; ++i) { put(JSONObject.wrap(Array.get(array, i))); } }
/** * Construct a JSONArray from an array * * @throws JSONException * If not an array. */ public JSONArray(Object array) throws JSONException { this(); if (array.getClass().isArray()) { int length = Array.getLength(array); for (int i = 0; i < length; i += 1) { this.put(JSONObject.wrap(Array.get(array, i))); } } else { throw new JSONException( "JSONArray initial value should be a string or collection or array."); } }
/** * Creates a new {@code JSONObject} by copying all name/value mappings from * the given map. * * @param copyFrom a map whose keys are of type {@link String} and whose * values are of supported types. * @throws NullPointerException if any of the map's keys are null. */ /* (accept a raw type for API compatibility) */ public JSONObject(Map copyFrom) { this(); Map<?, ?> contentsTyped = (Map<?, ?>) copyFrom; for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) { /* * Deviate from the original by checking that keys are non-null and * of the proper type. (We still defer validating the values). */ String key = (String) entry.getKey(); if (key == null) { throw new NullPointerException("key == null"); } nameValuePairs.put(key, wrap(entry.getValue())); } }
/** * Creates a new {@code JSONObject} by copying all name/value mappings from the given map. * * @param copyFrom a map whose keys are of type {@link String} and whose values are of supported * types. * @throws NullPointerException if any of the map's keys are null. */ /* (accept a raw type for API compatibility) */ public JSONObject(Map copyFrom) { this(); Map<?, ?> contentsTyped = (Map<?, ?>) copyFrom; for (Map.Entry<?, ?> entry : contentsTyped.entrySet()) { /* * Deviate from the original by checking that keys are non-null and of the proper type. (We * still defer validating the values). */ String key = (String) entry.getKey(); if (key == null) { throw new NullPointerException("key == null"); } nameValuePairs.put(key, wrap(entry.getValue())); } }
/** * Creates a new {@code JSONArray} with values from the given primitive array. * * @param array The values to use. * @throws JSONException if any of the values are non-finite double values (i.e. NaN or infinite) */ public JSONArray(Object array) throws JSONException { if (!array.getClass().isArray()) { throw new JSONException("Not a primitive array: " + array.getClass()); } final int length = Array.getLength(array); values = new ArrayList<Object>(length); for (int i = 0; i < length; ++i) { put(JSONObject.wrap(Array.get(array, i))); } }
/** * Construct a JSONObject from a Map. * * @param m * A map object that can be used to initialize the contents of * the JSONObject. */ public JSONObject(Map<?, ?> m) { if (m == null) { this.map = new HashMap<String, Object>(); } else { this.map = new HashMap<String, Object>(m.size()); for (final Entry<?, ?> e : m.entrySet()) { final Object value = e.getValue(); if (value != null) { this.map.put(String.valueOf(e.getKey()), wrap(value)); } } } }
/** * Construct a JSONArray from an array * * @throws JSONException * If not an array. */ public JSONArray(Object array) throws JSONException { this(); if (array.getClass().isArray()) { int length = Array.getLength(array); this.myArrayList.ensureCapacity(length); for (int i = 0; i < length; i += 1) { this.put(JSONObject.wrap(Array.get(array, i))); } } else { throw new JSONException( "JSONArray initial value should be a string or collection or array."); } }
this.map.put(key, wrap(result));
props.put(key, wrap(result)); } else if (!method.getReturnType().isArray()) { props.put(key, JSONObject.NULL);
final Object result = method.invoke(bean); if (result != null) { this.map.put(key, wrap(result));
/** * Construct a JSONArray from a Collection. * * @param collection * A Collection. */ public JSONArray(Collection<?> collection) { if (collection == null) { this.myArrayList = new ArrayList<Object>(); } else { this.myArrayList = new ArrayList<Object>(collection.size()); for (Object o: collection){ this.myArrayList.add(JSONObject.wrap(o)); } } }
/** * Construct a JSONObject from a Map. * * @param m * A map object that can be used to initialize the contents of * the JSONObject. * @throws JSONException * If a value in the map is non-finite number. * @throws NullPointerException * If a key in the map is <code>null</code> */ public JSONObject(Map<?, ?> m) { if (m == null) { this.map = new HashMap<String, Object>(); } else { this.map = new HashMap<String, Object>(m.size()); for (final Entry<?, ?> e : m.entrySet()) { if(e.getKey() == null) { throw new NullPointerException("Null key."); } final Object value = e.getValue(); if (value != null) { this.map.put(String.valueOf(e.getKey()), wrap(value)); } } } }
/** * Construct a JSONArray from an array. * * @param array * Array. If the parameter passed is null, or not an array, an * exception will be thrown. * * @throws JSONException * If not an array or if an array value is non-finite number. * @throws NullPointerException * Thrown if the array parameter is null. */ public JSONArray(Object array) throws JSONException { this(); if (array.getClass().isArray()) { int length = Array.getLength(array); this.myArrayList.ensureCapacity(length); for (int i = 0; i < length; i += 1) { this.put(JSONObject.wrap(Array.get(array, i))); } } else { throw new JSONException( "JSONArray initial value should be a string or collection or array."); } }