none
Consuming Json from GoogleReader

    Question

  • I have been trying to mimic GoogleReader's Json output, particularly the subscription listing. I started out creating classes for deserialization but I don't have a full grasp of the concepts. It crashes when I really try to consume the Json file, but when I generate my own serialized file it looks just right. I don't know what's going wrong. Thus far this is the progress I made:

     [DataContract(Name = "subscriptionsList")]
    [Serializable]
    public class Subscription
    {
        [DataMember(Name = "subscriptions")]
        public List<SubscriptionItem> Subscriptions = new List<SubscriptionItem>();
        public Subscription()
        {
        }

    }
    [DataContract(Name="subscriptionitem")]
    [Serializable]
    public class SubscriptionItem
    {
        [DataMember(Name = "id")]
        internal string id;
        public string Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
           
        [DataMember(Name="title")]
        internal string title;
        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }
           
        [DataMember(Name="sortid")]
        internal string sortid;
        public string SortId
        {
            get
            {
                return sortid;
            }
            set
            {
                sortid = value;
            }
        }
           
        [DataMember(Name="firstitemmsec")]
        internal string firstitemmsec;
        public string LatestUpdate
        {
            get
            {
                return firstitemmsec;
            }
            set
            {
                firstitemmsec = value;
            }
        }

        [DataMember(Name = "categories")]
        internal Categories categories;
        public Categories Categories
        {
            get
            {
                return categories;
            }
            set
            {
                categories = value;
            }
        }
    }

    [CollectionDataContract(Name = "categories")]
    public class Categories : List<string>
    {
        public Categories()
        : base()
        {
        }

        public Categories(string[] items)
        : base()
        {
            foreach (string item in items)
            {
                Add(item);
            }
        }
    }


    And here's the Exception thrown at me:

    System.Runtime.Serialization.SerializationException was unhandled
      Message=There was an error deserializing the object of type JsonDataModeling_Test.DataModels.Subscription. End element 'item' from namespace '' expected. Found element 'id' from namespace ''.
      Source=System.Runtime.Serialization
      StackTrace:
           at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
           at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName)
           at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(XmlDictionaryReader reader)
           at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)
           at JsonDataModeling_Test.Program.Main(String[] args) in D:\Mesh Shared Files\[CE123]\GReader\Project Integration Team\JsonDataModeling Test\JsonDataModeling Test\Program.cs:line 32
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.Xml.XmlException
           Message=End element 'item' from namespace '' expected. Found element 'id' from namespace ''.
           Source=System.Runtime.Serialization
           LineNumber=0
           LinePosition=0
           StackTrace:
                at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
                at System.Xml.XmlExceptionHelper.ThrowEndElementExpected(XmlDictionaryReader reader, String localName, String ns)
                at System.Xml.XmlBaseReader.ReadEndElement()
                at System.Xml.XmlBaseReader.ReadElementContentAsString()
                at System.Runtime.Serialization.XmlReaderDelegator.ReadElementContentAsString()
                at ReadcategoriesFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
                at System.Runtime.Serialization.Json.JsonCollectionDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at ReadsubscriptionitemFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] )
                at System.Runtime.Serialization.Json.JsonClassDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at ReadArrayOfsubscriptionitemFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString , CollectionDataContract )
                at System.Runtime.Serialization.Json.JsonCollectionDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, String name, String ns)
                at ReadsubscriptionsListFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] )
                at System.Runtime.Serialization.Json.JsonClassDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson context)
                at System.Runtime.Serialization.Json.XmlObjectSerializerReadContextComplexJson.ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator reader, String name, String ns, Type declaredType, DataContract& dataContract)
                at System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
                at System.Runtime.Serialization.XmlObjectSerializerReadContextComplex.InternalDeserialize(XmlReaderDelegator xmlReader, Type declaredType, DataContract dataContract, String name, String ns)
                at System.Runtime.Serialization.Json.DataContractJsonSerializer.InternalReadObject(XmlReaderDelegator xmlReader, Boolean verifyObjectName)
                at System.Runtime.Serialization.XmlObjectSerializer.InternalReadObject(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
                at System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator reader, Boolean verifyObjectName, DataContractResolver dataContractResolver)
           InnerException:
     

     

    A comparison of the Json original format:

    {"subscriptions":[{
    	"id":"feed/http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/rss.xml",
    	"title":"BBC Sport | Football | UK Edition",
    	"categories":[],
    	"sortid":"C6CEC826",
    	"firstitemmsec":"1178119338469"
    }]}
     
    and my output sample:
    {"subscriptions":[{
    	"categories":[],
    	"firstitemmsec":"0",
    	"id":"0",
    	"sortid":"0",
    	"title":"0th"
    }]}


     

    Monday, February 07, 2011 10:49 AM

All replies

  • These are all good materials and I'll definitely look into them. But I found an error in my code:

    The DataMember attributes have to precede the class attributes or properties that will be used in serialization. I fixed this and ran it through Windows Phone 7's JsonDataContractSerializer and it worked. It was deserialized.

    However, when I ran the same fixed code on the desktop .Net assemblies I still ran into the same exception. It seems strange to me.

    Monday, February 07, 2011 8:52 PM
  • However, when I ran the same fixed code on the desktop

    Hi,

    How's your fixed codes?

    Wednesday, February 09, 2011 10:21 PM
  • I found more bugs: I haven't been able to account for the elements contained in Categories to be more than simple strings.

    I'll be working on this, but it seems to me that the Windows Phone 7 deployment I made simply ignored the error. IMHO, it must be provision to data providers suddenly changing their mind on how their Json syndications are formatted. Correct me if I'm wrong though.

    Edit: I'll post my WP7 codes and the desktop tests I've been doing.

    Wednesday, February 09, 2011 11:45 PM