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

Serialization of Enumerable Interface #222

Closed cubed-it closed 5 years ago

cubed-it commented 5 years ago
   public class SimpleClass : ISimpleClass
   {
      public string SimpleClassProp { get; set; }
      public string SimpleClassBaseProp { get; set; }
   }

   public interface ISimpleClass : ISimpleClassBase
   {
      string SimpleClassProp { get; set; }
   }
   public interface ISimpleClassBase
   {
      string SimpleClassBaseProp { get; set; }
   }

            var jsonSettings = new NetJSON.NetJSONSettings() { DateFormat = NetJSON.NetJSONDateFormat.ISO, SkipDefaultValue = false, UseEnumString = true, IncludeTypeInformation = true};
            var isc = (ISimpleClassBase)new SimpleClass();
            var iscEnumerable = new List<ISimpleClassBase>(){ new SimpleClass() };
            var iscs = NetJSON.NetJSON.SerializeObject(isc, jsonSettings);
            var iscsE = NetJSON.NetJSON.SerializeObject(iscEnumerable, jsonSettings); 

While IncludeTypeInformation serialize all information for an single object of an interface it does not provide all properties for a enumerable list of an interface.

iscs = {"SimpleClassProp":null,"SimpleClassBaseProp":null} iscsE = [{"SimpleClassBaseProp":null}]

It would be nice if all fields were also available for lists.

rpgmaker commented 5 years ago

Here is the result. I had to make one change to make it work properly since it only supported non abstract class. So i allowed interface. Maybe i should allow abstract types too. The problem is deserialization of interface/abstract type. I am working on fixing it to properly use the $type embedded

image

rpgmaker commented 5 years ago

If you pull the latest code in the branch that i just pushed. You can get the same result in my screenshot. Also not that you have to use NetJSON.IncludeTypeInformation = true instead of using the settings. Because I have not been able to make it work with the settings object so far since it is mostly runtime compilation changes that needs to be done. I am trying to generate less code

rpgmaker commented 5 years ago

I was able to make it work now with interface and abstract type.

image

rpgmaker commented 5 years ago

I take that back. It does not work with abstract type properly. There is a current bug that i have. I will try to work more on it once i get IncludeTypeInformation as part of the property of netjson setting object

rpgmaker commented 5 years ago

Please test and let me know if it works for you. Thanks

rpgmaker commented 5 years ago

If it works for you then i can create a nuget package for it.

cubed-it commented 5 years ago

Hey, sorry for the delay, I was very busy with other problems ;) Looks good so far! Interfaces support is suficent for my purpose - but all you said may of cause be interesting for the future or someone else I guess. Great work!