Skip to content

Commit

Permalink
GH-9614: SimpleJsonSerializer: escape only \
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
artembilan authored and spring-builds committed Oct 30, 2024
1 parent 3f28e2a commit e718cf8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -22,7 +22,6 @@
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;
Expand Down Expand Up @@ -100,7 +99,7 @@ private static String toElement(Object result) {
return result.toString();
}
else {
return "\"" + (result == null ? "null" : Matcher.quoteReplacement(result.toString())) + "\"";
return "\"" + (result == null ? "null" : result.toString().replace("\\", "\\\\")) + "\"";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;

Expand All @@ -29,10 +29,10 @@
* @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);
Expand All @@ -43,7 +43,15 @@ public void test() throws Exception {
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";

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

private String fileInfo;

public Foo() {
super();
Foo() {
}

public Foo(String info) {
this.fileInfo = "foo";
Foo(String info) {
this.fileInfo = info;
}

public String getFoo() {
Expand All @@ -79,7 +86,11 @@ public boolean isBool() {
return this.bool;
}

public String fileInfo() {
public void setFileInfo(String fileInfo) {
this.fileInfo = fileInfo;
}

public String getFileInfo() {
return this.fileInfo;
}

Expand Down

0 comments on commit e718cf8

Please sign in to comment.