Class JsArraySpecParser

java.lang.Object
jsonvalues.spec.JsArraySpecParser

public final class JsArraySpecParser extends Object
The JsArraySpecParser class is responsible for creating JSON array parsers based on provided JSON specifications (specs). It allows you to define a schema for JSON arrays using a specification and then use that schema to parse JSON array data into structured Java objects. This class is part of the JSONValues library, which focuses on providing a type-safe and functional approach to JSON parsing and manipulation.

A parser, in the context of this class, refers to an instance of JsParser that encapsulates the rules and constraints defined in a JSON specification. The parser ensures that the JSON array data being parsed conforms to the specified schema, enforcing rules such as element data types, array length, and user-defined conditions.

Usage of this class typically involves creating an instance with a JSON specification and then using that instance to parse JSON array data. If the JSON array data does not adhere to the specified schema, a JsParserException will be raised, providing detailed information about the parsing code.

This class provides three main methods for parsing JSON array data:

  • parse(byte[] bytes): Parses a byte array representing JSON array data into a JSON array.
  • parse(String str): Parses a JSON array string into a JSON array.
  • parse(InputStream inputStream): Parses JSON array data from an input stream into a JSON array. This method also handles potential I/O exceptions when reading from the stream.

It's important to note that the provided JSON specification should match the structure and constraints of the JSON array data you expect to parse. The parser will enforce these constraints during parsing.

  • Method Details

    • of

      public static JsArraySpecParser of(JsSpec spec)
      Creates a JSON array parser based on the provided JSON array specification (spec). The parser will validate that every element in a JSON array adheres to the schema defined in the given specification.
      Parameters:
      spec - The JSON array specification that defines the expected schema for each element in the array.
      Returns:
      a Json array parser
    • parse

      public JsArray parse(byte[] bytes)
      Parses an array of bytes representing JSON array data into a structured JSON array. The parsed JSON array must conform to the schema defined in the associated JSON specification (spec). If the input bytes do not represent a well-formed JSON array or if the parsed array does not adhere to the specified schema, a JsParserException is thrown.
      Parameters:
      bytes - An array of bytes containing JSON array data.
      Returns:
      The parsed JSON array if parsing is successful.
      Throws:
      JsParserException - If parsing fails due to JSON syntax errors or specification violations.
    • parse

      public JsArray parse(String str)
      Parses a JSON array string into a structured JSON array. The parsed JSON array must conform to the schema defined in the associated JSON specification (spec). If the input string does not represent a well-formed JSON array or if the parsed array does not adhere to the specified schema, a JsParserException is thrown.
      Parameters:
      str - A string containing JSON array data.
      Returns:
      The parsed JSON array if parsing is successful.
      Throws:
      JsParserException - If parsing fails due to JSON syntax errors or specification violations.
    • parse

      public JsArray parse(InputStream inputstream)
      Parses JSON array data from an input stream into a structured JSON array. The parsed JSON array must conform to the schema defined in the associated JSON specification (spec). If the input stream does not contain a well-formed JSON array or if the parsed array does not adhere to the specified schema, a JsParserException is thrown. Any I/O exceptions encountered while reading from the input stream are also captured and wrapped in the thrown exception.
      Parameters:
      inputstream - An input stream containing JSON array data.
      Returns:
      The parsed JSON array if parsing is successful.
      Throws:
      JsParserException - If parsing fails due to JSON syntax errors, specification violations, or I/O exceptions.