Gson Unicode字符转换为Unicode字符代码
在下面查看我的代码。我有一个包含Unicode字符代码的JSON字符串。我将其转换为Java对象,然后将其转换回JSON字符串。但是,您可以看到输入和输出JSON字符串不匹配。是否可以使用Gson将我的对象转换为原始JSON字符串?我想outputJson
和一样inputJson
。
static class Book { String description;
}
public static void test() {
Gson gson = new Gson();
String inputJson = "{\"description\":\"Tikrovi\\u0161kai para\\u0161ytas k\\u016brinys\"}";
Book book = gson.fromJson(inputJson, Book.class);
String outputJson = gson.toJson(book);
System.out.println(inputJson);
System.out.println(outputJson);
// Prints:
// {"description":"Tikrovi\u0161kai para\u0161ytas k\u016brinys"}
// {"description":"Tikroviškai parašytas kūrinys"}
}
回答:
不幸的是,Gson似乎不支持它。所有JSON输入/输出分别集中在Gson(从2.8.0开始)JsonReader
和JsonWriter
。JsonReader
可以使用其私有readEscapeCharacter
方法读取Unicode转义。但是,与有所不同的是JsonReader
,JsonWriter
只需将字符串写入后备Writer
实例,除了\u2028
和以外,不对127以上的字符进行字符校正\u2029
。可能唯一可以做的是编写自定义转义符,Writer
以便发出Unicode转义。
final class EscapedWriter extends Writer {
private static final char[] hex = {
'0', '1', '2', '3',
'4', '5', '6', '7',
'8', '9', 'a', 'b',
'c', 'd', 'e', 'f'
};
private final Writer writer;
// I/O components are usually implemented in not thread-safe manner
// so we can save some time on constructing a single UTF-16 escape
private final char[] escape = { '\\', 'u', 0, 0, 0, 0 };
EscapedWriter(final Writer writer) {
this.writer = writer;
}
// This implementation is not very efficient and is open for enhancements:
// * constructing a single "normalized" buffer character array so that it could be passed to the downstream writer
// rather than writing characters one by one
// * etc...
@Override
public void write(final char[] buffer, final int offset, final int length)
throws IOException {
for ( int i = offset; i < length; i++ ) {
final int ch = buffer[i];
if ( ch < 128 ) {
writer.write(ch);
} else {
escape[2] = hex[(ch & 0xF000) >> 12];
escape[3] = hex[(ch & 0x0F00) >> 8];
escape[4] = hex[(ch & 0x00F0) >> 4];
escape[5] = hex[ch & 0x000F];
writer.write(escape);
}
}
}
@Override
public void flush()
throws IOException {
writer.flush();
}
@Override
public void close()
throws IOException {
writer.close();
}
// Some java.io.Writer subclasses may use java.lang.Object.toString() to materialize their accumulated state by design
// so it has to be overridden and forwarded as well
@Override
public String toString() {
return writer.toString();
}
}
该作家未经充分测试,不尊重\u2028
和\u2029
。然后只需在调用toJson
方法时配置输出目标即可:
final String input = "{\"description\":\"Tikrovi\\u0161kai para\\u0161ytas k\\u016brinys\"}";final Book book = gson.fromJson(input, Book.class);
final Writer output = new EscapedWriter(new StringWriter());
gson.toJson(book, output);
System.out.println(input);
System.out.println(output);
输出:
{“描述”:“ Tikrovi \ u0161ytas k \ u016brinys”}
这是一个有趣的问题,您可能还会在google /
gson上提出一个问题,以添加字符串编写配置选项-
或至少从开发团队那里获得一些意见。我确实相信他们非常了解这种行为,并通过设计使它像这样工作,但是他们也可以对此有所了解(我现在唯一想到的是,他们目前有更多的性能,无法再增加在编写字符串之前进行转换,但这是一个微不足道的猜测)。
以上是 Gson Unicode字符转换为Unicode字符代码 的全部内容, 来源链接: utcz.com/qa/423387.html