使用重复键来解析JSON时,Gson引发异常 [英] Make Gson throw exception on parsing JSON with duplicated key
问题描述
我正在用Gson解析简单的JSON对象。我希望它在键名重复时抛出一些错误。例如
{
a:2,
a:3
}
code>
在我的例子中,Gson解析这样的JSON并将a设置为3.我希望它抛出一些异常。
我知道我可以将JSON解析为map,然后Gson在这种情况下抛出异常,但前提是重复键不嵌套在地图中。如果我有例如JSON是这样的:
{
a:2,
b:{
dup:1,
dup:2
}
}
解析没有任何异常,我只有一个值为2的dup。
我能以某种方式设置Gson在这种情况下抛出错误吗?或者在 JsonObject
实例中有重复条目,这样我就可以自己检测到它(但我怀疑它是无效的 JsonObject
可重复使用的例子
pre $ c $ String $ json = {\a \:2,\a \:3};
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(json,JsonObject.class);
System.out.println(jsonObject);
列印出来
{a:3}
1)您可以稍微编辑一下gson的来源。这只是一个理解事物如何工作的建议。我不建议你在真实/生产环境中使用它。
Gson使用 com.google.gson.internal.LinkedTreeMap ,同时将json字符串解析为 JsonObject
。对于测试问题,您可以使用相同的名称和软件包名称将该类复制到您的项目中。
@Override
public V put(K key,V value ){
if(key == null){
throw new NullPointerException(key == null);
}
//我在这里编辑
if(find(key,false)!= null){
throw new IllegalArgumentException('+ key.toString ()+'是json的重复键!);
}
节点< K,V> created = find(key,true);
V result = created.value;
created.value = value;
返回结果;
$ / code>
2)另一个干净的解决方案是定义要映射到的自定义类你的json字符串。然后编写自定义 TypeAdapters
3)使用解串器?我不认为这是可能的。如果你尝试使用它,你会看到你已经拥有了一个jsonObject,你的重复键被处理为一个。
I'm parsing simple JSON object with Gson. I want it to throw some error when key name is duplicated. E.g.
{
a: 2,
a: 3
}
In my case, Gson parses such JSON and sets a to 3. I want it to throw some exception.
I know I can parse JSON as map, and then Gson throws exception in such case, but only if the duplicated key is not nested in the map. If I have e.g. JSON like this:
{
a: 2,
b: {
dup: 1,
dup: 2
}
}
Still, it is parsed without any exception and I have only one "dup" with value 2.
Can I somehow setup Gson to throw error in such case? Or to have duplicated entries in JsonObject
instance, so that I can detect it myself (but I doubt that, as it would be invalid JsonObject
)
Reproducible example
String json = "{\"a\":2, \"a\":3}";
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(json, JsonObject.class);
System.out.println(jsonObject);
prints out
{"a":3}
1) You may edit the source of gson a little bit. This is just a suggestion to understand how things work. I don't advice you to use this on a real/production environment.
Gson uses com.google.gson.internal.LinkedTreeMap while parsing a json string to a JsonObject
. For testing issues you can copy that class into your project with the same name and package name. And edit its put method to not allow duplicate keys.
@Override
public V put(K key, V value) {
if (key == null) {
throw new NullPointerException("key == null");
}
// my edit here
if(find(key, false) != null) {
throw new IllegalArgumentException("'" + key.toString() + "' is duplicate key for json!");
}
Node<K, V> created = find(key, true);
V result = created.value;
created.value = value;
return result;
}
2) Another clean solution is to define custom classes which are going to map to your json strings. Then write their custom TypeAdapters
3) Do it by using a Deserializer? I don't think it is possible. If you try to use it you'll see that you already have a jsonObject there which your duplicate keys are handled as one.
这篇关于使用重复键来解析JSON时,Gson引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!