Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/main/java/dev/toonformat/jtoon/JToon.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static String encode(final Object input) {
* @param input The object to encode (can be null)
* @param options Encoding options (indent, delimiter, length marker)
* @return The JToon-formatted string
* @throws IllegalArgumentException if a key or string value contains an unpaired surrogate
* @throws NullPointerException if options is null
*/
public static String encode(final Object input, final EncodeOptions options) {
Expand All @@ -62,7 +63,8 @@ public static String encode(final Object input, final EncodeOptions options) {
*
* @param json The JSON string to encode (must be valid JSON)
* @return The TOON-formatted string
* @throws IllegalArgumentException if the input is not valid JSON
* @throws IllegalArgumentException if the input is not valid JSON, or a key or
* string value contains an unpaired surrogate
*/
public static String encodeJson(final String json) {
return encodeJson(json, EncodeOptions.DEFAULT);
Expand All @@ -78,7 +80,8 @@ public static String encodeJson(final String json) {
* @param json The JSON string to encode (must be valid JSON)
* @param options Encoding options (indent, delimiter, length marker)
* @return The TOON-formatted string
* @throws IllegalArgumentException if the input is not valid JSON
* @throws IllegalArgumentException if the input is not valid JSON, or a key or
* string value contains an unpaired surrogate
* @throws NullPointerException if options is null
*/
public static String encodeJson(final String json, final EncodeOptions options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private static String stripTrailingZeros(final String value) {
* @return the encoded string, quoted if necessary
*/
static String encodeStringLiteral(final String value, final String delimiter) {
requireScalarValues(value);
if (StringValidator.isSafeUnquoted(value, delimiter)) {
return value;
}
Expand All @@ -114,13 +115,33 @@ static String encodeStringLiteral(final String value, final String delimiter) {
* @return the encoded key, quoted if necessary
*/
public static String encodeKey(final String key) {
requireScalarValues(key);
if (StringValidator.isValidUnquotedKey(key)) {
return key;
}

return DOUBLE_QUOTE + StringEscaper.escape(key) + DOUBLE_QUOTE;
}

/**
* Rejects strings containing an unpaired surrogate, which TOON cannot
* represent (§3).
*
* @param value the key or string value to check
* @throws IllegalArgumentException if the string contains an unpaired surrogate
*/
static void requireScalarValues(final String value) {
int index = 0;
while (index < value.length()) {
final int codePoint = value.codePointAt(index);
if (Character.getType(codePoint) == Character.SURROGATE) {
throw new IllegalArgumentException(String.format(
"Cannot encode string containing an unpaired surrogate U+%04X at index %d", codePoint, index));
}
index += Character.charCount(codePoint);
}
}

/**
* Joins encoded primitive values with the specified delimiter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,36 @@ void testInactiveDelimiter() {
}
}

@Nested
@DisplayName("unpaired surrogates")
class UnpairedSurrogates {

@Test
@DisplayName("should reject a lone high surrogate in a string value")
void testLoneHighSurrogateValue() {
// Given
final JsonNode node = new StringNode("a" + (char) 0xD800 + "b");

// When
final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> PrimitiveEncoder.encodePrimitive(node, Delimiter.COMMA.toString()));

// Then
assertEquals("Cannot encode string containing an unpaired surrogate U+D800 at index 1",
exception.getMessage());
}

@Test
@DisplayName("should reject a lone low surrogate in a key")
void testLoneLowSurrogateKey() {
// Given
final String key = "k" + (char) 0xDC00;

// When / Then
assertThrows(IllegalArgumentException.class, () -> PrimitiveEncoder.encodeKey(key));
}
}

@Nested
@DisplayName("encodeKey")
class EncodeKey {
Expand Down
Loading