0

I'm having difficulties using Newtonsoft when it comes to serialize objects that came from the server side.

I have a collection List<ObjectExample> with two elements, if some of those elements of that ObjectExample are the same, it only serializes one of them.

Examples below:

(since "Nome da Entidade" or "Tipo de Entidade" are different it shows, if not it only show one, but they are different.)

"Nome da Entidade" and "Tipo de Entidade" are referenced to tables in database.

enter image description here

I'm using linq to return the values and then serialize them like this:

json = JsonConvert.SerializeObject(ins /*LIST*/, Formatting.Indented,
       new JsonSerializerSettings
       {
           PreserveReferencesHandling = PreserveReferencesHandling.Objects,
           DefaultValueHandling = DefaultValueHandling.Include,
           ObjectCreationHandling = ObjectCreationHandling.Auto
       });
ViVi
  • 4,339
  • 8
  • 29
  • 52
PTLearner
  • 63
  • 8
  • 1
    I have no idea what you're trying to show us with your example. Could you please make it clearer, include more details, etc.? – JLRishe Sep 03 '16 at 16:42

1 Answers1

0

I don't understand your example, but it looks like this is happening because you're using PreserveReferencesHandling.Objects. If you look at the JSON, you will probably see $ref references in it referencing the objects that have already been serialized.

Here are some code samples for how you can repconstitute the references within your JavaScript.

Community
  • 1
  • 1
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Sorry for my example, this is exactly what happens! but if i don't have PreserveReferencesHandling.Objects it will cause me the other problem of circular references – PTLearner Sep 03 '16 at 16:48
  • @PTLearner Ok, then you either need to copy your objects to something that doesn't have circular references and serialize _that_, or take the other option I described and resolve the references when you deserialize the JSON. I think those are the two options. – JLRishe Sep 03 '16 at 16:58