fastJSON icon indicating copy to clipboard operation
fastJSON copied to clipboard

More flexible RegisterCustomType

Open friuns2 opened this issue 8 years ago • 0 comments

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();
    }
}

friuns2 avatar Nov 19 '17 18:11 friuns2