Java + jackson parsing error Unrecognized character escape
Asked Answered
H

4

11

I need to do a POST json string , using HttpClient. Following will be the code i have. From the other end the Json is mapped to an object.

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\mlpdemoins\" }";
post.setEntity(new ByteArrayEntity(         jsonData.toString().getBytes("UTF8")));
HttpResponse response = client.execute(post);

Here all others are correctly mapping expect the userId. Here the problem is with the backward slash(mlpdemo\mlpdemins). I guess. If I send a single string as the user id it will be mapped without any issues. Eg:-

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemoins\" }";

This works .

But I need this (mlpdemo\mlpdemins)to be sent through the POSt. Please help me out.

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\mlpdemoins\" }";

Here is the exception Im getting.

com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'm' (code 109)
 at [Source: java.io.InputStreamReader@29f0a0a2; line: 1, column: 62]
BadRequestException (0ea35150-f33a-4932-a31e-8a1048af53ad): 400 Bad Request, com.strategicgains.restexpress.serialization.DeserializationException: com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'm' (code 109)
 at [Source: java.io.InputStreamReader@29f0a0a2; line: 1, column: 62]
    at com.strategicgains.restexpress.Request.getBodyAs(Request.java:165)
    at com.strategicgains.restexpress.Request.getBodyAs(Request.java:181)
Homespun answered 12/5, 2015 at 10:0 Comment(1)
Try escaping the backslash with one or more backslash \"mlpdemo\\\mlpdemoins\". Not sure but give it a try. Sorry if I am wrongWhitacre
S
6

mlpdemo\mlpdemoins is an invalid string you can't use it in JSON . But you can use mlpdemo\\mlpdemoins easily.

below code works fine for me :

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\\\mlpdemoins\" }";

ObjectMapper mapper=new ObjectMapper();

System.out.println(mapper.readTree(jsonData));

It will produce this output JSON :

{"provider":null,"password":"a","userid":"mlpdemo\\mlpdemoins"}
Salty answered 12/5, 2015 at 10:30 Comment(0)
B
27

Set your mapper

mapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true); 
Baisden answered 8/6, 2016 at 14:13 Comment(4)
Where to write it?Giustino
In my version of jackson 2.9.9, the JsonParser is missingRudnick
Need to write in ObjectMapper Configuration.Chiarra
Deprecated. Since 2.10 use JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER instead.Commie
D
8

Since version 2.10 (released Sep, 2019), JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER has been deprecated, together with some other properties:

@deprecated Since 2.10 use {@link com.fasterxml.jackson.core.json.JsonReadFeature#ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER} instead

While the properties at the time still exist in the current version (2.11.3), it has actually been removed from the GIT master branch, suggesting that it will probably be removed when version 3.0.0 is released.

The correct way to configure this property is instead by using JsonReadFeature instead. Instead of creating an ObjectMapper and call configure() on it, use JsonMapper and its builder() method:

ObjectMapper mapper = JsonMapper.builder()
    .enable(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER)
    .build();

This returns a JsonMapper, which is a sub class of ObjectMapper, with the property enabled.


The related GitHub issue can be found here, and this issue suggests some alternatives on how to achieve the configuration.

Doeskin answered 5/10, 2020 at 13:37 Comment(1)
I'd like to thank you for providing links inline to the classes in question. More importantly, your code snippet worked for me.Radman
S
6

mlpdemo\mlpdemoins is an invalid string you can't use it in JSON . But you can use mlpdemo\\mlpdemoins easily.

below code works fine for me :

String jsonData = "{ \"provider\" : null , \"password\" : \"a\", \"userid\" : \"mlpdemo\\\\mlpdemoins\" }";

ObjectMapper mapper=new ObjectMapper();

System.out.println(mapper.readTree(jsonData));

It will produce this output JSON :

{"provider":null,"password":"a","userid":"mlpdemo\\mlpdemoins"}
Salty answered 12/5, 2015 at 10:30 Comment(0)
C
1

In my case I was using Spring so setting this property in the application.yml did the job:

spring:
  jackson:
    parser:
      allow-backslash-escaping-any-character: true

This GitHub comment was helpful: https://github.com/FasterXML/jackson-core/issues/586#issuecomment-672199588

Cinerary answered 9/8, 2021 at 4:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.