Deserialize然后Nullable类型DateTimeOffset?抛出异常 [英] When Deserialize then Nullable type DateTimeOffset? throw exception
问题描述
我的开发平台是Win7,VS.Net 2010,C#。
当我使用fastJSON.JSON在可空类型DateTimeOffset?时序列化自定义类时,它可以,但是当它反序列化序列化字符串,即fastJSON抛出异常。
fastJSON的版本是v2.0.10。
你可以测试它,当你定义一个DateTimeOffset?属性并设置DateTime.Now值。
当使用fastJSON.JSON.Instance.ToJSON()方法时它没关系,但是当使用fastJSON.JSON.Instance.ToObject()方法时,它抛出异常:无法将类型System.Collections.Generic.Dictionary`2 [System.String,System.Object]转换为类型System.String
测试类是这样的:
public static class 测试
{
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,
年龄= 16 ,
ShirtColor = ConsoleColor.Blue ,
Mode = StartMode.Manual,
PreviewExecuteionTime = null ,
NextExecuteionTime = DateTime.Now,
Teachers = < span class =code-keyword> new RefClass1 [ 2 ] {oTeacher,oTeacher2}
};
return oRet;
}
}
public class testclass
{
public string 名称{获得跨度>; set ; }
public int 年龄{获得跨度>; set ; }
public string 学校{获得跨度>; private set ; }
public ConsoleColor ShirtColor { get ; set ; }
public StartMode模式{ get ; 设置;}
public string ModeText
{
get
{
if (Mode = = StartMode.Auto)
{
return 0跨度>;
}
return 1 跨度>;
}
}
public DateTimeOffset? PreviewExecuteionTime { get ; set ; }
public DateTimeOffset? NextExecuteionTime { get ; set ; }
public RefClass1 []教师{获取; set ; }
}
public enum StartMode
{
Auto = 0 ,
手动= 1
}
public class RefClass1
{
public string Class1ID { get ; set ; }
public string Class1Name {获得跨度>; set ; }
public EnumObj1 Class1ObjType { get ; set ; }
public RefClass2 RefClass { get ; set ; }
}
public class RefClass2
{
public string Class2ID { get ; set ; }
public string Class2Name {获得跨度>; set ; }
}
public enum EnumObj1
{
A = 0 ,
B = 1
}
调用Test.Create()静态方法可以创建测试对象。</ blockquote>
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"解决方案the test class is like this :
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.
这篇关于Deserialize然后Nullable类型DateTimeOffset?抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!