您的位置:首页 > 编程语言 > Java开发

GSON一个小BUG的解决之道!

2017-10-13 00:00 176 查看
gson
会把
key:value
value
中的
=
解析成
/u003d`测试用例如下:

@Test
public void test() throws Exception {
DataSourceConfig result = new DataSourceConfig("com.mysql.jdbc.Driver", "password123456", "jdbc:mysql://DEVIP:PORT/DBNAME?characterEncoding=UTF-8", "username123456", "0");
String expected = "{\"driver\":\"com.mysql.jdbc.Driver\",\"password\":\"password123456\",\"url\":\"jdbc:mysql://DEVIP:PORT/DBNAME?characterEncoding=UTF-8\",\"username\":\"username123456\",\"port\":\"0\"}";
String unexpected = "{\"driver\":\"com.mysql.jdbc.Driver\",\"password\":\"password123456\",\"url\":\"jdbc:mysql://DEVIP:PORT/DBNAME?characterEncoding\\u003dUTF-8\",\"username\":\"username123456\",\"port\":\"0\"}";
assertNotEquals(expected, new Gson().toJson(result));
assertEquals(unexpected, new Gson().toJson(result));
}

可以把其中的new Gson换个方式建出来就OK了!
new GsonBuilder().disableHtmlEscaping().create().toJson(result)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java Gson