0

In one of my projects I have a class that contains a lot of properties and a lot of interfaces. When the class is exposed using a Web API, the browser version displays the XML-serialized version of the class, which is okay. But when I use it in JavaScript, the JSON version is used and I get the NewtonSoft-related exception "An item with the same key has already been added." Because the XML-serialization does work, I assume it is caused by a duplicate class property name, but I do not know how to find the culprit.

My question is: How can I determine which class property causes this exception?

Prutswonder
  • 9,894
  • 3
  • 27
  • 39
  • your model class contains the same property twice. Please look at this also http://stackoverflow.com/questions/5648060/an-item-with-the-same-key-has-already-been-added – Asif Bhutto Apr 14 '14 at 12:25
  • @bhutto I know that, but I want to find out which property that is. As I said, it is a large class with a lot of properties. – Prutswonder Apr 14 '14 at 12:44
  • Please show us/post code that class code, how we will know which property is twice in your code – Asif Bhutto Apr 14 '14 at 13:01
  • @bhutto I haven't posted the class, because it doesn't matter. I do not want to know **which** property it is, I want to know **how** I can find it. – Prutswonder Apr 14 '14 at 13:21

1 Answers1

0

Is another thread modifying your structure while you serialize it? I observed DataContractJsonSerializer generate either "Collection was modified; enumeration operation may not execute." or just create a JSON with duplicate keys, which in turn generates "An item with the same key has already been added." when you try to read it.

Yaniv
  • 504
  • 4
  • 10
  • Unfortunately not, it is a single-thread application. If I remove all properties in the class, the exception is not thrown. But because the class has a lot of properties, I'm looking for a smart way to find the property/properties that cause(s) the exception. – Prutswonder Jun 18 '14 at 06:41