DataContractJsonSerializer -反序列化列表中的DateTime< object>

zu0ti5jz  于 2023-05-08  发布在  其他
关注(0)|答案(5)|浏览(125)

我在使用System.Runtime.Serialization.Json.DataContractJsonSerializer类反序列化List<object>中包含的DateTime示例时遇到了问题。我似乎无法让DateTime反序列化回原始类型。DataContractJsonSerializer总是将其反序列化为格式为"/Date(1329159196126-0500)/"的字符串类型。如果我使用强类型List<DateTime>运行它,它将序列化和反序列化很好,但是我正在寻找方法,让序列化器识别和正确反序列化DateTimes时遇到object的简单列表或数组。
注意,DateTimes是这个列表中除了原语和字符串之外唯一包含的类型。下面是我用来测试的代码片段。

var list = new List<object> { 27, "foo bar", 12.34m, true, DateTime.Now };
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>;
}
koaltpgm

koaltpgm1#

在.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")
              });
vvppvyoh

vvppvyoh2#

这看起来很奇怪,我猜这是因为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;
        }
    }
sq1bmfud

sq1bmfud3#

如果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

2nbm6dog

2nbm6dog4#

您可以在序列化之前将 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]);
  }
fjaof16o

fjaof16o5#

这个问题的解决方案是使用DataContractJsonSerializerSettingsDateTimeFormat更改为round trip o,以便保留所有内容,然后成功转换。
使用LocalUtc进行测试

public DataContractJsonSerializerSettings DataContractJsonSerializerSettings = new()
        {
            DateTimeFormat = new DateTimeFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"),
            SerializeReadOnlyTypes = true,
            EmitTypeInformation = EmitTypeInformation.Never,
            UseSimpleDictionaryFormat = true,
            IgnoreExtensionDataObject = true,
        };

相关问题