问题
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