rpgmaker / NetJSON

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

NetJSON cannot serialize model casted to object #212

Closed ZOXEXIVO closed 5 years ago

ZOXEXIVO commented 5 years ago

I tried to replace Jil serilizer by NetJSON, but it not worked for simple cases! It my second attemp to use it (first was 2-3 years ago).

How you test your serializer?

I have some JsonExtensions that use some serialization settings. I can use many other serializers and it all works well, except NetJSON. It not worked!

How can develop serializer, that fail serialization of simple objects?

//P.S It serialize array of simple objects as [,,,,,,,]

rpgmaker commented 5 years ago

Can you be more specific for the simple object scenario?

rpgmaker commented 5 years ago

You can pull down the code. There is 90+ test for it at the moment. Thanks

ZOXEXIVO commented 5 years ago

My project simply serialize List of SomeModel with

 private static readonly NetJSONSettings Options = new NetJSONSettings
 {
            CamelCase = true
  };

   public static string ToJson(this object obj)
   {
            return NetJSON.NetJSON.Serialize(obj, Options);
   }
public class SomeModel
    {
        public MentionModel()
        {
            Related = new RelatedSomeModel();
        }

        public long Id { get; set; }

        public string Title { get; set; }

        public string Text { get; set; }

        public DateTime Date { get; set; }

        public OtherModel3 Mode3 { get; set; }

        public OtherModel Model1 { get; set; }

        public OtherModel2 Model2 { get; set; }

        public double[] Location { get; set; }

        public int SomeCount{ get; set; }

        public string SourceUrl { get; set; }

        public bool IsApproved { get; set; }

        public RelatedSomeModelRelated { get; set; }

        public DateTime? SomeDate{ get; set; }

        public class RelatedSomeModel
        {
            public RelatedSomeModel()
            {
                Players = new List<int>();
                Clubs = new List<int>();
            }

            public List<int> SomeCollection { get; set; }
            public List<int> SomeCollection { get; set; }
        }
    }

Result:

image

rpgmaker commented 5 years ago

Thanks for the example. Could you please provide a fully working example if possible?

Thanks,

rpgmaker commented 5 years ago

A working example with the failure case will be nice for me to debug the issue and resolve. Thanks

ZOXEXIVO commented 5 years ago

It caused only if model passed as object. Object type must be provider for generic ToJson extension, that worked for all serializers

public class MentionModel
    {
        public long Id { get; set; }

        public string Title { get; set; }

        public string Text { get; set; }
    }

    class Program
    {
        private static readonly NetJSONSettings Options = new NetJSONSettings
        {
            CamelCase = true
        };

        static async Task Main(string[] args)
        {
            object model = new List<MentionModel>
            {
                new MentionModel
                {
                    Text = "test",
                    Id = 23232,
                    Title = "test",
                },
                new MentionModel
                {
                    Text = "test",
                    Id = 23232,
                    Title = "test",

                }
            };

            var resultStr = NetJSON.NetJSON.Serialize(model, Options);

           //resultStr = "[,]"
        }
    }
rpgmaker commented 5 years ago

That is normal. It is not expected to work. It is by design

rpgmaker commented 5 years ago

Object serialization only support primitive type as the wiki describe. It is not Netjson fully broken, if you specific the type via typeof as an additional parameter it will work fine. Please change the title as it does not match what you are describing.😀

Thanks,

rpgmaker commented 5 years ago

Would it not be best to manually call .GetType within your code and use the overload that accept system.Type?

Thanks,

rpgmaker commented 5 years ago

image

ZOXEXIVO commented 5 years ago

netcoreapp2.2 console application

image

rpgmaker commented 5 years ago

You need to use SerializeObject method since that will do the get type of the model for you underlying it. I can push the changes to nuget for you.

rpgmaker commented 5 years ago

I have pushed the changes to nuget. So you can now use the SerializeObject method now. It is in 1.2.10.1 version

ZOXEXIVO commented 5 years ago

@rpgmaker it works! thank you