Skip to content

Commit e718cf8

Browse files
artembilanspring-builds
authored andcommitted
GH-9614: SimpleJsonSerializer: escape only \
Fixes: #9614 Issue link: #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 `\` (cherry picked from commit 8dad15b)
1 parent 3f28e2a commit e718cf8

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2020 the original author or authors.
2+
* Copyright 2017-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,6 @@
2222
import java.util.Arrays;
2323
import java.util.HashSet;
2424
import java.util.Set;
25-
import java.util.regex.Matcher;
2625

2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
@@ -100,7 +99,7 @@ private static String toElement(Object result) {
10099
return result.toString();
101100
}
102101
else {
103-
return "\"" + (result == null ? "null" : Matcher.quoteReplacement(result.toString())) + "\"";
102+
return "\"" + (result == null ? "null" : result.toString().replace("\\", "\\\\")) + "\"";
104103
}
105104
}
106105

spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.integration.json;
1818

19-
import org.junit.Test;
19+
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.integration.support.json.JsonObjectMapperProvider;
2222

@@ -29,10 +29,10 @@
2929
* @since 5.0
3030
*
3131
*/
32-
public class SimpleJsonSerializerTests {
32+
class SimpleJsonSerializerTests {
3333

3434
@Test
35-
public void test() throws Exception {
35+
void verifySimpleJsonSerializerAgainstSimpleContent() throws Exception {
3636
Foo foo = new Foo("foo");
3737
String json = SimpleJsonSerializer.toJson(foo, "fileInfo");
3838
Foo fooOut = JsonObjectMapperProvider.newInstance().fromJson(json, Foo.class);
@@ -43,7 +43,15 @@ public void test() throws Exception {
4343
assertThat(fooOut.fileInfo).isNull();
4444
}
4545

46-
public static class Foo {
46+
@Test
47+
void verifySimpleJsonSerializerAgainstDollarContent() throws Exception {
48+
Foo foo = new Foo("some content with $");
49+
String json = SimpleJsonSerializer.toJson(foo);
50+
Foo fooOut = JsonObjectMapperProvider.newInstance().fromJson(json, Foo.class);
51+
assertThat(fooOut.fileInfo).isEqualTo("some content with $");
52+
}
53+
54+
static class Foo {
4755

4856
private final String foo = "bar";
4957

@@ -55,12 +63,11 @@ public static class Foo {
5563

5664
private String fileInfo;
5765

58-
public Foo() {
59-
super();
66+
Foo() {
6067
}
6168

62-
public Foo(String info) {
63-
this.fileInfo = "foo";
69+
Foo(String info) {
70+
this.fileInfo = info;
6471
}
6572

6673
public String getFoo() {
@@ -79,7 +86,11 @@ public boolean isBool() {
7986
return this.bool;
8087
}
8188

82-
public String fileInfo() {
89+
public void setFileInfo(String fileInfo) {
90+
this.fileInfo = fileInfo;
91+
}
92+
93+
public String getFileInfo() {
8394
return this.fileInfo;
8495
}
8596

0 commit comments

Comments
 (0)