GH-9614: SimpleJsonSerializer: escape only \

Fixes: #9614
Issue link: https://github.com/spring-projects/spring-integration/issues/9614

The `Matcher.quoteReplacement()` escapes `\` as well as `$`.
However, Jackson tries to resolve special symbol from the escaped `$` and fails as `Unrecognized character escape '$' (code 36)`

* Fix `SimpleJsonSerializer.toElement()` to escape only `\`

**Auto-cherry-pick to `6.3.x` & `6.2.x`**
This commit is contained in:
Artem Bilan
2024-10-30 14:17:41 -04:00
parent 745dde1f65
commit 8dad15bac2
2 changed files with 23 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -100,7 +99,7 @@ public final class SimpleJsonSerializer {
return result.toString();
}
else {
return "\"" + (result == null ? "null" : Matcher.quoteReplacement(result.toString())) + "\"";
return "\"" + (result == null ? "null" : result.toString().replace("\\", "\\\\")) + "\"";
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
package org.springframework.integration.json;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.integration.support.json.JsonObjectMapperProvider;
@@ -29,10 +29,10 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 5.0
*
*/
public class SimpleJsonSerializerTests {
class SimpleJsonSerializerTests {
@Test
public void test() throws Exception {
void verifySimpleJsonSerializerAgainstSimpleContent() throws Exception {
Foo foo = new Foo("foo");
String json = SimpleJsonSerializer.toJson(foo, "fileInfo");
Foo fooOut = JsonObjectMapperProvider.newInstance().fromJson(json, Foo.class);
@@ -43,7 +43,15 @@ public class SimpleJsonSerializerTests {
assertThat(fooOut.fileInfo).isNull();
}
public static class Foo {
@Test
void verifySimpleJsonSerializerAgainstDollarContent() throws Exception {
Foo foo = new Foo("some content with $");
String json = SimpleJsonSerializer.toJson(foo);
Foo fooOut = JsonObjectMapperProvider.newInstance().fromJson(json, Foo.class);
assertThat(fooOut.fileInfo).isEqualTo("some content with $");
}
static class Foo {
private final String foo = "bar";
@@ -55,12 +63,11 @@ public class SimpleJsonSerializerTests {
private String fileInfo;
public Foo() {
super();
Foo() {
}
public Foo(String info) {
this.fileInfo = "foo";
Foo(String info) {
this.fileInfo = info;
}
public String getFoo() {
@@ -79,7 +86,11 @@ public class SimpleJsonSerializerTests {
return this.bool;
}
public String fileInfo() {
public void setFileInfo(String fileInfo) {
this.fileInfo = fileInfo;
}
public String getFileInfo() {
return this.fileInfo;
}