Class JsonList
- All Implemented Interfaces:
- Serializable,- Cloneable,- Iterable<Object>,- Collection<Object>,- Deque<Object>,- List<Object>,- Queue<Object>
- Direct Known Subclasses:
- DelegateList
 An extension of LinkedList, so all methods available to in that class are also available to this class.
 
 Note that the use of this class is optional for generating JSON.  The serializers will accept any objects that implement the
 Collection interface.  But this class provides some useful additional functionality when working with JSON
 models constructed from Java Collections Framework objects.  For example, a constructor is provided for converting a
 JSON array string directly into a List.  It also contains accessor methods for to avoid common typecasting
 when accessing elements in a list.
 
Example:
   
Notes:
- This class is not thread safe.
See Also:
- 
Field SummaryFieldsFields inherited from class java.util.AbstractListmodCount
- 
Constructor SummaryConstructorsConstructorDescriptionJsonList()Construct an empty list.Construct a list initialized with the specified reader containing JSON.Construct a list initialized with the specified string.JsonList(CharSequence json) Construct a list initialized with the specified JSON.JsonList(CharSequence in, Parser p) Construct a list initialized with the specified string.Construct a list initialized with the contents.JsonList(Collection<?> copyFrom) Construct a list initialized with the specified list.JsonList(BeanSession session) Construct an empty list with the specified bean context.
- 
Method SummaryModifier and TypeMethodDescriptionAdds the value to this list.Adds all the values in the specified array to this list.append(Collection<?> values) Adds all the values in the specified collection to this list.Adds an entry to this list if the boolean flag istrue .<T> JsonListAdd if predicate matches.appendReverse(Object... values) Adds the contents of the array to the list in reverse order.appendReverse(List<?> values) Adds all the entries in the specified collection to this list in reverse order.asJson()A synonym fortoString()asString()Serialize this array to Simplified JSON.asString(WriterSerializer serializer) Serialize this array to a string using the specified serializer.Converts this object into the specified class type.static JsonListcreate()Construct an empty list.Similar toremove(int),but the key is a slash-delimited path used to traverse entries in this POJO.<E> Iterable<E>Creates anIterablewith elements of the specified child type.<T> TGet the entry at the specified index, converted to the specified type.<T> TGet the entry at the specified index, converted to the specified type.<T> TSame asget(int,Class), but the key is a slash-delimited path used to traverse entries in this POJO.<T> TSame asgetAt(String,Class), but allows for conversion to complex maps and collections.Returns theBeanSessioncurrently associated with this list.getBoolean(int index) Shortcut for callingget(index, Boolean..class )ClassMeta<?>getClassMeta(int index) Returns theClassMetaof the class of the object at the specified index.getInt(int index) Shortcut for callingget(index, Integer..class )getList(int index) Shortcut for callingget(index, JsonList..class )<E> List<E>Same asgetList(int)except converts the elements to the specified types.getLong(int index) Shortcut for callingget(index, Long..class )getMap(int index) Shortcut for callingget(index, JsonMap..class )<K,V> Map<K, V> Same asgetMap(int)except converts the keys and values to the specified types.getString(int index) Shortcut for callingget(index, String..class )booleanReturnstrue if this list is unmodifiable.Returns a modifiable copy of this list if it's unmodifiable.static JsonListConstruct a list initialized with the specified values.static JsonListof(Collection<?> values) Construct a list initialized with the specified list.static JsonListConvenience method for creating a list of array objects.static JsonListofCollections(Collection<?>... values) Convenience method for creating a list of collection objects.static JsonListConstruct a list initialized with the specified reader containing JSON.static JsonListofJson(CharSequence json) Construct a list initialized with the specified JSON string.static JsonListParses a string that can consist of either a JSON array or comma-delimited list.static JsonListConstruct a list initialized with the specified string.static JsonListofText(CharSequence in, Parser p) Construct a list initialized with the specified string.Similar toputAt(String,Object), but used to append to collections and arrays.Same asset(int,Object), but the key is a slash-delimited path used to traverse entries in this POJO.session(BeanSession session) Override the default bean session used for converting POJOs.setBeanSession(BeanSession value) Sets theBeanSessioncurrently associated with this list.toString()Returns an unmodifiable copy of this list if it's modifiable.Convenience method for serializing this JsonList to the specified Writer using the JsonSerializer.DEFAULT serializer.Methods inherited from class java.util.LinkedListadd, add, addAll, addAll, addFirst, addLast, clear, clone, contains, descendingIterator, element, get, getFirst, getLast, indexOf, lastIndexOf, listIterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, set, size, spliterator, toArray, toArrayMethods inherited from class java.util.AbstractSequentialListiteratorMethods inherited from class java.util.AbstractListequals, hashCode, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollectioncontainsAll, isEmpty, removeAll, retainAllMethods inherited from class java.lang.Objectfinalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.CollectionparallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.ListcontainsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList
- 
Field Details- 
EMPTY_LISTAn empty read-only JsonList.
 
- 
- 
Constructor Details- 
JsonListpublic JsonList()Construct an empty list.
- 
JsonListConstruct an empty list with the specified bean context.- Parameters:
- session- The bean session to use for creating beans.
 
- 
JsonListConstruct a list initialized with the specified list.- Parameters:
- copyFrom- The list to copy.
 Can be- null .
 
- 
JsonListConstruct a list initialized with the specified JSON.- Parameters:
- json- The JSON text to parse.
 Can be normal or simplified JSON.
- Throws:
- ParseException- Malformed input encountered.
 
- 
JsonListConstruct a list initialized with the specified string.- Parameters:
- in- The input being parsed.
 Can be- null .
- p- The parser to use to parse the input.
 If- null , uses- JsonParser.
- Throws:
- ParseException- Malformed input encountered.
 
- 
JsonListConstruct a list initialized with the specified reader containing JSON.- Parameters:
- json- The reader containing JSON text to parse.
 Can contain normal or simplified JSON.
- Throws:
- ParseException- Malformed input encountered.
 
- 
JsonListConstruct a list initialized with the specified string.- Parameters:
- in- The reader containing the input being parsed.
 Can contain normal or simplified JSON.
- p- The parser to use to parse the input.
 If- null , uses- JsonParser.
- Throws:
- ParseException- Malformed input encountered.
 
- 
JsonListConstruct a list initialized with the contents.- Parameters:
- entries- The entries to add to this list.
 
 
- 
- 
Method Details- 
ofJsonOrCdlParses a string that can consist of either a JSON array or comma-delimited list.The type of string is auto-detected. - Parameters:
- s- The string to parse.
- Returns:
- The parsed string.
- Throws:
- ParseException- Malformed input encountered.
 
- 
createConstruct an empty list.- Returns:
- An empty list.
 
- 
ofConstruct a list initialized with the specified list.- Parameters:
- values- The list to copy.
 Can be- null .
- Returns:
- A new list or null if the list wasnull .
 
- 
ofCollectionsConvenience method for creating a list of collection objects.- Parameters:
- values- The initial values.
- Returns:
- A new list.
 
- 
ofArraysConvenience method for creating a list of array objects.- Parameters:
- values- The initial values.
- Returns:
- A new list.
 
- 
ofJsonConstruct a list initialized with the specified JSON string.- Parameters:
- json- The JSON text to parse.
 Can be normal or simplified JSON.
- Returns:
- A new list or null if the string was null.
- Throws:
- ParseException- Malformed input encountered.
 
- 
ofTextConstruct a list initialized with the specified string.- Parameters:
- in- The input being parsed.
 Can be- null .
- p- The parser to use to parse the input.
 If- null , uses- JsonParser.
- Returns:
- A new list or null if the input wasnull .
- Throws:
- ParseException- Malformed input encountered.
 
- 
ofJsonConstruct a list initialized with the specified reader containing JSON.- Parameters:
- json- The reader containing JSON text to parse.
 Can contain normal or simplified JSON.
- Returns:
- A new list or null if the input wasnull .
- Throws:
- ParseException- Malformed input encountered.
 
- 
ofTextConstruct a list initialized with the specified string.- Parameters:
- in- The reader containing the input being parsed.
 Can contain normal or simplified JSON.
- p- The parser to use to parse the input.
 If- null , uses- JsonParser.
- Returns:
- A new list or null if the input wasnull .
- Throws:
- ParseException- Malformed input encountered.
 
- 
ofConstruct a list initialized with the specified values.- Parameters:
- values- The values to add to this list.
- Returns:
- A new list, never null .
 
- 
sessionOverride the default bean session used for converting POJOs.Default is BeanContext.DEFAULT, which is sufficient in most cases.Useful if you're serializing/parsing beans with transforms defined. - Parameters:
- session- The new bean session.
- Returns:
- This object.
 
- 
appendAdds the value to this list.- Parameters:
- value- The value to add to this list.
- Returns:
- This object.
 
- 
appendAdds all the values in the specified array to this list.- Parameters:
- values- The values to add to this list.
- Returns:
- This object.
 
- 
appendAdds all the values in the specified collection to this list.- Parameters:
- values- The values to add to this list.
- Returns:
- This object.
 
- 
appendIfAdds an entry to this list if the boolean flag istrue .- Parameters:
- flag- The boolean flag.
- value- The value to add.
- Returns:
- This object.
 
- 
appendReverseAdds all the entries in the specified collection to this list in reverse order.- Parameters:
- values- The collection to add to this list.
- Returns:
- This object.
 
- 
appendReverseAdds the contents of the array to the list in reverse order.i.e. add values from the array from end-to-start order to the end of the list. - Parameters:
- values- The collection to add to this list.
- Returns:
- This object.
 
- 
appendIfAdd if predicate matches.- Type Parameters:
- T- The type being tested.
- Parameters:
- test- The predicate to match against.
- value- The value to add if the predicate matches.
- Returns:
- This object.
 
- 
getGet the entry at the specified index, converted to the specified type.This is the preferred get method for simple types. Examples:JsonList list = JsonList.ofJson ("..." );// Value converted to a string. Stringstring =list .get(1, String.class );// Value converted to a bean. MyBeanbean =list .get(2, MyBean.class );// Value converted to a bean array. MyBean[]beanArray =list .get(3, MyBean[].class );// Value converted to a linked-list of objects. Listlist2 =list .get(4, LinkedList.class );// Value converted to a map of object keys/values. Mapmap =list .get(5, TreeMap.class );See BeanSession.convertToType(Object, ClassMeta)for the list of valid data conversions.- Type Parameters:
- T- The type of object to convert the entry to.
- Parameters:
- index- The index into this list.
- type- The type of object to convert the entry to.
- Returns:
- The converted entry.
 
- 
getGet the entry at the specified index, converted to the specified type.The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps). Examples:JsonList list = JsonList.ofJson ("..." );// Value converted to a linked-list of strings. List<String>list1 =list .get(1, LinkedList.class , String.class );// Value converted to a linked-list of beans. List<MyBean>list2 =list .get(2, LinkedList.class , MyBean.class );// Value converted to a linked-list of linked-lists of strings. List<List<String>>list3 =list .get(3, LinkedList.class , LinkedList.class , String.class );// Value converted to a map of string keys/values. Map<String,String>map1 =list .get(4, TreeMap.class , String.class , String.class );// Value converted to a map containing string keys and values of lists containing beans. Map<String,List<MyBean>>map2 =list .get(5, TreeMap.class , String.class , List.class , MyBean.class );Collection classes are assumed to be followed by zero or one objects indicating the element type.Map classes are assumed to be followed by zero or two meta objects indicating the key and value types.The array can be arbitrarily long to indicate arbitrarily complex data structures. See BeanSession.convertToType(Object, ClassMeta)for the list of valid data conversions.- Type Parameters:
- T- The type of object to convert the entry to.
- Parameters:
- index- The index into this list.
- type- The type of object to convert the entry to.
- args- The type arguments of the type to convert the entry to.
- Returns:
- The converted entry.
 
- 
getStringShortcut for callingget(index, String..class )- Parameters:
- index- The index.
- Returns:
- The converted value.
 
- 
getIntShortcut for callingget(index, Integer..class )- Parameters:
- index- The index.
- Returns:
- The converted value.
- Throws:
- InvalidDataConversionException- If value cannot be converted.
 
- 
getBooleanShortcut for callingget(index, Boolean..class )- Parameters:
- index- The index.
- Returns:
- The converted value.
- Throws:
- InvalidDataConversionException- If value cannot be converted.
 
- 
getLongShortcut for callingget(index, Long..class )- Parameters:
- index- The index.
- Returns:
- The converted value.
- Throws:
- InvalidDataConversionException- If value cannot be converted.
 
- 
getMapShortcut for callingget(index, JsonMap..class )- Parameters:
- index- The index.
- Returns:
- The converted value.
- Throws:
- InvalidDataConversionException- If value cannot be converted.
 
- 
getMapSame asgetMap(int)except converts the keys and values to the specified types.- Type Parameters:
- K- The key type class.
- V- The value type class.
- Parameters:
- index- The index.
- keyType- The key type class.
- valType- The value type class.
- Returns:
- The converted value.
- Throws:
- InvalidDataConversionException- If value cannot be converted.
 
- 
getListShortcut for callingget(index, JsonList..class )- Parameters:
- index- The index.
- Returns:
- The converted value.
- Throws:
- InvalidDataConversionException- If value cannot be converted.
 
- 
getListSame asgetList(int)except converts the elements to the specified types.- Type Parameters:
- E- The element type.
- Parameters:
- index- The index.
- elementType- The element type class.
- Returns:
- The converted value.
- Throws:
- InvalidDataConversionException- If value cannot be converted.
 
- 
getAtSame asget(int,Class), but the key is a slash-delimited path used to traverse entries in this POJO.For example, the following code is equivalent: JsonList list = JsonList.ofJson ("..." );// Long way long long1 =list .getMap("0" ).getLong("baz" );// Using this method long long2 =list .getAt("0/baz" ,long .class );This method uses the ObjectRestclass to perform the lookup, so the map can contain any of the various class types that theObjectRestclass supports (e.g. beans, collections, arrays).- Type Parameters:
- T- The class type.
- Parameters:
- path- The path to the entry.
- type- The class type.
- Returns:
- The value, or null if the entry doesn't exist.
 
- 
getAtSame asgetAt(String,Class), but allows for conversion to complex maps and collections.- Type Parameters:
- T- The class type.
- Parameters:
- path- The path to the entry.
- type- The class type.
- args- The class parameter types.
- Returns:
- The value, or null if the entry doesn't exist.
 
- 
putAtSame asset(int,Object), but the key is a slash-delimited path used to traverse entries in this POJO.For example, the following code is equivalent: JsonList list = JsonList.ofJson ("..." );// Long way list .getMap("0" ).put("baz" , 123);// Using this method list .putAt("0/baz" , 123);This method uses the ObjectRestclass to perform the lookup, so the map can contain any of the various class types that theObjectRestclass supports (e.g. beans, collections, arrays).- Parameters:
- path- The path to the entry.
- o- The new value.
- Returns:
- The previous value, or null if the entry doesn't exist.
 
- 
postAtSimilar toputAt(String,Object), but used to append to collections and arrays.For example, the following code is equivalent: JsonList list = JsonList.ofJson ("..." );// Long way list .getMap(0).getList("bar" ).append(123);// Using this method list .postAt("0/bar" , 123);This method uses the ObjectRestclass to perform the lookup, so the map can contain any of the various class types that theObjectRestclass supports (e.g. beans, collections, arrays).- Parameters:
- path- The path to the entry.
- o- The new value.
- Returns:
- The previous value, or null if the entry doesn't exist.
 
- 
deleteAtSimilar toremove(int),but the key is a slash-delimited path used to traverse entries in this POJO.For example, the following code is equivalent: JsonList list = JsonList.ofJson ("..." );// Long way list .getMap(0).getList("bar" ).delete(0);// Using this method list .deleteAt("0/bar/0" );This method uses the ObjectRestclass to perform the lookup, so the map can contain any of the various class types that theObjectRestclass supports (e.g. beans, collections, arrays).- Parameters:
- path- The path to the entry.
- Returns:
- The previous value, or null if the entry doesn't exist.
 
- 
getBeanSessionReturns theBeanSessioncurrently associated with this list.- Returns:
- The BeanSessioncurrently associated with this list.
 
- 
setBeanSessionSets theBeanSessioncurrently associated with this list.- Parameters:
- value- The- BeanSessioncurrently associated with this list.
- Returns:
- This object.
 
- 
elementsCreates anIterablewith elements of the specified child type.Attempts to convert the child objects to the correct type if they aren't already the correct type. The next() method on the returned iterator may throw aInvalidDataConversionExceptionif the next element cannot be converted to the specified type.See BeanSession.convertToType(Object, ClassMeta)for a description of valid conversions.Example:// Iterate over a list of JsonMaps. JsonListlist = JsonList.ofJson ("[{foo:'bar'},{baz:123}]" );for (JsonMapmap :list .elements(JsonMap.class )) {// Do something with map. }// Iterate over a list of ints. JsonListlist = JsonList.ofJson ("[1,2,3]" );for (Integeri :list .elements(Integer.class )) {// Do something with i. }// Iterate over a list of beans. // Automatically converts to beans. JsonListlist = JsonList.ofJson ("[{name:'John Smith',age:45}]" );for (Personp :list .elements(Person.class )) {// Do something with p. }- Type Parameters:
- E- The child object type.
- Parameters:
- childType- The child object type.
- Returns:
- A new Iterable object over this list.
 
- 
getClassMetaReturns theClassMetaof the class of the object at the specified index.- Parameters:
- index- An index into this list, zero-based.
- Returns:
- The data type of the object at the specified index, or null if the value is null.
 
- 
asStringSerialize this array to a string using the specified serializer.- Parameters:
- serializer- The serializer to use to convert this object to a string.
- Returns:
- This object as a serialized string.
 
- 
asStringSerialize this array to Simplified JSON.- Returns:
- This object as a serialized string.
 
- 
isUnmodifiableReturnstrue if this list is unmodifiable.- Returns:
- true if this list is unmodifiable.
 
- 
modifiableReturns a modifiable copy of this list if it's unmodifiable.- Returns:
- A modifiable copy of this list if it's unmodifiable, or this list if it is already modifiable.
 
- 
unmodifiableReturns an unmodifiable copy of this list if it's modifiable.- Returns:
- An unmodifiable copy of this list if it's modifiable, or this list if it is already unmodifiable.
 
- 
writeToConvenience method for serializing this JsonList to the specified Writer using the JsonSerializer.DEFAULT serializer.- Parameters:
- w- The writer to send the serialized contents of this object.
- Returns:
- This object.
- Throws:
- IOException- If a problem occurred trying to write to the writer.
- SerializeException- If a problem occurred trying to convert the output.
 
- 
castConverts this object into the specified class type.TODO - The current implementation is very inefficient. - Parameters:
- cm- The class type to convert this object to.
- Returns:
- A converted object.
 
- 
asJsonA synonym fortoString()- Returns:
- This object as a JSON string.
 
- 
toString- Overrides:
- toStringin class- AbstractCollection<Object>
 
 
-