FasterXML / jackson-dataformats-text

Uber-project for (some) standard Jackson textual format backends: csv, properties, yaml (xml to be added in future)
Apache License 2.0
404 stars 148 forks source link

How to convert POJO to properties using Charset.UTF8 #245

Open caiqfrog opened 3 years ago

caiqfrog commented 3 years ago

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>
cowtowncoder commented 3 years ago

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.