2

I am trying to serialize a Class like this:

[DataContract]
public class GenericFlow 
{
    [DataMember]
    public ISource Source { get; set; }
    [DataMember]
    public IList<IFilter> Filters { get; set; }
}

When i seralize an instance of this object, everything goes right but if i try to deserialize i get an error. I am using Mono 2.6 for my tests, this is the error on the mono platform :

Stacktrace:


Native stacktrace:

    /usr/bin/mono() [0x48563b]
    /usr/bin/mono() [0x4d275f]
    /lib/libpthread.so.0(+0xfb40) [0x7fd5f8d6eb40]
    /usr/bin/mono(mono_object_get_virtual_method+0x174) [0x4f5744]
    /usr/bin/mono() [0x555524]
    [0x41632228]

Debug info from gdb:

If i remove the DataMember Attribute from the Source Property, deserialization works.

Taking a look at the json string produced during serialization, i can see that each item in the Filters list include a "__type" attribute especifing the concrete class that implements IFilter. But this is not true with the property "Source" of type ISource where the property "__type" is missing, so the desarialization process doesnt know which concrete class use to recreate the property.

Since i dont have a windows box handy, i dont know if this is mono specific problem or maybe i am missing something here.

How should i tell DataContractJsonSerializer to include the "__type" attribute in the Source Property?

Thanks in advance,

UPDATE:

Investigating a bit more, now i can force DataContractJsonSerializer to always emit type information calling the constructor with alwaysEmitTypeInformation=true. But the problem persists. Any ideas?

dbc
  • 104,963
  • 20
  • 228
  • 340
Bolo
  • 41
  • 4
  • Yes. The concrete class that implements ISource is a DataContract. ISource itself can not have DataContract attribute since it is not a class (It is an interface).Thanks. – Bolo Mar 22 '11 at 13:07
  • I remember having to use a [ServiceKnownType] attribute, but that was on windows. – Fedearne Mar 22 '11 at 13:33
  • I have added ServiceKnownType attributes to ISource listings the concrete classes that implements it. It crash the same. – Bolo Mar 22 '11 at 13:49

1 Answers1

0

In my expereince with DataContractJsonSerializer and de-serializing complex objects (on windows) you need to include the __type hints in order for the de-serialization to work properly.

My DataContractJsonSerializer experience has been sending JSON to a WCF service

LoveGandhi
  • 402
  • 4
  • 13
  • I have updated the question. Now, I am able to send __type attribute on the ISource property but it still segfault on mono_object_get_virtual_method. I think i will make some tests on a windows machine to know if this is a mono specific problem. – Bolo Mar 22 '11 at 20:56