Json.net no longer throws in case of duplicate

送分小仙女□ 提交于 2019-12-10 18:09:51

问题


I'm trying to upgrade my C# application from Newtonsoft.JSON 6 to the latest version (9.0.1).

I noticed a change of behavior when deserializing object containing duplicate elements, such as :

{
     "name": "test",
     "data":
     {
        "myElem": 1,
        "myElem": 2
    }
}

When deserializing such object, Json.net was previously throwing an ArgumentException. Now the deserialization succeed and it seems that it uses the value of the last duplicated key (hence "2" in above example).

From what I read, there is some debate around duplicates but the RFC only recommend to have unique keys.

I'd like to keep the previous behavior in order to validate the data received by my application. Is there a way to configure Json.net to do so? I tried to look into JsonSerializerSettings but did not find anything matching my need.

The code I use to deserialize JSON :

JsonSerializer.Create(new JsonSerializerSettings())
                          .Deserialize<JObject>(new JsonTextReader(new StringReader(rawJson));

or

JsonConvert.DeserializeObject<JObject>(rawJson);

来源:https://stackoverflow.com/questions/41942171/json-net-no-longer-throws-in-case-of-duplicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!