Announcement

Collapse
No announcement yet.

[FIXED] [#115] Dictionary not serialized correctly to JSON

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    [FIXED] [#115] Dictionary not serialized correctly to JSON

    We are seeing an error trying to deserialize a dictionary using JSON.NET in ASP.NET that had been serialized by the Bridge.NET JSON library. We started seeing this issue after upgrading to version 17.1. Here is the JSON that bridge produces when serializing:

    {
    "Business": {
    "$type": "App.Address, App",
    "Attention": null,
    "City": "test",
    "PostalCode": "test",
    "StateCode": "test",
    "Status": 0,
    "Street": "test",
    "Suite": null
    },
    "$type": "System.Collections.Generic.Dictionary`2[[App.AddressType, App],[App.Address, App]], mscorlib"
    }

    This is the same dictionary that has been serialized the by the server JSON.NET library:

    {
    "$type": "System.Collections.Generic.Dictionary`2[[App.AddressType, App],[App.Address, App]], mscorlib",
    "Home": {
    "$type": "App.Address, App",
    "Street": "test",
    "City": "test",
    "StateCode": "test",
    "PostalCode": "test",
    "Suite": "test",
    "Attention": null,
    "Status": 0
    }
    }

    The main difference is the order of the fields in the JSON. It seems the problem deserializing the Bridge.NET json comes from the $type field of the dictionary not being first. If I move that field to the first position, deserialization works.
    Last edited by fabricio.murta; 2018-07-05 @ 08:02 PM.

    #2
    Hi. Thanks for the report. We will investigate.

    Could you share a Deck demonstrating the exact scenario to reproduce? Looks like this is a simple case, but getting a reproducible sample from you will ensure we're all dealing with the same code and we will add that sample to our unit test project.

    Once we have a Deck we'll create a GitHub Issue and associate the issue to this thread.

    Comment


      #3
      Here is a deck demonstrating the issue from the Bridge end:

      https://deck.net/79930cde9b8eaf20fd694f6ef52afb21

      https://dotnetfiddle.net/ROepQT

      using System;
      using System.Collections.Generic;
      using Newtonsoft.Json;
      
      public class Program
      {
          public static void Main()
          {
              var request = new Dictionary<AddressType, Address>()
              {
                  {
                      AddressType.Home,
                      new Address
                      {
                          Street = "test",
                          City = "test",
                          State = "test",
                          Zip = "test"
                      }
                  }
              };
      
              var config = new JsonSerializerSettings()
              {
                  TypeNameHandling = TypeNameHandling.Objects
              };
      
              var serialized = Newtonsoft.Json.JsonConvert.SerializeObject(request, Formatting.Indented, config);
      
              Console.WriteLine(serialized);
          }
      }
      
      public enum AddressType
      {
          Home,
          Business
      }
      
      public class Address
      {
          public string Street { get; set; }
          public string City { get; set; }
          public string State { get; set; }
          public string Zip { get; set; }
      }
      Running the same code in .NET will produce a different result (the $type property of the dictionary will come before any of the items in the dictionary).
      Last edited by geoffrey.mcgill; 2018-06-09 @ 12:57 AM.

      Comment


        #4
        Thanks for the sample. A new issue has been created and we will try to fix as soon as possible.

        https://github.com/bridgedotnet/Brid...son/issues/115

        Comment


          #5
          Here's a sample that actually demonstrates how to reproduce the exception in native .NET.

          https://dotnetfiddle.net/VBSLtZ

          https://deck.net/58ff280ec16303082f03a150a56b18b8

          Looks there is another exception being thrown in Bridge:

          System.Exception: RangeError: Maximum call stack size exceeded
          using Newtonsoft.Json;
          using System;
          using System.Collections.Generic;
          
          namespace Demo
          {
              public class Program
              {
                  public static void Main()
                  {
                      // Actual
                      var serialized = @"{
          ""Home"": {
          ""$type"": ""Address, Demo"",
          ""City"": ""London""
          },
          ""$type"": ""System.Collections.Generic.Dictionary`2[[AddressType, Demo],[Address, Demo]], mscorlib""
          }";
          
          //            // Expected
          //            var serialized = @"{
          //  ""$type"": ""System.Collections.Generic.Dictionary`2[[AddressType, Demo],[Address, Demo]], mscorlib"",
          //  ""Home"": {
          //    ""$type"": ""Address, Demo"",
          //    ""City"": ""London""
          //  }
          //}";
          
                      var values = JsonConvert.DeserializeObject<Dictionary<AddressType, Address>>(serialized);
          
                      Console.WriteLine(values[0].City);
          
                      Console.ReadLine();
                  }
              }
          
              public enum AddressType
              {
                  Home,
                  Business
              }
          
              public class Address
              {
                  public string City { get; set; }
              }
          }

          Comment


            #6
            Is there an ETA on when this might be fixed?

            Previously several features of our app had been blocked by:
            https://forums.bridge.net/forum/brid...key-is-an-enum

            Now these same features are being blocked by this issue.

            Comment


              #7
              Hello jguy! Sorry for the extended silence time, we've been caught on a lot of things here.

              We've just fixed the issue! So the fix will make it to the next Bridge.Newtonsoft.Json package release, which should coincide with next Bridge release!

              At the time this response was written, the planned releases were:
              Bridge: 17.2.0
              Bridge.Newtonsoft.Json: 1.9.0.

              Please notice this version release information may change without further updates here, but the issues' milestone in the respective github issue should be updated to reflect the exact version the fix went into.

              Thanks again for taking your time to report the issue!

              By the way, the different exception Geoffrey shown is no longer reproducible, but it does not match .NET behavior either. For that, we've logged a new issue under #129. As this should be a minor issue (as we don't expect either Bridge nor native .NET to emit this kind of code), and also it has a positive effect of having previously-bridge-crafted serialized strings to be able to be deserialized even after the change, then we let the side issue be fixed separately to the main issue reported here.
              Now Bridge-serialized strings should be fully compatible with native .NET's. And the other way around.
              Last edited by fabricio.murta; 2018-07-05 @ 08:05 PM. Reason: Clarifies what may "change without notice here".

              Comment

              Working...
              X