5

I have a problem when serializing inherited objects in Web API.

[DataContract] 
public class Item{
     [DataMember]
     public int ID{get; set;}
     [DataMember]
     public string Name{get; set;} }

[DataContract] 
public class SitecoreItem : Item{
     [DataMember]     
     public DbType SitecoreInstance{get; set;} }

I am trying to return the above mentioned SitecoreItem from WebAPI, but it screams serialization errors. What am I doing wrong?

1 Answer 1

4

This is no different than serialization in WCF. Just use KnownType

[DataContract] 
[KnownType(typeof(SitecoreItem))]
public class Item{
     [DataMember]
     public int ID{get; set;}
     [DataMember]
     public string Name{get; set;} }

[DataContract] 
public class SitecoreItem : Item{
     [DataMember]     
     public DbType SitecoreInstance{get; set;} 
}
Sign up to request clarification or add additional context in comments.

1 Comment

What if we don't have access to the code of Item? For example, Item is in a third-party library. How could we put KnownType above it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.