DataContractJsonSerializer set value extension point

Posted by svinto on Stack Overflow See other posts from Stack Overflow or by svinto
Published on 2010-04-08T11:15:59Z Indexed on 2010/04/08 12:23 UTC
Read the original article Hit count: 323

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?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about datacontractjsonserialize