Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Serializing objects to JSON with properties that return interfaces (http://www.velocityreviews.com/forums/t717693-serializing-objects-to-json-with-properties-that-return-interfaces.html)

Ra Carter 03-14-2010 02:18 PM

Serializing objects to JSON with properties that return interfaces
 
I want to serialize and deserialize an object to and from JSON. My
code looks something like this:

http://pastebin.com/yB5idwNq

This gives me an error like this:

System.Runtime.Serialization.SerializationExceptio n: Type 'MyObject1'
with data contract name 'MyObject1:http://schemas.datacontract.org/
2004/07/namespace' is not expected. Add any types not known statically
to the list of known types - for example, by using the
KnownTypeAttribute attribute or by adding them to the list of known
types passed to DataContractSerializer.

I am only using ISerializable because [Serializable] [DataContract]
would not work of course. The main problem I think I am having is that
I have declared my return type as an interface.

I don't want to change my objects and it is actually important that I
return the interface and not the implementation for various reasons.
What can be done? if Serializable or DataContract attributes won't
work automatically, can I pull this off manually using Serializable?

sloan 03-17-2010 02:36 AM

Re: Serializing objects to JSON with properties that return interfaces
 
http://social.msdn.microsoft.com/For...f-c0859e041e85
http://blogs.msdn.com/sowmy/archive/...nowntypes.aspx

Please post your solution after you figure out, so others can learn from it.





"Ra Carter" <spacemonkeyiv@gmail.com> wrote in message
news:172f43fb-c4e1-47b6-b433-47600b46525b@k17g2000yqb.googlegroups.com...
>I want to serialize and deserialize an object to and from JSON. My
> code looks something like this:
>
> http://pastebin.com/yB5idwNq
>
> This gives me an error like this:
>
> System.Runtime.Serialization.SerializationExceptio n: Type 'MyObject1'
> with data contract name 'MyObject1:http://schemas.datacontract.org/
> 2004/07/namespace' is not expected. Add any types not known statically
> to the list of known types - for example, by using the
> KnownTypeAttribute attribute or by adding them to the list of known
> types passed to DataContractSerializer.
>
> I am only using ISerializable because [Serializable] [DataContract]
> would not work of course. The main problem I think I am having is that
> I have declared my return type as an interface.
>
> I don't want to change my objects and it is actually important that I
> return the interface and not the implementation for various reasons.
> What can be done? if Serializable or DataContract attributes won't
> work automatically, can I pull this off manually using Serializable?





All times are GMT. The time now is 09:04 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.