Serializing objects to JSON with properties that return interfaces

Discussion in 'ASP .Net' started by Ra Carter, Mar 14, 2010.

  1. Ra Carter

    Ra Carter Guest

    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.SerializationException: 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?
     
    Ra Carter, Mar 14, 2010
    #1
    1. Advertising

  2. Ra Carter

    sloan Guest

    http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/6b70e9f4-52bc-4fa9-a0ff-c0859e041e85
    http://blogs.msdn.com/sowmy/archive/2006/06/06/all-about-knowntypes.aspx

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





    "Ra Carter" <> wrote in message
    news:...
    >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.SerializationException: 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, Mar 17, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Frank
    Replies:
    0
    Views:
    259
    Florian Frank
    Jun 30, 2009
  2. sajuptpm
    Replies:
    2
    Views:
    374
    sajuptpm
    Dec 28, 2012
  3. Acácio Centeno
    Replies:
    1
    Views:
    302
    dieter
    Feb 15, 2013
  4. Bryan Britten
    Replies:
    9
    Views:
    286
    Bryan Britten
    May 28, 2013
  5. David Karr
    Replies:
    1
    Views:
    188
    David Karr
    Jun 17, 2013
Loading...

Share This Page