Is there a way to tell Jackson to use UTF-8 encoding when using ObjectMapper
to serialize and deserialize Objects?
Jackson's ObjectMapper is completely thread safe and should not be re-instantiated every time #2170.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With