I have an issue while trying to parse a string value using object mapper.
Below is my code,
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payload {
private String id;
private String message;
}
Then my main class
import com.fasterxml.jackson.databind.ObjectMapper;
public class MyClass {
public static void main(String args[]) throws Exception {
String payloadText = "{\"id\":\"1\",\"message\":\"Message \"Yes\"\"}";
ObjectMapper mapper = new ObjectMapper();
Payload p = mapper.readValue(payloadText, Payload.class);
System.out.println(p);
}
}
My JSON string contains a word with double quotes "". Looks like because of the same, when I am trying to parse the JSON using ObjectMapper readValue method, it gives me below exception,
Exception in thread "main" com.fasterxml.jackson.core.JsonParseException: Unexpected character ('Y' (code 89)): was expecting comma to separate Object entries
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 31]
at com.fasterxml.jackson.core.JsonParser._constructReadException(JsonParser.java:2660)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:741)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._skipComma(ReaderBasedJsonParser.java:2429)
at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextFieldName(ReaderBasedJsonParser.java:924)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:317)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:177)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4917)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3860)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3828)
at MyClass.main(MyClass.java:9)
I have tried using escape character methods from StringEscapeUtils class, but no use. I also tried using JsonParser feature ALLOW BACKSLASH ESCAPING ANY CHARACTER, still no luck.
Can anyone guide as to how can make the object mapper ignore the backslashes used for skipping double quotes.
Any help or pointers are appreciated.