0

I use to serialize a complex object with some children anonymous properties with JSON.NET setting : TypenameHandling.All.

The issue is that since I added more anonymous class on the same assembly the anonymous type name change so I fail miserably when I try to deserialize the previous object.

{
    "$type": "System.Collections.Generic.List`1[[System.Object, mscorlib]], mscorlib",
    "$values": [
      {
        "$type": "<>f__AnonymousType2`5[[System.Int32, mscorlib],[System.Boolean, mscorlib],[System.String, mscorlib],[System.Int32, mscorlib],[System.Int32, mscorlib]], GlobalNamespace.Api",
        "dashLength": 6,
        "inside": true,
        "label": "OC",
        "lineAlpha": 1,
        "value": 100
      }
    ]
}

I cannot use this answer (Deserializing JSON into an object with Json.NET) as I want to recover the previous serialized object and I really need the TypenameHandling.All as I have same classes with same properties.

Pablo Honey
  • 1,074
  • 1
  • 10
  • 23
  • The obvious solution is simply *not* to use an anonymous type for this. It was always going to be brittle like this - it's not (IMO) a situation that's a good match for anonymous types. – Jon Skeet Nov 27 '17 at 15:37
  • Thanks. So no way to recover the previous serialized JSON apart for changing the $type field to an explicit Explicit type ? – Pablo Honey Nov 27 '17 at 15:45
  • Well you could parse it using LINQ to JSON instead to get a JObject you can then deal with as you like... but I'd avoid doing this in future. (And ideally, convert any existing documents into more robust ones as a one-off approach.) – Jon Skeet Nov 27 '17 at 15:46
  • Understood. I won't step into this mistake again. Thanks. – Pablo Honey Nov 27 '17 at 15:54

0 Answers0