Interface JsSpec

All Known Subinterfaces:
JsArraySpec
All Known Implementing Classes:
JsObjSpec

public sealed interface JsSpec permits JsArraySpec, JsObjSpec (not exhaustive)
The `JsSpec` interface represents a specification for validating JSON data structures. It provides methods and contracts for defining validation rules and constraints on JSON values to ensure they conform to expected formats and patterns.

JSON specifications are essential for parsing and validating JSON data in applications, ensuring that the data adheres to predefined rules before processing or storing it.

Implementations of this interface define custom validation rules and provide methods for testing JSON values against these rules. The primary goal is to verify whether a given JSON value satisfies the specification.

The `JsSpec` interface offers the following key functionality: - Enabling the nullable flag, indicating whether the JSON value can be null. - Retrieving the deserializer used during the parsing process for parsing arrays of bytes or strings into JSON values. - Reading and parsing JSON values token by token from a reader while verifying if they conform to the specification. - Testing JSON values against the specification and returning a set of path/code pairs for validation errors.

This interface serves as a foundation for building a JSON validation framework and allows for the creation of custom JSON specifications for various data types, including numbers, strings, arrays, objects, and more.

Implementations of this interface should be immutable and thread-safe to support concurrent usage.

See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    Returns the same spec with the nullable flag enabled.
    default JsValue
    parse(String json)
    Low-level method to parse a JSON value from their string representation.
    jsonvalues.spec.JsParser
    Returns the deserializer used during the parsing process to parse an array of bytes or strings into a JSON value.
    test(JsPath parentPath, JsValue value)
    Verify if the given JSON value satisfies this spec.
    default List<SpecError>
    test(JsValue value)
    Verify if the given JSON value satisfies this spec, starting from the root path.
  • Method Details

    • nullable

      JsSpec nullable()
      Returns the same spec with the nullable flag enabled.
      Returns:
      A new `JsSpec` instance with the nullable flag enabled.
    • parser

      jsonvalues.spec.JsParser parser()
      Returns the deserializer used during the parsing process to parse an array of bytes or strings into a JSON value.
      Returns:
      The deserializer used during parsing.
    • parse

      default JsValue parse(String json) throws JsParserException
      Low-level method to parse a JSON value from their string representation. Returns the JsValue if it conforms to this spec, otherwise throws a `JsParserException`.
      Parameters:
      json - The reader to parse JSON values from.
      Returns:
      The next token as a `JsValue`.
      Throws:
      JsParserException - If the parsed value does not conform to this spec.
    • test

      List<SpecError> test(JsPath parentPath, JsValue value)
      Verify if the given JSON value satisfies this spec.
      Parameters:
      parentPath - The path where the tested value is located within the JSON structure.
      value - The JSON value to be tested.
      Returns:
      A set of path/code pairs representing validation errors.
    • test

      default List<SpecError> test(JsValue value)
      Verify if the given JSON value satisfies this spec, starting from the root path.
      Parameters:
      value - The JSON value to be tested.
      Returns:
      A set of path/code pairs representing validation errors.
    • isNullable

      boolean isNullable()