locked
SerializationException using JSON Serializer RRS feed

  • Question

  • I'm getting a SerializationException with the following message when I deserialize a simple object serialized with JSON:

    Expecting element 'root' from namespace ''.. Encountered 'None'  with name '', namespace ''.

    Any ideas what's wrong?  Here is the code:

    namespace

     

     

    JSONSerialization

    {

     

     

    class Program

    {

    [

     

    DataContract ]

     

     

    public class Info

    {

    [

     

    DataMember ]

     

     

    public string version;

    }

     

     

    static void Main( string [] args)

    {

     

     

    Info apiInfo = new Info () {version = "1.0" };

     

     

    MemoryStream stream1 = new MemoryStream ();

     

     

    DataContractJsonSerializer ser = new DataContractJsonSerializer ( typeof ( Info ));

    ser.WriteObject(stream1, apiInfo

     

    );

     

     

    Info proxyReceived = ( Info )ser.ReadObject(stream1);

     

     

    Console .WriteLine( string .Format( "{0}: {1}" , "API version" , proxyReceived.version));

    }

    }

    }

    Monday, July 19, 2010 9:21 PM

All replies

  • I think I hit this recently, is it that you didn't reset your memory stream to the beginning, so it is trying to read from the end of the stream?
    Tim
    Thursday, August 12, 2010 7:11 AM
  • Ha, ha, ha. Thanks tilovell !!!
    Thursday, June 23, 2011 12:39 PM
  • Thank you.
    Thursday, November 29, 2012 2:05 PM