如何强制
DataContractJsonSerializer接受
System.Json DateTime序列化格式(ISO 8601)?
问题是System.Json输出“2012-03-01T16:24:55.000”格式但DataContractJsonSerializer需要“/ Date(1329161615596 0200)/”格式.
我有这个错误:
反序列化xyz类型的对象时出错. DateTime内容’2012-03-01T16:24:55.000’不以’/ Date(‘和以’)结尾,以及JSON所需的.
解决方法
您可以编写一个适配器类,在反序列化期间预处理序列化数据,并将所有其他函数连接到密封的DataContractJsonSerializer类.
public class DataContractSystemJsonSerializer : XmlObjectSerializer { protected DataContractJsonSerializer innerSerializer; public DataContractSystemJsonSerializer(Type t) { this.innerSerializer = new DataContractJsonSerializer (t); } ... public override Object ReadObject(Stream stream) { Object obj = null; MemoryStream out = new MemoryStream(); Byte[] buf = new Byte[64]; stream.Read(buf,64); int i = 0; while(stream.Read(buf,i,1)) { convertDatesInBuffer(&buf,&i); out.write(buf,1); i = (i+1)%64; } return innerSerializer.ReadObject(out); } }