I have a data model with a Dictionary<string, string> of attributes as follows:
public class Model
{
public string Name { get; set; }
public Dictionary<string, string> Attributes { get; set; }
}
Under certain rare circumstances, I am receiving JSON with duplicated property names for Attributes, e.g.:
{
"name":"Object Name",
"attributes":{
"key1":"adfadfd",
"key1":"adfadfadf"
}
}
I would like for an exception to be thrown in such a situation, however when I deserialize with Json.NET there is no error and the dictionary instead contains the last value encountered. How can I force an error in such a situation?
As a workaround, I am currently declaring attributes as a list of key/value pairs:
public List<KeyValuePair<string, string>> Attributes { get; set;
This requires me to serialize the attributes in the following format:
"attributes": [
{
"key": "key1",
"value": "adfadfd"
},
{
"key": "key1",
"value": "adfadfadf"
}
]
Then later I can detect the duplicate. However, I would prefer to use the more compact JSON object syntax rather than the JSON array syntax, and declare Attributes as a dictionary.