rpgmaker / NetJSON

Faster than Any Binary? Benchmark: http://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
MIT License
230 stars 29 forks source link

NetJSON.NetJSONTypeMismatchException: 'Unexpected type was encountered in JSON' #246

Closed AmirHJabari closed 7 months ago

AmirHJabari commented 1 year ago
namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var p = new Person()
            {
                Id = 1,
                Addresses = new Address[] { new Address(), null, new Address() }
            };

            var json = NetJSON.NetJSON.Serialize(p);
            var p2 = NetJSON.NetJSON.Deserialize<Person>(json); // <=======  Error

            Console.WriteLine("Hello, World!");
        }
    }

    public class Person
    {
        public int Id { get; set; }

        public Address[] Addresses { get; set; } // basically any class type
    }
    public class Address 
    {
        public int Id { get; set; } = 1;
    }
}

Hello as you can see in this simple example there is a major error. Basically, having null in an array of class types. @rpgmaker @wmjordan

rpgmaker commented 1 year ago

Thanks for the finding. The optimization to skip null is only called when dealing with the object property directly and not with collection items. This should be an easy fix.

rpgmaker commented 1 year ago

@AmirHJabari , can you try using List or IList to see if the behavior is similar in your test. I will take a look at it over the weekend when I get a chance. Thanks

AmirHJabari commented 1 year ago

@rpgmaker sure. Tested with this code and resulted to the same error.

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var p = new Person()
            {
                Id = 1,
                Addresses = new List<Address> { new Address(), null, new Address() }
            };

            var json = NetJSON.NetJSON.Serialize(p);
            var p2 = NetJSON.NetJSON.Deserialize<Person>(json); // <=======  Error

            Console.WriteLine("Hello, World!");
        }
    }

    public class Person
    {
        public int Id { get; set; }

        public List<Address> Addresses { get; set; } // basically any class type
    }
    public class Address 
    {
        public int Id { get; set; } = 1;
    }
}
rpgmaker commented 1 year ago

Thanks for verifying. I should be able to resolve it this weekend.

AmirHJabari commented 1 year ago

That would be great thank you.

rpgmaker commented 7 months ago

Sorry, I dropped off for a bit. I will be reviewing all the current open items. Thanks

rpgmaker commented 7 months ago

@AmirHJabari , I have fixed the issue. I will push it to a new version once i fix the other open issues. Thanks again for the patience. It was a minor issue with checking for null during deserialization, i was incrementing by 3 characters to skip null instead of 4 characters.

rpgmaker commented 7 months ago

@AmirHJabari , I have pushed the changes to version 1.4.4. Please test again and reopen if it is still an issue. Thanks