杰克逊ObjectMapper具有UTF-8编码? [英] Jackson ObjectMapper with UTF-8 encoding?
问题描述
在使用ObjectMapper
序列化和反序列化对象时,有没有办法告诉Jackson使用UTF-8编码?
Is there a way to tell Jackson to use UTF-8 encoding when using ObjectMapper
to serialize and deserialize Objects?
推荐答案
Jackson自动检测源中使用的编码:根据JSON规范,只有有效的编码为UTF-8,UTF-16和UTF-32.不能使用其他编码(如Latin-1). 因此,自动检测很容易,并且可以由解析器完成-出于这个原因,不接受任何编码检测. 因此,如果输入为UTF-8,则会被检测为此类.
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.
对于输出,UTF-8是默认值;但是如果您明确希望使用其他编码,则可以显式创建JsonGenerator
(使用采用JsonEncoding
的方法),并将其传递给ObjectMapper
.
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
.
或者,在两种情况下,您当然都可以手动构造java.io.Reader
/java.io.Writer
,并使其使用所需的任何编码.
Alternatively in both cases you can of course manually construct java.io.Reader
/ java.io.Writer
, and make it use whatever encoding you want.
这篇关于杰克逊ObjectMapper具有UTF-8编码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!