mgholam / fastJSON

Smallest, fastest polymorphic JSON serializer
https://www.codeproject.com/Articles/159450/fastJSON-Smallest-Fastest-Polymorphic-JSON-Seriali
MIT License
479 stars 147 forks source link

More flexible RegisterCustomType #63

Open friuns2 opened 6 years ago

friuns2 commented 6 years ago

Instead of converting string to object, now it can convert any known type to object

here is an example with result {"$types":{"ConsoleApp8.Test":"1"},"$type":"1","custom":3}


using System;
using System.Diagnostics;
using System.IO;
using fastJSON;

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            JSON.RegisterCustomType<CustomType, int>(a => a.value, a => new CustomType() { value = a });

            var json = Serializer.Serialize(new Test() { custom = new CustomType() { value = 3 } });
            Console.WriteLine(json);
            var t = Serializer.Deserialize<Test>(json);
            Debug.Assert(t.custom.value == 3);

            Console.ReadLine();
        }
    }
    public struct CustomType
    {
        public int value;
    }
    public class Test
    {
        public CustomType custom = new CustomType();
    }
}