Class Json5List

All Implemented Interfaces:
Serializable, Cloneable, Iterable<Object>, Collection<Object>, Deque<Object>, List<Object>, Queue<Object>

public class Json5List extends MarshalledList
JSON5-flavored MarshalledList.

Carries the JSON5-specific surface that historically lived on JsonList: a JSON5 toString(), JSON5 default-parser constructors, and convenience methods for working with JSON5 array text.

For strict (RFC 8259) JSON behavior, use JsonList. For a neutral, marshaller-agnostic base with no language coupling, use MarshalledList.

Example:

// Construct an empty list Json5List list = Json5List.of(); // Construct a list of generic Json5Map objects from JSON5 list = Json5List.ofString("[{foo:'bar'},{baz:'bing'}]"); // JSON5 round-trip via toString() String s = list.toString(); // "[{foo:'bar'},{baz:'bing'}]"

Notes:
  • This class is not thread safe.
  • Field Details

  • Constructor Details

    • Json5List

      public Json5List()
      Construct an empty list.
    • Json5List

      public Json5List(MarshallingSession session)
      Construct an empty list with the specified bean context.
      Parameters:
      session - The bean session to use for creating beans.
    • Json5List

      public Json5List(CharSequence json5) throws ParseException
      Construct a list initialized with the specified JSON5 text.
      Parameters:
      json5 - The JSON5 text to parse.
      Throws:
      ParseException - Malformed input encountered.
    • Json5List

      Construct a list initialized with the specified string.
      Parameters:
      in - The input being parsed. Can be null or empty (results in an empty list; no parsing occurs).
      p - The parser to use. If null, uses Json5Parser.
      Throws:
      ParseException - Malformed input encountered.
    • Json5List

      public Json5List(Collection<?> copyFrom)
      Construct a list initialized with the specified list.
      Parameters:
      copyFrom - The list to copy. Can be null (results in an empty list).
    • Json5List

      public Json5List(Object... entries)
      Construct a list initialized with the contents.
      Parameters:
      entries - The entries to add to this list.
    • Json5List

      public Json5List(Reader json5) throws ParseException
      Construct a list initialized with the specified reader containing JSON5.
      Parameters:
      json5 - The reader containing JSON5 text to parse.
      Throws:
      ParseException - Malformed input encountered.
    • Json5List

      public Json5List(Reader in, Parser p) throws ParseException
      Construct a list initialized with the specified reader and parser.
      Parameters:
      in - The reader containing the input being parsed.
      p - The parser to use. If null, uses Json5Parser.
      Throws:
      ParseException - Malformed input encountered.
  • Method Details

    • create

      public static Json5List create()
      Construct an empty list.
      Returns:
      An empty list.
    • of

      public static Json5List of(Collection<?> values)
      Construct a list initialized with the specified list.
      Parameters:
      values - The list to copy. Can be null (returns null).
      Returns:
      A new list or null if the list was null.
    • of

      public static Json5List of(Object... values)
      Construct a list initialized with the specified values.
      Parameters:
      values - The values to add to this list.
      Returns:
      A new list, never null.
    • ofArrays

      public static Json5List ofArrays(Object[]... values)
      Convenience method for creating a list of array objects.
      Parameters:
      values - The initial values.
      Returns:
      A new list.
    • ofCollections

      public static Json5List ofCollections(Collection<?>... values)
      Convenience method for creating a list of collection objects.
      Parameters:
      values - The initial values.
      Returns:
      A new list.
    • ofString

      public static Json5List ofString(CharSequence json5) throws ParseException
      Construct a list initialized with the specified JSON5 string.
      Parameters:
      json5 - The JSON5 text to parse.
      Returns:
      A new list (empty if the input was null), never null.
      Throws:
      ParseException - Malformed input encountered.
    • ofString

      public static Json5List ofString(Reader json5) throws ParseException
      Construct a list initialized with the specified reader containing JSON5.
      Parameters:
      json5 - The reader containing JSON5 text to parse.
      Returns:
      A new list (empty if the input was null), never null.
      Throws:
      ParseException - Malformed input encountered.
    • ofJson5OrCdl

      public static Json5List ofJson5OrCdl(String s) throws ParseException
      Parses a string that can consist of either a JSON5 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), never null.
      Throws:
      ParseException - Malformed input encountered.
    • ofString

      public static Json5List ofString(CharSequence in, Parser p) throws ParseException
      Construct a list initialized with the specified string.
      Parameters:
      in - The input being parsed.
      p - The parser to use. If null, uses Json5Parser.
      Returns:
      A new list (empty if the input was null), never null.
      Throws:
      ParseException - Malformed input encountered.
    • ofString

      public static Json5List ofString(Reader in, Parser p) throws ParseException
      Construct a list initialized with the specified reader.
      Parameters:
      in - The reader containing the input being parsed.
      p - The parser to use. If null, uses Json5Parser.
      Returns:
      A new list (empty if the input was null), never null.
      Throws:
      ParseException - Malformed input encountered.
    • append

      public Json5List append(Collection<?> values)
      Description copied from class: MarshalledList
      Adds all the values in the specified collection to this list.
      Overrides:
      append in class MarshalledList
      Parameters:
      values - The values to add to this list.
      Returns:
      This object.
    • append

      public Json5List append(Object value)
      Description copied from class: MarshalledList
      Adds the value to this list.
      Overrides:
      append in class MarshalledList
      Parameters:
      value - The value to add to this list.
      Returns:
      This object.
    • append

      public Json5List append(Object... values)
      Description copied from class: MarshalledList
      Adds all the values in the specified array to this list.
      Overrides:
      append in class MarshalledList
      Parameters:
      values - The values to add to this list.
      Returns:
      This object.
    • appendIf

      public Json5List appendIf(boolean flag, Object value)
      Description copied from class: MarshalledList
      Adds an entry to this list if the boolean flag is true.
      Overrides:
      appendIf in class MarshalledList
      Parameters:
      flag - The boolean flag.
      value - The value to add.
      Returns:
      This object.
    • appendIf

      public <T> Json5List appendIf(Predicate<T> test, T value)
      Description copied from class: MarshalledList
      Add if predicate matches.
      Overrides:
      appendIf in class MarshalledList
      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

      public Json5List appendReverse(List<?> values)
      Description copied from class: MarshalledList
      Adds all the entries in the specified collection to this list in reverse order.
      Overrides:
      appendReverse in class MarshalledList
      Parameters:
      values - The collection to add to this list.
      Returns:
      This object.
    • appendReverse

      public Json5List appendReverse(Object... values)
      Description copied from class: MarshalledList
      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.

      Overrides:
      appendReverse in class MarshalledList
      Parameters:
      values - The collection to add to this list.
      Returns:
      This object.
    • cast

      public Object cast(ClassMeta<?> cm)
      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

      public Json5List getList(int index)
      Description copied from class: MarshalledList
      Shortcut for calling get(index, MarshalledList.class).
      Overrides:
      getList in class MarshalledList
      Parameters:
      index - The index.
      Returns:
      The converted value.
    • getMap

      public Json5Map getMap(int index)
      Description copied from class: MarshalledList
      Shortcut for calling get(index, MarshalledMap.class).
      Overrides:
      getMap in class MarshalledList
      Parameters:
      index - The index.
      Returns:
      The converted value.
    • modifiable

      Description copied from class: MarshalledList
      Returns a modifiable copy of this list if it's unmodifiable.
      Overrides:
      modifiable in class MarshalledList
      Returns:
      A modifiable copy of this list if it's unmodifiable, or this list if it is already modifiable.
    • session

      Description copied from class: MarshalledList
      Override 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:
      session in class MarshalledList
      Parameters:
      session - The new bean session.
      Returns:
      This object.
    • setBeanSession

      Description copied from class: MarshalledList
      Sets the MarshallingSession currently associated with this list.
      Overrides:
      setBeanSession in class MarshalledList
      Parameters:
      value - The MarshallingSession currently associated with this list.
      Returns:
      This object.
    • toJson5

      public String toJson5()
      Serializes this list to a JSON5 string.

      A synonym for toString().

      Returns:
      This object as a JSON5 string.
    • toString

      public String toString()
      Overrides:
      toString in class AbstractCollection<Object>
    • unmodifiable

      Description copied from class: MarshalledList
      Returns an unmodifiable copy of this list if it's modifiable.
      Overrides:
      unmodifiable in class MarshalledList
      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 list to the specified Writer using the Json5.DEFAULT marshaller.
      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.