Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My develop platform is Win7,VS.Net 2010,C#.
When I using the fastJSON.JSON to serialize a custom class when the nullable type DateTimeOffset?,it is OK,but when it deserialize the serialized string,the fastJSON throw exception.

the fastJSON ‘s version is v2.0.10.
you can test it,when you define a DateTimeOffset? Property and set a DateTime.Now value.
when use fastJSON.JSON.Instance.ToJSON() method it's ok,but when use fastJSON.JSON.Instance.ToObject() method,it throw a exception:can not convert the type“System.Collections.Generic.Dictionary`2[System.String,System.Object]”to type“System.String”
Posted

1 solution

the test class is like this :
C#
public static class Test
    {
        public static testclass Create()
        {
            RefClass2 oRef = new RefClass2()
            {
                Class2ID = ",id2,",
                Class2Name = "name2"
            };
            RefClass2 oRef2 = new RefClass2()
            {
                Class2ID = "id3",
                Class2Name = "name3"
            };
            RefClass1 oTeacher = new RefClass1()
            {
                Class1ID = "id1",
                Class1Name = "name1",
                Class1ObjType = EnumObj1.A,
                RefClass = oRef
            };
            RefClass1 oTeacher2 = new RefClass1()
            {
                Class1ID = "\"id4\"",
                Class1Name = "name4",
                Class1ObjType = EnumObj1.B,
                RefClass = oRef2
            };
            testclass oRet = new testclass()
            {
                Name = "xiao ming",
                Age = 16,
                ShirtColor = ConsoleColor.Blue,
                Mode = StartMode.Manual,
                PreviewExecuteionTime = null,
                NextExecuteionTime = DateTime.Now,
                Teachers = new RefClass1[2] { oTeacher, oTeacher2 }
            };
            return oRet;
        }
    }

    public class testclass
    {
        public string Name { get; set; }

        public int Age { get; set; }

        public string School { get; private set; }

        public ConsoleColor ShirtColor { get; set; }

        public StartMode Mode{get;set;}

        public string ModeText
        {
            get
            {
                if (Mode == StartMode.Auto)
                {
                    return "0";
                }
                return "1";
            }
        }

        public DateTimeOffset? PreviewExecuteionTime { get; set; }

        public DateTimeOffset? NextExecuteionTime { get; set; }

        public RefClass1[] Teachers { get; set; }
    }

    public enum StartMode
    {
        Auto = 0,
        Manual = 1
    }

    public class RefClass1
    {
        public string Class1ID { get; set; }

        public string Class1Name { get; set; }

        public EnumObj1 Class1ObjType { get; set; }

        public RefClass2 RefClass { get; set; }
    }

    public class RefClass2
    {
        public string Class2ID { get; set; }

        public string Class2Name { get; set; }
    }

    public enum EnumObj1
    {
        A = 0,
        B = 1
    }


invoke the Test.Create() static method can create a testing object.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900