Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jackson ObjectMapper with UTF-8 encoding?

Tags:

json

jackson

Is there a way to tell Jackson to use UTF-8 encoding when using ObjectMapper to serialize and deserialize Objects?

like image 681
Patricio Avatar asked Apr 04 '12 02:04

Patricio


People also ask

Is ObjectMapper thread safe Jackson?

Jackson's ObjectMapper is completely thread safe and should not be re-instantiated every time #2170.

Can JSON handle UTF 8?

The JSON spec requires UTF-8 support by decoders. As a result, all JSON decoders can handle UTF-8 just as well as they can handle the numeric escape sequences. This is also the case for Javascript interpreters, which means JSONP will handle the UTF-8 encoded JSON as well.

What does Jackson ObjectMapper do?

The Jackson ObjectMapper can parse JSON from a string, stream or file, and create a Java object or object graph representing the parsed JSON. Parsing JSON into Java objects is also referred to as to deserialize Java objects from JSON. The Jackson ObjectMapper can also create JSON from Java objects.

Does spring boot have Jackson ObjectMapper?

XML support Since 2.0 release, Jackson provides first class support for some other data formats than JSON. Spring Framework and Spring Boot provide builtin support for Jackson based XML serialization/deserialization.


1 Answers

Jackson automatically detects encoding used in source: as per JSON specification, only valid encodings are UTF-8, UTF-16 and UTF-32. No other encodings (like Latin-1) can be used. Because of this, auto-detection is easy and done by parser -- no encoding detection is accepted for this reason. So, if input is UTF-8, it will be detected as such.

For output, UTF-8 is the default; but if you explicitly want to use another encoding, you can create JsonGenerator explicitly (with a method that takes JsonEncoding), and pass this to ObjectMapper.

Alternatively in both cases you can of course manually construct java.io.Reader / java.io.Writer, and make it use whatever encoding you want.

like image 143
StaxMan Avatar answered Oct 19 '22 02:10

StaxMan