Interface JsSpec
- All Known Subinterfaces:
JsArraySpec
- All Known Implementing Classes:
JsObjSpec
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.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
nullable()
Returns the same spec with the nullable flag enabled.default JsValue
Low-level method to parse a JSON value from their string representation.jsonvalues.spec.JsParser
parser()
Returns the deserializer used during the parsing process to parse an array of bytes or strings into a JSON value.Verify if the given JSON value satisfies this spec.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
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
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
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()
-