1

I have such string value in my json:

{
"number": "3111\/227\/005369",
}

I'm using ObjectMapper autoconfigured with Spring Boot and it ignores all of \ This is the result of deserialized property i have:

 number="3111/227/005369"

ObjectMapper totally ignore \

This is my expectations of field String value:

number="3111\\/227\\/005369"

how may i solve this problem ?

Igor
  • 478
  • 9
  • 22
  • 4
    JSON doesn't require to escape slashes but [often backslashes are used to escape them anyways](https://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped) so `"3111\/227\/005369"` in JSON represents the text `3111/227/005369`- hence Jackson is doing it correctly. – Thomas Nov 07 '18 at 14:50
  • 1
    What you could do is try to add [`@JsonRawValue`](https://fasterxml.github.io/jackson-annotations/javadoc/2.9/com/fasterxml/jackson/annotation/JsonRawValue.html) to `number` or look for a configuration feature that allows you to disable that escaping for the object mapper entirely (I'm sure there is one but don't know it's name atm). – Thomas Nov 07 '18 at 14:57
  • @Thomas thanks for your answers! JsonRawValue does't fixed the problem, but there is not such values in our production base, so for me this question is closed by itself. Maybe someone will answer how to avoid such problem, if they occur in future – Igor Nov 07 '18 at 15:49
  • Well, if such values are in the database the corresponding JSON would be `"3111\\/227\\/005369"` and the Java string would be `3111\/227\/005369` (the literal would be the same as the JSON value) - so that shouldn't be a problem. – Thomas Nov 08 '18 at 12:08

1 Answers1

0

Backslash is an escape character in JSON so the JSON should also have double backslash to be deserialized as you expect

Muji
  • 531
  • 6
  • 10