Skip to content

fix: JSON builder string encoding of control characters #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
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
23 changes: 17 additions & 6 deletions src/main/java/org/hisp/dhis/jsontree/JsonAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,26 @@ void appendEscaped( CharSequence str ) {
return;
}
append( '"' );
str.chars().forEachOrdered( c -> {
if ( c == '"' || c == '\\' || c < ' ' ) {
appendChar.accept( '\\' );
}
appendChar.accept( (char) c );
} );
str.chars().forEachOrdered( this::appendEscaped );
append( '"' );
}

private void appendEscaped( int c ) {
switch ( c ) {
case '\b' -> append( "\\b" );
case '\f' -> append( "\\f" );
case '\n' -> append( "\\n" );
case '\r' -> append( "\\r" );
case '\t' -> append( "\\t" );
case '"' -> append( "\\\"" );
case '\\' -> append( "\\\\" );
case -31 -> append( "\\u%04X".formatted( c ) );
case 0x2028 -> append( "\\u2028" );
case 0x2029 -> append( "\\u2029" );
default -> appendChar.accept( (char) c );
}
}

private void beginLevel( char c ) {
append( c );
hasChildrenAtLevel[++level] = false;
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/org/hisp/dhis/jsontree/JuonAppender.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,23 @@ private void append(char c) {

void appendEscaped(@Surly CharSequence str ) {
append( '\'' );
str.chars().forEachOrdered( c -> {
if ( c == '\'' || c == '\\' || c < ' ' ) {
append( '\\' );
}
append( (char) c );
} );
str.chars().forEachOrdered( this::appendEscaped );
append( '\'' );
}

private void appendEscaped( int c ) {
switch ( c ) {
case '\b' -> append( "\\b" );
case '\f' -> append( "\\f" );
case '\n' -> append( "\\n" );
case '\r' -> append( "\\r" );
case '\t' -> append( "\\t" );
case '\'' -> append( "\\'" );
case '\\' -> append( "\\\\" );
case -31 -> append( "\\u%04X".formatted( c ) );
case 0x2028 -> append( "\\u2028" );
case 0x2029 -> append( "\\u2029" );
default -> append( (char) c );
}
}
}
19 changes: 19 additions & 0 deletions src/test/java/org/hisp/dhis/jsontree/JsonAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,25 @@ void testArray_JsonNode() {
.addElement( JsonNode.of( "[\"a\",\"b\"]" ) ) ) );
}

@Test
void testNewlines() {
//language=JSON
String json = """
{"id":"woOg1dUFoX0","time":1738685143915,"message":"com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\\n at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]","level":"ERROR"}""";
JsonObject msg = JsonMixed.of( json );
String message = msg.getString( "message" ).string();
assertEquals( """
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]""",
message );
JsonObject actual = Json.object( obj ->
obj.addString( "id", "woOg1dUFoX0" )
.addNumber( "time", 1738685143915L )
.addString( "message", message )
.addString( "level", "ERROR" ));
assertEquals( json, actual.toJson() );
}

private static void assertJson( String expected, JsonNode actual ) {
assertEquals( expected.replace( '\'', '"' ), actual.getDeclaration() );
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/org/hisp/dhis/jsontree/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ void testOf_Boolean() {
void testOf_String() {
assertJson( "null", Json.of( (String) null ) );
assertJson( "\"hello\"", Json.of( "hello" ) );
assertJson( "\"hello\\\n"
+ "world\"", Json.of( "hello\nworld" ) );
JsonString hello = Json.of( "hello\nworld" );
assertJson( "\"hello\\nworld\"", hello );
assertEquals( "hello\nworld", hello.string() );
}

@Test
Expand Down