Deserialisation of derived class work, but not with derived exception

Feb 7, 2011 at 9:38 AM

Hi

 

Ich have a problem to deserialize a derived exception.

    public class obase
    {
        public int A;
    }

    public class o1: obase
    {
        public int B;
    }

    public class s
    {
        public obase O;
        public Exception ex;
    }

    class Program
    {


        private static readonly JsonSerializer JsonSerializer = new JsonSerializer
                                                                    {
                                                                        NullValueHandling = NullValueHandling.Ignore,
                                                                        TypeNameHandling = TypeNameHandling.All
                                                                    };
        static void Main(string[] args)
        {
            // required for datetime serialization
            JsonSerializer.Converters.Add(new IsoDateTimeConverter());

            var toSer = new s() {X = 4, O = new o1 {A = 1, B = 2}, ex = new NotImplementedException()};

            var stream = new MemoryStream();
            using (var streamWriter = new BsonWriter(new NonClosingStreamWrapper(stream)))
            {
                JsonSerializer.Serialize(streamWriter, toSer);
                streamWriter.Flush();
            }

            stream.Position = 0;

            using (var streamReader = new BsonReader(new NonClosingStreamWrapper(stream)))
            {
                var des = JsonSerializer.Deserialize(streamReader, typeof(s));
            }
       
        }
    }

in s.O the o1 object is deserialized, but in exception a system.Exception is deserialized.

Can anybody help?

thx