DataContractJsonSerializer无法反序列化Newtonsoft JsonSerializer

时间:2016-03-30 10:54:11

标签: c# json json.net datacontractjsonserializer

JsonSerializer在流的乞讨时写的前3个字节是什么意思,为什么DataContractJsonSerializer对它们有问题?

样品:

PendingIntent

反序列化最终会出现异常:Foo foo = new Foo(); using (MemoryStream stream = new MemoryStream()) { //serialize using JsonSerializer using (var streamWriter = new StreamWriter(stream, Encoding.UTF8, 4096, true)) using (var jsonWriter = new JsonTextWriter(streamWriter)) { JsonSerializer jsonSerializer = JsonSerializer.Create(); jsonSerializer.Serialize(jsonWriter, foo, typeof(Foo)); } // reset position stream.Seek(0, SeekOrigin.Begin); // deserialize using DataContractJsonSerializer using (var jsonReader = JsonReaderWriterFactory.CreateJsonReader(stream, Encoding.UTF8, XmlDictionaryReaderQuotas.Max, null)) { DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(Foo)); foo = (Foo)dataContractJsonSerializer.ReadObject(jsonReader); } }

0 个答案:

没有答案