【发布时间】:2021-01-13 16:30:29
【问题描述】:
我尝试用 json4s 漂亮地编写一个包含€ 字符的JString,如下所示:
import org.joda.time.format.ISODateTimeFormat
import org.joda.time.{DateTime, DateTimeZone}
import org.json4s.native.Serialization.writePretty
import org.json4s.{DateFormat, DefaultFormats, Formats, JString}
import java.util.{Date, TimeZone}
object Json4sEncodingTest {
val formats = new Formats {
val dateFormat: DateFormat = new DateFormat {
override def parse(s: String): Option[Date] =
try {
Option(
DateTime
.parse(s, ISODateTimeFormat.dateTimeParser().withZoneUTC())
.withZone(DateTimeZone.forID(timezone.getID))
.toDate
)
} catch {
case e: IllegalArgumentException => None
}
override def format(d: Date): String = DefaultFormats.lossless.dateFormat.format(d)
override def timezone: TimeZone = DefaultFormats.lossless.dateFormat.timezone
}
override def alwaysEscapeUnicode: Boolean = false
}
def main(args: Array[String]): Unit = {
println(writePretty(JString("2€"))(formats))
}
}
这会导致:
"2\u20ac"
我的预期结果是:
"2€"
我发现在 org.json4s.ParserUtil.quote 中 \u2000 和 \u2100 之间的字符总是被转义。
问题:为什么会这样?
- json4s版本:3.7.0-M7
- scala 版本:2.12.11
【问题讨论】:
-
我会在这里打开一个问题github.com/json4s/json4s/issues