Class JsonList
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<Object>,Collection<Object>,Deque<Object>,List<Object>,Queue<Object>
- Direct Known Subclasses:
DelegateList
This class is a subclass of MarshalledList that carries strict-JSON-specific surface, including
strict-JSON toString() (via Json.of(Object)), JsonParser-defaulting
CharSequence / Reader constructors, and convenience methods for serializing to the
various JSON dialects (JSON5, JSON Lines, canonical JSON, HJSON).
v10.0 behavioral break: toString() now produces strict (RFC 8259) JSON (was JSON5),
and the CharSequence / Reader constructors now default to JsonParser.DEFAULT
(was Json5Parser.DEFAULT). For the previous JSON5 behavior use Json5List.
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.
-
Field Summary
FieldsFields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionJsonList()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(MarshallingSession 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> 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.Converts this object into the specified class type.static JsonListcreate()Construct an empty list.getList(int index) Shortcut for callingget(index, MarshalledList..class )getMap(int index) Shortcut for callingget(index, MarshalledMap..class )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 JsonListParses a string that can consist of either a JSON array or comma-delimited list.static JsonListConstruct a list initialized with the specified reader containing JSON.static JsonListConstruct a list initialized with the specified string.static JsonListofString(CharSequence json) Construct a list initialized with the specified JSON string.static JsonListofString(CharSequence in, Parser p) Construct a list initialized with the specified string.session(MarshallingSession session) Override the default bean session used for converting POJOs.setBeanSession(MarshallingSession value) Sets theMarshallingSessioncurrently associated with this list.toHjson()Serializes this list to an HJSON string.toJcs()Serializes this list to a canonical JSON string (RFC 8785).toJson()Serializes this list to a standard JSON string.toJson5()Serializes this list to a JSON5 string.toJsonl()Serializes this list to a JSON Lines string.toString()Returns an unmodifiable copy of this list if it's modifiable.Convenience method for serializing this JsonList to the specified Writer using theJson.DEFAULTmarshaller.Methods inherited from class org.apache.juneau.marshall.collections.MarshalledList
at, bs, deleteAt, elements, get, get, getAt, getAt, getBoolean, getClassMeta, getInt, getList, getLong, getMap, getMarshallingSession, getString, isUnmodifiable, postAt, putAt, toStringMethods 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, toArrayMethods inherited from class java.util.AbstractSequentialList
iteratorMethods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
containsAll, isEmpty, removeAll, retainAllMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods 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 JSON.- Parameters:
json- The JSON text to parse.
Must be strict (RFC 8259) JSON; for JSON5 input useJson5Listor passJson5Parser.DEFAULTexplicitly.- Throws:
ParseException- Malformed input encountered.
-
JsonList
Construct a list initialized with the specified string.- Parameters:
in- The input being parsed.
Can benull or empty (results in an empty list; no parsing occurs).p- The parser to use to parse the input.
Ifnull , usesJsonParser.- Throws:
ParseException- Malformed input encountered.
-
JsonList
Construct a list initialized with the specified list.- Parameters:
copyFrom- The list to copy.
Can benull (results in an empty list).
-
JsonList
Construct a list initialized with the contents.- Parameters:
entries- The entries to add to this list.
-
JsonList
Construct a list initialized with the specified reader containing JSON.- Parameters:
json- The reader containing JSON text to parse.
Must be strict (RFC 8259) JSON; for JSON5 input useJson5Listor passJson5Parser.DEFAULTexplicitly.- Throws:
ParseException- Malformed input encountered.
-
JsonList
Construct a list initialized with the specified string.- Parameters:
in- The reader containing the input being parsed.
Must be strict (RFC 8259) JSON.p- The parser to use to parse the input.
Ifnull , usesJsonParser.- Throws:
ParseException- Malformed input encountered.
-
-
Method Details
-
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 (returnsnull ).- Returns:
- A new list or
null if the list wasnull .
-
of
Construct a list initialized with the specified values.- Parameters:
values- The values to add to this list.- Returns:
- A new list, never
null .
-
ofArrays
Convenience method for creating a list of array objects.- Parameters:
values- The initial values.- Returns:
- A new list.
-
ofCollections
Convenience method for creating a list of collection objects.- Parameters:
values- The initial values.- Returns:
- A new list.
-
ofString
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 (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
ofString
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 (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
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:
- A new list (empty if the input was
null or empty), nevernull . - Throws:
ParseException- Malformed input encountered.
-
ofString
Construct a list initialized with the specified string.- Parameters:
in- The input being parsed.p- The parser to use to parse the input.
Ifnull , usesJsonParser.- Returns:
- A new list (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
ofString
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 (empty if the input was
null ), nevernull . - Throws:
ParseException- Malformed input encountered.
-
append
Description copied from class:MarshalledListAdds all the values in the specified collection to this list.- Overrides:
appendin classMarshalledList- Parameters:
values- The values to add to this list.- Returns:
- This object.
-
append
Description copied from class:MarshalledListAdds the value to this list.- Overrides:
appendin classMarshalledList- Parameters:
value- The value to add to this list.- Returns:
- This object.
-
append
Description copied from class:MarshalledListAdds all the values in the specified array to this list.- Overrides:
appendin classMarshalledList- Parameters:
values- The values to add to this list.- Returns:
- This object.
-
appendIf
Description copied from class:MarshalledListAdds an entry to this list if the boolean flag istrue .- Overrides:
appendIfin classMarshalledList- Parameters:
flag- The boolean flag.value- The value to add.- Returns:
- This object.
-
appendIf
Description copied from class:MarshalledListAdd if predicate matches.- Overrides:
appendIfin classMarshalledList- 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.
-
appendReverse
Description copied from class:MarshalledListAdds all the entries in the specified collection to this list in reverse order.- Overrides:
appendReversein classMarshalledList- Parameters:
values- The collection to add to this list.- Returns:
- This object.
-
appendReverse
Description copied from class:MarshalledListAdds 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.
- Overrides:
appendReversein classMarshalledList- Parameters:
values- The collection to add to this list.- Returns:
- This object.
-
toJson
Serializes this list to a standard JSON string.- Returns:
- This object as a standard JSON string.
-
toJson5
Serializes this list to a JSON5 string.A synonym for
toString().- Returns:
- This object as a JSON5 string.
-
toJsonl
Serializes this list to a JSON Lines string.- Returns:
- This object as a JSON Lines string.
-
toJcs
Serializes this list to a canonical JSON string (RFC 8785).- Returns:
- This object as a canonical JSON string.
-
toHjson
Serializes this list to an HJSON string.- Returns:
- This object as an HJSON string.
-
cast
Converts this object into the specified class type.This method performs a round-trip serialization and deserialization to convert the list into the target type.
Notes:
- The current implementation uses a serialization round-trip which may be inefficient for frequent conversions of large objects. Consider caching results or using direct object conversion where possible.
- Parameters:
cm- The class type to convert this object to.- Returns:
- A converted object.
-
getList
Description copied from class:MarshalledListShortcut for callingget(index, MarshalledList..class )- Overrides:
getListin classMarshalledList- Parameters:
index- The index.- Returns:
- The converted value.
-
getMap
Description copied from class:MarshalledListShortcut for callingget(index, MarshalledMap..class )- Overrides:
getMapin classMarshalledList- Parameters:
index- The index.- Returns:
- The converted value.
-
modifiable
Description copied from class:MarshalledListReturns a modifiable copy of this list if it's unmodifiable.- Overrides:
modifiablein classMarshalledList- Returns:
- A modifiable copy of this list if it's unmodifiable, or this list if it is already modifiable.
-
session
Description copied from class:MarshalledListOverride the default bean session used for converting POJOs.Default is
MarshallingContext.DEFAULT, which is sufficient in most cases.Useful if you're serializing/parsing beans with transforms defined.
- Overrides:
sessionin classMarshalledList- Parameters:
session- The new bean session.- Returns:
- This object.
-
setBeanSession
Description copied from class:MarshalledListSets theMarshallingSessioncurrently associated with this list.- Overrides:
setBeanSessionin classMarshalledList- Parameters:
value- TheMarshallingSessioncurrently associated with this list.- Returns:
- This object.
-
toString
- Overrides:
toStringin classAbstractCollection<Object>
-
unmodifiable
Description copied from class:MarshalledListReturns an unmodifiable copy of this list if it's modifiable.- Overrides:
unmodifiablein classMarshalledList- 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 theJson.DEFAULTmarshaller.- 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.
-