fix issue #5127 escape key of json data (#5130)

This commit is contained in:
leoloel
2024-05-22 09:41:50 +08:00
committed by GitHub
parent bbea802914
commit 5ff6449620
2 changed files with 26 additions and 1 deletions

View File

@@ -291,7 +291,7 @@ public class JsonConversion {
if (i > 0) { if (i > 0) {
buf.append(", "); buf.append(", ");
} }
buf.append('"').append(key(i, charset)).append('"'); buf.append('"').append(escapse(key(i, charset))).append('"');
buf.append(": "); buf.append(": ");
element(i, charset).toJsonString(buf, charset); element(i, charset).toJsonString(buf, charset);
} }

View File

@@ -9,6 +9,8 @@ import org.junit.rules.Timeout;
import com.taobao.tddl.dbsync.binlog.JsonConversion.Json_Value; import com.taobao.tddl.dbsync.binlog.JsonConversion.Json_Value;
import com.taobao.tddl.dbsync.binlog.JsonConversion.Json_enum_type; import com.taobao.tddl.dbsync.binlog.JsonConversion.Json_enum_type;
import java.nio.charset.Charset;
public class JsonConversion_Json_ValueTest { public class JsonConversion_Json_ValueTest {
@Rule @Rule
@@ -51,4 +53,27 @@ public class JsonConversion_Json_ValueTest {
// Method is not expected to return due to exception thrown // Method is not expected to return due to exception thrown
} }
// test for 5127
@Test
public void testJsonKeyContainsSpecialCharacter() {
// {"internal_uri_rewrite": {"(.*)(/[^/]+\\.(mp|MP)4)$": "$1/mp4$2"}}
String jsonData = "{\"internal_uri_rewrite\": {\"(.*)(/[^/]+\\\\.(mp|MP)4)$\": \"$1/mp4$2\"}}";
byte[] data = new byte[]{1, 0, 74, 0, 11, 0, 20, 0, 0, 31, 0, 105, 110, 116, 101, 114, 110, 97, 108, 95, 117, 114, 105, 95, 114, 101, 119, 114, 105, 116, 101, 1, 0, 43, 0, 11, 0, 23, 0, 12, 34, 0, 40, 46, 42, 41, 40, 47, 91, 94, 47, 93, 43, 92, 46, 40, 109, 112, 124, 77, 80, 41, 52, 41, 36, 8, 36, 49, 47, 109, 112, 52, 36, 50};
final LogBuffer buffer = new LogBuffer(data, 0, 74);
Charset charset = Charset.forName("UTF-8");
Json_Value jsonValue = JsonConversion.parse_value(0, buffer, 74, charset);
StringBuilder builder = new StringBuilder();
jsonValue.toJsonString(builder, charset);
Assert.assertEquals(builder.toString(), jsonData);
Assert.assertEquals(jsonValue.key(0, charset), "internal_uri_rewrite");
Json_Value element = jsonValue.element(0, charset);
Assert.assertEquals(element.key(0, charset), "(.*)(/[^/]+\\.(mp|MP)4)$");
}
} }