DataContractJsonSerializer and inherited objects

D

Dave Sanders

I have an object that has a [DataContract] attribute on it and several
[DataMembers]. This object inherits from another object that does NOT
have any data contract attributes, but whose properties trickle down
into the object I'm trying to serialize. This makes the serializer
give me errors telling me I need to add the DataContract to my parent
object.

Is there any way around this? Is there a way that I can tell it to
serialize my main object into JSON and just ignore those properties
that come from the parent object? I don't particularly need the
properties, and I can't add the attributes to the parent class.

Thanks
 
M

Michael Nemtsev [MVP]

Hello Dave,

AFAIK u can't do this,
If you have a data contract class which u gonna serialize u need to apply
the DataContract to all base classes to do this

U can't just take one class from hierarchy, apply DataContracts and serialize
it

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo


DS> I have an object that has a [DataContract] attribute on it and
DS> several [DataMembers]. This object inherits from another object
DS> that does NOT have any data contract attributes, but whose
DS> properties trickle down into the object I'm trying to serialize.
DS> This makes the serializer give me errors telling me I need to add
DS> the DataContract to my parent object.
DS>
DS> Is there any way around this? Is there a way that I can tell it to
DS> serialize my main object into JSON and just ignore those properties
DS> that come from the parent object? I don't particularly need the
DS> properties, and I can't add the attributes to the parent class.
DS>
DS> Thanks
DS>
 
D

Dave Sanders

HelloDave,

AFAIK u can't do this,
If you have a data contract class which u gonna serialize u need to apply
the DataContract to all base classes to do this

U can't just take one class from hierarchy, apply DataContracts and serialize
it

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog:http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

DS> I have an object that has a [DataContract] attribute on it and
DS> several [DataMembers]. This object inherits from another object
DS> that does NOT have any data contract attributes, but whose
DS> properties trickle down into the object I'm trying to serialize.
DS> This makes the serializer give me errors telling me I need to add
DS> the DataContract to my parent object.
DS>
DS> Is there any way around this? Is there a way that I can tell it to
DS> serialize my main object into JSON and just ignore those properties
DS> that come from the parent object? I don't particularly need the
DS> properties, and I can't add the attributes to the parent class.
DS>
DS> Thanks
DS>

Thanks. I think I'm going to roll my own implementation or modify
another one so that I can have it only serialize those properties that
are marked and ignore all others. This seems rather silly to me that
MS's implementation doesn't do this already frankly, but maybe there
is something I'm not understanding.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top