Package com.alibaba.fastjson2
Class JSONValidator
java.lang.Object
com.alibaba.fastjson2.JSONValidator
A utility class for validating JSON strings or byte arrays to check if they
represent valid JSON structures.
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
- Since:
- 2.0.59
- Author:
- wenshao[szujobs@hotmail.com]
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumAn enumeration representing the type of JSON structure. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedJSONValidator(JSONReader jsonReader) Constructs a new JSONValidator with the specified JSONReader. -
Method Summary
Modifier and TypeMethodDescriptionstatic JSONValidatorfrom(JSONReader jsonReader) Creates a new JSONValidator for the specified JSONReader.static JSONValidatorCreates a new JSONValidator for the specified JSON string.static JSONValidatorfromUtf8(byte[] jsonBytes) Creates a new JSONValidator for the specified UTF-8 encoded byte array.getType()Returns the type of the JSON content.booleanvalidate()Validates the JSON content and returns true if it is valid JSON.
-
Constructor Details
-
JSONValidator
Constructs a new JSONValidator with the specified JSONReader.This constructor is protected and intended for internal use. Use the static factory methods to create instances.
- Parameters:
jsonReader- the JSONReader to use for validation
-
-
Method Details
-
fromUtf8
Creates a new JSONValidator for the specified UTF-8 encoded byte array.- Parameters:
jsonBytes- the UTF-8 encoded byte array containing JSON content- Returns:
- a new JSONValidator instance
-
from
Creates a new JSONValidator for the specified JSON string.- Parameters:
jsonStr- the string containing JSON content- Returns:
- a new JSONValidator instance
-
from
Creates a new JSONValidator for the specified JSONReader.- Parameters:
jsonReader- the JSONReader containing JSON content- Returns:
- a new JSONValidator instance
-
validate
public boolean validate()Validates the JSON content and returns true if it is valid JSON.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.
- Returns:
- true if the content is valid JSON, false otherwise
-
getType
Returns the type of the JSON content.If the type has not yet been determined, this method will call
validate()to parse the content and determine its type.- Returns:
- the Type of the JSON content (Object, Array, or Value)
-