我正在尝试序列化一个包含字典作为其值之一的对象数组,并且我得到了这个运行时序列化异常:
Type 'System.Collections.Generic.Dictionary`2 ...with data contract name 'ArrayOfKeyValueOfstringanyType
is not expected. Consider using a DataContractResolver if you are using
DataContractSerializer or add any types not known statically to the list of
known types - for example, by using the KnownTypeAttribute attribute or by
adding them to the list of known types passed to the serializer.
这就是我试图完成任务的方式:
object[] taskArgs = new object[] { 1, 2 };
IDictionary<string, object> kwargs = new Dictionary<string, object>();
IDictionary<string, object> embed = new Dictionary<string, object>();
embed.Add("callbacks", null);
embed.Add("errbacks", null);
embed.Add("chain", null);
embed.Add("chord", null);
var knownTypes = new List<Type> { typeof(IDictionary<string, object>), typeof(object []), typeof(List<string>) };
//object[] arguments = new object[] { taskArgs, "{}", "{}" };
object[] arguments = new object[] { taskArgs, kwargs, embed };
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(object[]), knownTypes);
ser.WriteObject(stream1, arguments);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
string message = sr.ReadToEnd();
即使我尝试将typeof(IDictionary<string, object>)
添加到knownTypes
,也无法使用。
任何帮助将不胜感激。感谢。
答案 0 :(得分:1)
对于已知类型,您必须使用具体类型typeof(IDictionary<string, object>)
而不是接口类型var knownTypes = new List<Type> { typeof(Dictionary<string, object>), typeof(object[]), typeof(List<string>) };
:
import pandas as pd
import numpy as np
data = pd.DataFrame([
[1879.289,np.nan],[1879.281,np.nan],[1879.292,1],[1879.295,1],[1879.481,1],[1879.294,1],[1879.268,1],
[1879.293,1],[1879.277,1],[1879.285,1],[1879.464,1],[1879.475,1],[1879.971,1],[1879.779,1],
[1879.986,1],[1880.791,1],[1880.29,1],[1879.253,np.nan],[1878.268,np.nan],[1875.73,1],[1876.792,1],
[1875.977,1],[1876.408,1],[1877.159,1],[1877.187,1],[1883.164,1],[1883.171,1],[1883.495,1],
[1883.962,1],[1885.158,1],[1885.974,1],[1886.479,np.nan],[1885.969,np.nan],[1884.693,1],[1884.977,1],
[1884.967,1],[1884.691,1],[1886.171,1],[1886.166,np.nan],[1884.476,np.nan],[1884.66,1],[1882.962,1],
[1881.496,1],[1871.163,1],[1874.985,1],[1874.979,1],[1871.173,np.nan],[1871.973,np.nan],[1871.682,np.nan],
[1872.476,np.nan],[1882.361,1],[1880.869,1],[1882.165,1],[1881.857,1],[1880.375,1],[1880.66,1],
[1880.891,1],[1880.377,1],[1881.663,1],[1881.66,1],[1877.888,1],[1875.69,1],[1875.161,1],
[1876.697,np.nan],[1876.671,np.nan],[1879.666,np.nan],[1877.182,np.nan],[1878.898,1],[1878.668,1],[1878.871,1],
[1878.882,1],[1879.173,1],[1878.887,1],[1878.68,1],[1878.872,1],[1878.677,1],[1877.877,1],
[1877.669,1],[1877.69,1],[1877.684,1],[1877.68,1],[1877.885,1],[1877.863,1],[1877.674,1],
[1877.676,1],[1877.687,1],[1878.367,1],[1878.179,1],[1877.696,1],[1877.665,1],[1877.667,np.nan],
[1878.678,np.nan],[1878.661,1],[1878.171,1],[1877.371,1],[1877.359,1],[1878.381,1],[1875.185,1],
[1875.367,np.nan],[1865.492,np.nan],[1865.495,1],[1866.995,1],[1866.672,1],[1867.465,1],[1867.663,1],
[1867.186,1],[1867.687,1],[1867.459,1],[1867.168,1],[1869.689,1],[1869.693,1],[1871.676,1],
[1873.174,1],[1873.691,np.nan],[1873.685,np.nan]
])
已知类型列表中的类型与要生效的其他未知类型的对象的确切类型(由GetType()
返回)非常匹配。匹配基类类型或实现的接口类型是不够的,并且无论如何数据协定序列化程序都不支持序列化接口。