public class JSONValidator
extends java.lang.Object
This class provides methods to validate JSON content and determine its type (Object, Array, or Value). It can handle both UTF-8 encoded byte arrays and String representations of JSON.
Example usage:
// Validate a JSON string
JSONValidator validator = JSONValidator.from("{\"name\":\"John\", \"age\":30}");
boolean isValid = validator.validate(); // returns true
JSONValidator.Type type = validator.getType(); // returns Type.Object
// Validate a JSON byte array
byte[] jsonBytes = "[1, 2, 3]".getBytes(StandardCharsets.UTF_8);
boolean isValidArray = JSONValidator.fromUtf8(jsonBytes).validate(); // returns true
| Modifier and Type | Class and Description |
|---|---|
static class |
JSONValidator.Type
An enumeration representing the type of JSON structure.
|
| Modifier and Type | Field and Description |
|---|---|
private JSONReader |
jsonReader |
private JSONValidator.Type |
type |
private java.lang.Boolean |
validateResult |
| Modifier | Constructor and Description |
|---|---|
protected |
JSONValidator(JSONReader jsonReader)
Constructs a new JSONValidator with the specified JSONReader.
|
| Modifier and Type | Method and Description |
|---|---|
static JSONValidator |
from(JSONReader jsonReader)
Creates a new JSONValidator for the specified JSONReader.
|
static JSONValidator |
from(java.lang.String jsonStr)
Creates a new JSONValidator for the specified JSON string.
|
static JSONValidator |
fromUtf8(byte[] jsonBytes)
Creates a new JSONValidator for the specified UTF-8 encoded byte array.
|
JSONValidator.Type |
getType()
Returns the type of the JSON content.
|
boolean |
validate()
Validates the JSON content and returns true if it is valid JSON.
|
private final JSONReader jsonReader
private java.lang.Boolean validateResult
private JSONValidator.Type type
protected JSONValidator(JSONReader jsonReader)
This constructor is protected and intended for internal use. Use the static factory methods to create instances.
jsonReader - the JSONReader to use for validationpublic static JSONValidator fromUtf8(byte[] jsonBytes)
jsonBytes - the UTF-8 encoded byte array containing JSON contentpublic static JSONValidator from(java.lang.String jsonStr)
jsonStr - the string containing JSON contentpublic static JSONValidator from(JSONReader jsonReader)
jsonReader - the JSONReader containing JSON contentpublic boolean validate()
This method parses the JSON content to check its validity. The result is cached, so subsequent calls will return the same result without re-parsing.
public JSONValidator.Type getType()
If the type has not yet been determined, this method will call validate()
to parse the content and determine its type.