DataContractJsonSerializer - 在List <object> </object>中反序列化DateTime

时间:2012-02-13 19:10:24

标签: c# json datacontractjsonserializer

我在使用System.Runtime.Serialization.Json.DataContractJsonSerializer类反序列化List<object>中包含的DateTime实例时遇到问题。我似乎无法将DateTime反序列化为原始类型。 DataContractJsonSerializer始终将其反序列化为格式为"/Date(1329159196126-0500)/"的字符串类型。如果我通过使用强类型List<DateTime>运行它,它将序列化和反序列化很好,但是我正在寻找方法在{{1}的简单列表或数组中遇到序列化程序时识别并正确反序列化DateTimes }}

请注意,除了此列表将包含的基元和字符串之外,DateTimes是唯一的类型。 这是我用来测试它的代码片段。

object

4 个答案:

答案 0 :(得分:33)

在.NET Framework 4.5版中,DataContractJsonSerializer有一个构造函数,它接受一个DataContractJsonSerializerSettings对象,可用于设置DateTimeFormat

var ser = new DataContractJsonSerializer(typeof(CreateOmsEntryCommand),
              new DataContractJsonSerializerSettings
              {
                  DateTimeFormat = new DateTimeFormat("yyyy-MM-dd'T'HH:mm:ssZ")
              });

答案 1 :(得分:7)

这似乎是非常奇怪的行为,我的猜测是它源于DateTime不是一个在JSON中被识别的类型。但是,您可以滚动自己的IDataContractSurrogate来修改序列化/反序列化过程。

要使用此功能,请在为此创建序列化程序时修改示例代码:

var serializer = new DataContractJsonSerializer(typeof(List<object>), null, int.MaxValue, false, new DateTimeDataContractSurrogate(), true);

然后添加这个类:

public class DateTimeDataContractSurrogate : IDataContractSurrogate
    {
        private static readonly Regex dateRegex = new Regex(@"/Date\((\d+)([-+])(\d+)\)/");
        private static readonly DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public object GetCustomDataToExport(Type clrType, Type dataContractType)
        {
            // not used
            return null;
        }

        public object GetCustomDataToExport(System.Reflection.MemberInfo memberInfo, Type dataContractType)
        {
            // not used
            return null;
        }

        public Type GetDataContractType(Type type)
        {
            // not used
            return type;
        }

        public object GetDeserializedObject(object obj, Type targetType)
        {
            // for debugging
            //Console.WriteLine("GetDeserializedObject: obj = {0} ({1}), targetType = {2}", obj, obj.GetType(), targetType);

            // only act on List<object> types
            if (obj.GetType() == typeof(List<object>))
            {
                var objList = (List<object>)obj;

                List<object> copyList = new List<object>(); // a list to copy values into. this will be the list returned.
                foreach (var item in objList)
                {
                    string s = item as string;
                    if (s != null)
                    {
                        // check if we match the DateTime format
                        Match match = dateRegex.Match(s);
                        if (match.Success)
                        {
                            // try to parse the string into a long. then create a datetime and convert to local time.
                            long msFromEpoch;
                            if (long.TryParse(match.Groups[1].Value, out msFromEpoch))
                            {
                                TimeSpan fromEpoch = TimeSpan.FromMilliseconds(msFromEpoch);
                                copyList.Add(TimeZoneInfo.ConvertTimeFromUtc(epoch.Add(fromEpoch), TimeZoneInfo.Local));
                                continue;
                            }
                        }
                    }

                    copyList.Add(item); // add unmodified
                }

                return copyList;
            }

            return obj;
        }

        public void GetKnownCustomDataTypes(System.Collections.ObjectModel.Collection<Type> customDataTypes)
        {
            // not used   
        }

        public object GetObjectToSerialize(object obj, Type targetType)
        {
            // for debugging
            //Console.WriteLine("GetObjectToSerialize: obj = {0} ({1}), targetType = {2}", obj, obj.GetType(), targetType);
            return obj;
        }

        public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
        {
            // not used
            return null;
        }

        public System.CodeDom.CodeTypeDeclaration ProcessImportedType(System.CodeDom.CodeTypeDeclaration typeDeclaration, System.CodeDom.CodeCompileUnit compileUnit)
        {
            // not used
            return typeDeclaration;
        }
    }

答案 2 :(得分:5)

如果DataContractJsonSerializer不是必须,则以下是使用Json.Net的解决方案。

var list = new List<object> { 27, "foo bar", 12.34m, true, DateTime.Now };

string json = JsonConvert.SerializeObject(list);
var orgObj=JsonConvert.DeserializeObject<List<object>>(json);

这是Json字符串

[27,"foo bar",12.34,true,"\/Date(1329161615596+0200)\/"]

并且返回的类型包括longstringdoubleboolDateTime

答案 3 :(得分:3)

您可以在序列化之前将 DateTime.Now 转换为字符串 反序列化后将其转换回 DateTime

转换为字符串:

string dateAsString = Convert.ToString(DateTime.Now);

反序列化后转换回DateTime:

DateTime dateTime = Convert.ToDateTime(deserializedList[4]);

所以整个代码就像:

  string dateAsString = Convert.ToString(DateTime.Now);
  var list = new object[] { 27, "foo bar", 12.34m, true, dateAsString };

  var serializer = new DataContractJsonSerializer(typeof (List<object>));

  using (MemoryStream ms = new MemoryStream())
  {
    serializer.WriteObject(ms, list);
    ms.Position = 0;
    var deserializedList = serializer.ReadObject(ms) as List<object>;
    DateTime dateTime = Convert.ToDateTime(deserializedList[4]);
  }