jackson-dataformats-text
jackson-dataformats-text copied to clipboard
How to convert POJO to properties using Charset.UTF8
example by kotlin
val mapper = JavaPropsMapper()
val pojo = mapOf("a" to "道")
val properties = String(mapper.writeValueAsBytes(pojo), Charset.forName("UTF-8"))
I want output is "a=道", but it output is "a=\u9053" How do I achieve this?
// maven
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-properties</artifactId>
<version>2.9.8</version>
</dependency>
Java Properties files have traditionally been encoded as ISO-8859-1 (aka Latin-1):
Before Java 9, the encoding of a .properties file is ISO-8859-1, also known as Latin-1. All non-Latin-1 characters must be entered by using Unicode escape characters, e.g. \uHHHH where HHHH is a hexadecimal index of the character in the Unicode character set.
(from https://en.wikipedia.org/wiki/.properties)
and as such, Jackson always escapes all Unicode characters above value 0xFF.
It should be possible to add a JavaPropsGenerator.Feature to indicate whether escape should be performed or not, but there is no such setting currently. So as things are, you can not prevent escaping of Unicode characters beyond 8-bit range.