json 使用 UTF-8 编码的 Jackson ObjectMapper?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10004241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:16:27  来源:igfitidea点击:

Hymanson ObjectMapper with UTF-8 encoding?

jsonHymanson

提问by Patricio

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

有没有办法告诉 Hymanson 在ObjectMapper用于序列化和反序列化对象时使用 UTF-8 编码?

回答by StaxMan

Hymanson 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.

Hymanson 自动检测源代码中使用的编码:根据 JSON 规范,只有有效的编码是 UTF-8、UTF-16 和 UTF-32。不能使用其他编码(如 Latin-1)。因此,自动检测很容易并且由解析器完成——因此不接受编码检测。因此,如果输入是 UTF-8,则会被检测到。

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

对于输出,UTF-8 是默认值;但是,如果您明确想使用另一种编码,则可以JsonGenerator明确创建(使用采用 的方法JsonEncoding),并将其传递给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.

或者,在这两种情况下,您当然可以手动构建java.io.Reader/ java.io.Writer,并使其使用您想要的任何编码。