public class ObjectList extends LinkedList<Object>
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.
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.
This class is not thread safe.
Modifier and Type | Field and Description |
---|---|
static ObjectList |
EMPTY_LIST
An empty read-only ObjectList.
|
modCount
Constructor and Description |
---|
ObjectList()
Construct an empty JSON array.
|
ObjectList(BeanSession session)
Construct an empty JSON array with the specified bean context.
|
ObjectList(CharSequence s)
Shortcut for
|
ObjectList(CharSequence s,
Parser p)
Construct a JSON array directly from text using the specified parser.
|
ObjectList(Collection<?> c)
Construct a JSON array and fill it with the specified collection of objects.
|
ObjectList(Object... o)
Construct a JSON array and fill it with the specified objects.
|
ObjectList(Reader r)
Shortcut for
. |
ObjectList(Reader r,
Parser p)
Construct a JSON array directly from a reader using the specified parser.
|
Modifier and Type | Method and Description |
---|---|
ObjectList |
append(Object... o)
Convenience method for adding multiple objects to this list.
|
Object |
deleteAt(String path)
Similar to
remove(int) ,but the key is a slash-delimited path used to traverse entries in
this POJO. |
<E> Iterable<E> |
elements(Class<E> childType)
Creates an
Iterable with elements of the specified child type. |
<T> T |
get(int index,
Class<T> type)
Get the entry at the specified index, converted to the specified type.
|
<T> T |
get(int index,
Type type,
Type... args)
Get the entry at the specified index, converted to the specified type.
|
<T> T |
getAt(String path,
Class<T> type)
Same as
get(int,Class) , but the key is a slash-delimited path used to traverse entries in
this POJO. |
<T> T |
getAt(String path,
Type type,
Type... args)
Same as
getAt(String,Class) , but allows for conversion to complex maps and collections. |
Boolean |
getBoolean(int index)
Shortcut for calling
get(index, Boolean. . |
ClassMeta<?> |
getClassMeta(int index)
Returns the
ClassMeta of the class of the object at the specified index. |
Integer |
getInt(int index)
Shortcut for calling
get(index, Integer. . |
List<?> |
getList(int index)
Shortcut for calling
get(index, List. . |
<E> List<E> |
getList(int index,
Class<E> elementType)
Same as
getList(int) except converts the elements to the specified types. |
Long |
getLong(int index)
Shortcut for calling
get(index, Long. . |
Map<?,?> |
getMap(int index)
Shortcut for calling
get(index, Map. . |
<K,V> Map<K,V> |
getMap(int index,
Class<K> keyType,
Class<V> valType)
Same as
getMap(int) except converts the keys and values to the specified types. |
ObjectList |
getObjectList(int index)
Shortcut for calling
get(index, ObjectList. . |
ObjectMap |
getObjectMap(int index)
Shortcut for calling
get(index, ObjectMap. . |
String |
getString(int index)
Shortcut for calling
get(index, String. . |
Object |
postAt(String path,
Object o)
Similar to
putAt(String,Object) , but used to append to collections and arrays. |
Object |
putAt(String path,
Object o)
Same as
set(int,Object) , but the key is a slash-delimited path used to traverse entries
in this POJO. |
void |
serializeTo(Writer w)
Convenience method for serializing this ObjectList to the specified Writer using the JsonSerializer.DEFAULT
serializer.
|
ObjectList |
setBeanSession(BeanSession session)
Override the default bean session used for converting POJOs.
|
String |
toString()
Serialize this array to JSON using the
JsonSerializer.DEFAULT serializer. |
String |
toString(WriterSerializer serializer)
Serialize this array to a string using the specified serializer.
|
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
iterator
equals, hashCode, listIterator, removeRange, subList
containsAll, isEmpty, removeAll, retainAll
finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList
parallelStream, removeIf, stream
public static final ObjectList EMPTY_LIST
public ObjectList(CharSequence s, Parser p) throws ParseException
s
- The string being parsed.p
- The parser to use to parse the input.ParseException
- If the input contains a syntax error or is malformed.public ObjectList(CharSequence s) throws ParseException
new ObjectList(String,JsonParser.DEFAULT );
s
- The string being parsed.ParseException
- If the input contains a syntax error or is malformed.public ObjectList(Reader r, Parser p) throws ParseException, IOException
r
- The reader to read from.
Will automatically be wrapped in a BufferedReader
if it isn't already a BufferedReader.p
- The parser to use to parse the input.ParseException
- If the input contains a syntax error or is malformed.IOException
- If a problem occurred trying to read from the reader.public ObjectList(Reader r) throws ParseException, IOException
new ObjectList(reader, JsonParser.DEFAULT )
.r
- The reader to read from.
The reader will be wrapped in a BufferedReader
if it isn't already.ParseException
- If the input contains a syntax error or is malformed.IOException
- If a problem occurred trying to read from the reader.public ObjectList()
LinkedList
).public ObjectList(BeanSession session)
LinkedList
).session
- The bean context to associate with this object list for creating beans.public ObjectList(Object... o)
o
- A list of objects to add to this list.public ObjectList(Collection<?> c)
c
- A list of objects to add to this list.public ObjectList setBeanSession(BeanSession session)
Default is BeanContext.DEFAULT
, which is sufficient in most cases.
Useful if you're serializing/parsing beans with transforms defined.
session
- The new bean session.public ObjectList append(Object... o)
o
- The objects to add to the list.public <T> T get(int index, Class<T> type)
This is the preferred get method for simple types.
ObjectList l =
See BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.
T
- The type of object to convert the entry to.index
- The index into this list.type
- The type of object to convert the entry to.public <T> T get(int index, Type type, Type... args)
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
ObjectList l =
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.
T
- The type of object to convert the entry to.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.public String getString(int index)
get(index, String.class )
.index
- The index.public Integer getInt(int index)
get(index, Integer.class )
.index
- The index.InvalidDataConversionException
- If value cannot be converted.public Boolean getBoolean(int index)
get(index, Boolean.class )
.index
- The index.InvalidDataConversionException
- If value cannot be converted.public Long getLong(int index)
get(index, Long.class )
.index
- The index.InvalidDataConversionException
- If value cannot be converted.public Map<?,?> getMap(int index)
get(index, Map.class )
.index
- The index.InvalidDataConversionException
- If value cannot be converted.public <K,V> Map<K,V> getMap(int index, Class<K> keyType, Class<V> valType)
getMap(int)
except converts the keys and values to the specified types.index
- The index.keyType
- The key type class.valType
- The value type class.InvalidDataConversionException
- If value cannot be converted.public List<?> getList(int index)
get(index, List.class )
.index
- The index.InvalidDataConversionException
- If value cannot be converted.public <E> List<E> getList(int index, Class<E> elementType)
getList(int)
except converts the elements to the specified types.index
- The index.elementType
- The element type class.InvalidDataConversionException
- If value cannot be converted.public ObjectMap getObjectMap(int index)
get(index, ObjectMap.class )
.index
- The index.InvalidDataConversionException
- If value cannot be converted.public ObjectList getObjectList(int index)
get(index, ObjectList.class )
.index
- The index.InvalidDataConversionException
- If value cannot be converted.public <T> T getAt(String path, Class<T> type)
get(int,Class)
, but the key is a slash-delimited path used to traverse entries in
this POJO.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
T
- The class type.path
- The path to the entry.type
- The class type.public <T> T getAt(String path, Type type, Type... args)
getAt(String,Class)
, but allows for conversion to complex maps and collections.T
- The class type.path
- The path to the entry.type
- The class type.args
- The class parameter types.public Object putAt(String path, Object o)
set(int,Object)
, but the key is a slash-delimited path used to traverse entries
in this POJO.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
path
- The path to the entry.o
- The new value.public Object postAt(String path, Object o)
putAt(String,Object)
, but used to append to collections and arrays.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
path
- The path to the entry.o
- The new value.public Object deleteAt(String path)
remove(int)
,but the key is a slash-delimited path used to traverse entries in
this POJO.
For example, the following code is equivalent:
ObjectMap m = getObjectMap();
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
path
- The path to the entry.public <E> Iterable<E> elements(Class<E> childType)
Iterable
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 a InvalidDataConversionException
if
the next element cannot be converted to the specified type.
See BeanSession.convertToType(Object, ClassMeta)
for a description of valid conversions.
E
- The child object type.childType
- The child object type.Iterable
object over this list.public ClassMeta<?> getClassMeta(int index)
ClassMeta
of the class of the object at the specified index.index
- An index into this list, zero-based.public String toString(WriterSerializer serializer) throws SerializeException
serializer
- The serializer to use to convert this object to a string.SerializeException
- If a problem occurred trying to convert the output.public String toString()
JsonSerializer.DEFAULT
serializer.toString
in class AbstractCollection<Object>
public void serializeTo(Writer w) throws IOException, SerializeException
w
- The writer to send the serialized contents of this object.IOException
- If a problem occurred trying to write to the writer.SerializeException
- If a problem occurred trying to convert the output.Copyright © 2018 Apache. All rights reserved.