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 Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorDescriptionJsonList()
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 Summary
Modifier 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> JsonList
Add 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 JsonList
create()
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 anIterable
with elements of the specified child type.<T> T
Get the entry at the specified index, converted to the specified type.<T> T
Get the entry at the specified index, converted to the specified type.<T> T
Same asget(int,Class)
, but the key is a slash-delimited path used to traverse entries in this POJO.<T> T
Same asgetAt(String,Class)
, but allows for conversion to complex maps and collections.Returns theBeanSession
currently associated with this list.getBoolean
(int index) Shortcut for callingget(index, Boolean.
.class )ClassMeta<?>
getClassMeta
(int index) Returns theClassMeta
of 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 )boolean
Returnstrue if this list is unmodifiable.Returns a modifiable copy of this list if it's unmodifiable.static JsonList
Construct a list initialized with the specified values.static JsonList
of
(Collection<?> values) Construct a list initialized with the specified list.static JsonList
Convenience method for creating a list of array objects.static JsonList
ofCollections
(Collection<?>... values) Convenience method for creating a list of collection objects.static JsonList
Construct a list initialized with the specified reader containing JSON.static JsonList
ofJson
(CharSequence json) Construct a list initialized with the specified JSON string.static JsonList
Parses a string that can consist of either a JSON array or comma-delimited list.static JsonList
Construct a list initialized with the specified string.static JsonList
ofText
(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 theBeanSession
currently 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.LinkedList
add, 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, toArray
Methods inherited from class java.util.AbstractSequentialList
iterator
Methods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange, subList
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAll
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList
-
Field Details
-
EMPTY_LIST
An empty read-only JsonList.
-
-
Constructor Details
-
JsonList
public JsonList()Construct an empty list. -
JsonList
Construct an empty list with the specified bean context.- Parameters:
session
- The bean session to use for creating beans.
-
JsonList
Construct a list initialized with the specified list.- Parameters:
copyFrom
- The list to copy.
Can benull .
-
JsonList
Construct 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.
-
JsonList
Construct a list initialized with the specified string.- Parameters:
in
- The input being parsed.
Can benull .p
- The parser to use to parse the input.
Ifnull , usesJsonParser
.- Throws:
ParseException
- Malformed input encountered.
-
JsonList
Construct 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.
-
JsonList
Construct 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.
Ifnull , usesJsonParser
.- Throws:
ParseException
- Malformed input encountered.
-
JsonList
Construct a list initialized with the contents.- Parameters:
entries
- The entries to add to this list.
-
-
Method Details
-
ofJsonOrCdl
Parses 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.
-
create
Construct an empty list.- Returns:
- An empty list.
-
of
Construct a list initialized with the specified list.- Parameters:
values
- The list to copy.
Can benull .- Returns:
- A new list or
null if the list wasnull .
-
ofCollections
Convenience method for creating a list of collection objects.- Parameters:
values
- The initial values.- Returns:
- A new list.
-
ofArrays
Convenience method for creating a list of array objects.- Parameters:
values
- The initial values.- Returns:
- A new list.
-
ofJson
Construct 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.
-
ofText
Construct a list initialized with the specified string.- Parameters:
in
- The input being parsed.
Can benull .p
- The parser to use to parse the input.
Ifnull , usesJsonParser
.- Returns:
- A new list or
null if the input wasnull . - Throws:
ParseException
- Malformed input encountered.
-
ofJson
Construct 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.
-
ofText
Construct 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.
Ifnull , usesJsonParser
.- Returns:
- A new list or
null if the input wasnull . - Throws:
ParseException
- Malformed input encountered.
-
of
Construct a list initialized with the specified values.- Parameters:
values
- The values to add to this list.- Returns:
- A new list, never
null .
-
session
Override 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.
-
append
Adds the value to this list.- Parameters:
value
- The value to add to this list.- Returns:
- This object.
-
append
Adds all the values in the specified array to this list.- Parameters:
values
- The values to add to this list.- Returns:
- This object.
-
append
Adds all the values in the specified collection to this list.- Parameters:
values
- The values to add to this list.- Returns:
- This object.
-
appendIf
Adds an entry to this list if the boolean flag istrue .- Parameters:
flag
- The boolean flag.value
- The value to add.- Returns:
- This object.
-
appendReverse
Adds 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.
-
appendReverse
Adds 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.
-
appendIf
Add 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.
-
get
Get 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.
-
get
Get 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.
-
getString
Shortcut for callingget(index, String.
.class )- Parameters:
index
- The index.- Returns:
- The converted value.
-
getInt
Shortcut for callingget(index, Integer.
.class )- Parameters:
index
- The index.- Returns:
- The converted value.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getBoolean
Shortcut for callingget(index, Boolean.
.class )- Parameters:
index
- The index.- Returns:
- The converted value.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getLong
Shortcut for callingget(index, Long.
.class )- Parameters:
index
- The index.- Returns:
- The converted value.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getMap
Shortcut for callingget(index, JsonMap.
.class )- Parameters:
index
- The index.- Returns:
- The converted value.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getMap
Same 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.
-
getList
Shortcut for callingget(index, JsonList.
.class )- Parameters:
index
- The index.- Returns:
- The converted value.
- Throws:
InvalidDataConversionException
- If value cannot be converted.
-
getList
Same 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.
-
getAt
Same 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
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class 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.
-
getAt
Same 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.
-
putAt
Same 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
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class 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.
-
postAt
Similar 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
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class 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.
-
deleteAt
Similar 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
ObjectRest
class to perform the lookup, so the map can contain any of the various class types that theObjectRest
class 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.
-
getBeanSession
Returns theBeanSession
currently associated with this list.- Returns:
- The
BeanSession
currently associated with this list.
-
setBeanSession
Sets theBeanSession
currently associated with this list.- Parameters:
value
- TheBeanSession
currently associated with this list.- Returns:
- This object.
-
elements
Creates anIterable
with 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 aInvalidDataConversionException
if 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.
-
getClassMeta
Returns theClassMeta
of 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.
-
asString
Serialize 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.
-
asString
Serialize this array to Simplified JSON.- Returns:
- This object as a serialized string.
-
isUnmodifiable
Returnstrue if this list is unmodifiable.- Returns:
true if this list is unmodifiable.
-
modifiable
Returns 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.
-
unmodifiable
Returns 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.
-
writeTo
Convenience 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.
-
cast
Converts 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.
-
asJson
A synonym fortoString()
- Returns:
- This object as a JSON string.
-
toString
- Overrides:
toString
in classAbstractCollection<Object>
-