Eka Cahya Pratama Eka Cahya Pratama - 3 months ago 19
YAML Question

Jackson error : unacceptable character '' (0x0) special characters are not allowed

I'm trying to parse yaml data using jackson from MySQL but I get error saying there is a special character. I get error something like this :

com.fasterxml.jackson.databind.JsonMappingException: special characters are not allowed
[Source: java.io.StringReader@5528a42c; line: 13, column: 68] (through reference chain: com.app.ResultContentModel["opinion"])
...
Caused by: com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException: special characters are not allowed
at [Source: java.io.StringReader@5528a42c; line: 13, column: 68]
...
Caused by: unacceptable character '' (0x0) special characters are not allowed
in "'reader'", position 1027


My dependencies file look like this :

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.8.1</version>
</dependency>


I'm trying to replace it using regex :

Pattern nonASCII = Pattern.compile("[^\\x00-\\x7f]");//("[^\\x00-\\x7f]");
resultContent = Normalizer.normalize(nonASCII.matcher(resultContent).replaceAll("");
ResultContentModel rc = mapper.readValue(resultContent, ResultContentModel.class);


But still didn't work. What should I do?

Answer

It seems you want to match any non-ASCII + all characters with hex codes from 00 till 20 (space).

Use

Pattern nonASCII = Pattern.compile("[^\\x00-\\x7f]+|[\\x00-\\x20]+");

The \x00-\x20 will match all the control characters you need to remove and the [^\x00-\x7f] will match non-ASCII chars.

The + quantifier will match 1 or more occurrences, so as to remove/replace the character chunks matched in one go.

Comments