not serializing correctly when handling type names

Nov 4, 2009 at 7:31 PM

 

      [Test]
      public void SerializeObjectWithMultipleGenericLists()
      {
          var container = new Container
                              {
                                  In = new List<Product>(),
                                  Out = new List<Product>()
                              };
          var json = JsonConvert.SerializeObject(container, Formatting.Indented,
              new JsonSerializerSettings
                  {
                      NullValueHandling = NullValueHandling.Ignore,
                      TypeNameHandling = TypeNameHandling.All
                  });
          Assert.AreEqual(@"{
  ""$type"": ""Newtonsoft.Json.Tests.TestObjects.Container, Newtonsoft.Json.Tests.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null"",
  ""In"": {
    ""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.TestObjects.Product, Newtonsoft.Json.Tests.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"",
    ""$values"": []
  },
  ""Out"": {
      ""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.TestObjects.Product, Newtonsoft.Json.Tests.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"",
      ""$values"": []
    }
  }
}", json);

 

 

When TypeNameHandling = TypeNameHandling.All is set, and the object being serialized has properties such as ILists or arrays, the properties are not closed. That is, the closing } is ommitted.
I don't mind patching this, if someone could show me where to look.
See my test below.

Given the type
    using System.Collections.Generic;

    public class Container
    {
        public IList<Product> In { get; set; }
        public IList<Product> Out { get; set; }
    }
and the test
      [Test]
      public void SerializeObjectWithMultipleGenericLists()
      {
          var container = new Container
                              {
                                  In = new List<Product>(),
                                  Out = new List<Product>()
                              };

          var json = JsonConvert.SerializeObject(container, Formatting.Indented,
              new JsonSerializerSettings
                  {
                      NullValueHandling = NullValueHandling.Ignore,
                      TypeNameHandling = TypeNameHandling.All
                  });

          Assert.AreEqual(@"{
  ""$type"": ""Newtonsoft.Json.Tests.TestObjects.Container, Newtonsoft.Json.Tests.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null"",
  ""In"": {
    ""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.TestObjects.Product, Newtonsoft.Json.Tests.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"",
    ""$values"": []
  },
  ""Out"": {
      ""$type"": ""System.Collections.Generic.List`1[[Newtonsoft.Json.Tests.TestObjects.Product, Newtonsoft.Json.Tests.Silverlight, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"",
      ""$values"": []
    }
  }
}", json);

 

 

 

 

Coordinator
Nov 5, 2009 at 1:25 AM

Ooh, that's a nasty little bug. I must have missed writing a unit test to cover that particular feature.