public final class Jcs extends Object
This class provides two main capabilities:
This implementation uses
Jakarta JSON Processing
(JSON-P) as input and can output canonicalized JSON to a
Writer or return it as a String.
Usage examples:
// Canonicalize a JSON value and write to a writer
Jcs.canonize(jsonValue, writer);
// Compare two JSON values for canonical equality
boolean equal = Jcs.equals(json1, json2);
| Modifier and Type | Field and Description |
|---|---|
protected static DecimalFormat |
eFormat
Plain notation used by JCS for mid-range
BigDecimal values (English
locale). |
protected static DecimalFormat |
eFormatBigDecimal
Exponent notation used by JCS for very large/small
BigDecimal values
(English locale). |
| Constructor and Description |
|---|
Jcs() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
arrayEquals(jakarta.json.JsonArray array1,
jakarta.json.JsonArray array2)
Compares two JSON arrays for canonical equality under RFC 8785 (JCS).
|
static String |
canonize(jakarta.json.JsonValue value)
Canonicalizes a JSON value according to the RFC 8785 JSON Canonicalization
Scheme (JCS) and returns the canonicalized JSON as a string.
|
static void |
canonize(jakarta.json.JsonValue value,
Writer writer)
Canonicalizes a JSON according to the RFC 8785 JSON Canonicalization Scheme
(JCS).
|
static void |
canonizeArray(jakarta.json.JsonArray value,
Writer writer)
Canonicalizes a JSON array according to the RFC 8785 JSON Canonicalization
Scheme (JCS).
|
static String |
canonizeNumber(jakarta.json.JsonNumber number)
Canonicalizes a JSON number according to the RFC 8785 JSON Canonicalization
Scheme (JCS).
|
static void |
canonizeObject(jakarta.json.JsonObject value,
Writer writer)
Canonicalizes a JSON object according to the RFC 8785 JSON Canonicalization
Scheme (JCS).
|
static boolean |
equals(jakarta.json.JsonValue value1,
jakarta.json.JsonValue value2)
Compares two JSON values for canonical equality under RFC 8785 (JCS).
|
static String |
escape(String value)
Escapes special characters in a JSON property name according to JSON
canonicalization rules.
|
static boolean |
numberEquals(jakarta.json.JsonNumber number1,
jakarta.json.JsonNumber number2)
Compares two JSON numbers for canonical equality under RFC 8785 (JCS).
|
static boolean |
objectEquals(jakarta.json.JsonObject object1,
jakarta.json.JsonObject object2)
Compares two JSON objects for canonical equality under RFC 8785 (JCS).
|
protected static final DecimalFormat eFormatBigDecimal
BigDecimal values
(English locale).protected static final DecimalFormat eFormat
BigDecimal values (English
locale).public static final String canonize(jakarta.json.JsonValue value)
This method serializes the given JsonValue in a deterministic and
standardized manner, ensuring a consistent output regardless of formatting
differences. It handles all JSON value types, including objects, arrays,
numbers, strings, and literals (true, false, null).
value - the JSON value to be canonicalizedpublic static final void canonize(jakarta.json.JsonValue value,
Writer writer)
throws IOException
This method serializes the given JsonValue in a deterministic and
standardized manner, ensuring a consistent output regardless of formatting
differences. The canonicalized JSON is written to the provided
Writer.
This method handles different JSON value types, including objects, arrays, numbers, strings, and literals (true, false, null).
value - the JSON value to be canonicalizedwriter - the writer to which the canonicalized JSON output is writtenIOException - if an I/O error occurs while writing to the writerpublic static final String canonizeNumber(jakarta.json.JsonNumber number)
This method serializes the given JsonNumber in a deterministic and
standardized manner, ensuring a consistent numeric representation.
number - the JSON number to be canonicalizedpublic static final void canonizeArray(jakarta.json.JsonArray value,
Writer writer)
throws IOException
This method serializes the given JsonArray in a deterministic and
standardized manner, ensuring a consistent output. The canonicalized JSON
array is written to the provided Writer.
value - the JSON array to be canonicalizedwriter - the writer to which the canonicalized JSON output is writtenIOException - if an I/O error occurs while writing to the writerpublic static final void canonizeObject(jakarta.json.JsonObject value,
Writer writer)
throws IOException
This method serializes the given JsonObject in a deterministic and
standardized manner, ensuring a consistent output. The canonicalized JSON is
written to the provided Writer.
value - the JSON object to be canonicalizedwriter - the writer to which the canonicalized JSON output is writtenIOException - if an I/O error occurs while writing to the writerpublic static final String escape(String value)
This method ensures that control characters and other necessary characters are properly escaped to maintain a valid and consistent JSON representation.
value - the JSON property name to escapepublic static final boolean equals(jakarta.json.JsonValue value1,
jakarta.json.JsonValue value2)
Values are considered equal if their JSON data models are the same after applying JCS canonicalization rules. That includes canonical number formatting and lexicographic member ordering for objects.
value1 - the first JSON value (may be null)value2 - the second JSON value (may be null)true if the two values are canonically equal; false
otherwisepublic static final boolean numberEquals(jakarta.json.JsonNumber number1,
jakarta.json.JsonNumber number2)
The comparison is performed by canonicalizing each number using
canonizeNumber(JsonNumber) and comparing the resulting strings.
number1 - the first JSON numbernumber2 - the second JSON numbertrue if both numbers have the same canonical representation;
false otherwisepublic static final boolean objectEquals(jakarta.json.JsonObject object1,
jakarta.json.JsonObject object2)
Objects are equal if they contain the same set of member names (after
escaping per JCS) and corresponding member values are canonically equal as
determined by equals(JsonValue, JsonValue).
object1 - the first JSON objectobject2 - the second JSON objecttrue if the two objects are canonically equal; false
otherwisepublic static final boolean arrayEquals(jakarta.json.JsonArray array1,
jakarta.json.JsonArray array2)
Arrays are equal if they have the same length and each element at position
i is canonically equal as determined by
equals(JsonValue, JsonValue).
array1 - the first JSON arrayarray2 - the second JSON arraytrue if the two arrays are canonically equal; false
otherwiseCopyright © 2020–2025. All rights reserved.