DataContractJsonSerializer set value extension point
- by svinto
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
namespace ConsoleApplication1 {
internal class Program {
private static void Main(string[] args) {
var pony = new Pony();
var serializer = new DataContractJsonSerializer(pony.GetType());
var example = @"{""Foo"":null}";
var stream = new MemoryStream(Encoding.UTF8.GetBytes(example.ToCharArray()));
stream.Position = 0;
pony = (Pony) serializer.ReadObject(stream);
// The previous line throws an exception.
}
}
[DataContract]
public class Pony {
[DataMember]
private int Foo { get; set; }
}
}
Sometimes the serialization throws a casting error on Int32s being set to null. Is there any way to hook into the Json-serializer?