locked
Deserialize to object

    Question

  • Hi!

    I found the below code for how to deserialize data from a file. But the code only returns a string representing the data, not the original object. How can I get it to deserialize into the same object as it was serialized from?

    Thanks, Sigurd F

    	    string content = String.Empty;
    
                var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);
                using (StreamReader reader = new StreamReader(myStream))
                {
                    content = await reader.ReadToEndAsync();
                }
                var obj = App.Current as App;
                //obj.objectGraph = content;

    Tuesday, February 24, 2015 10:17 PM

Answers

  • Hi Sigurd,

    According to this page, https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx, you need to read the json file to MemoryStream object, then use ReadObject method to parse to the available class. Code snippet may look like the following.

    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
    
                DataContractJsonSerializer ser = new DataContractJsonSerializer((typeof(WordsItem));
    
                item = ser.ReadObject(ms) as WordsItem;
    

    Try and let me know the result.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Sunday, March 1, 2015 7:03 AM
    Moderator

All replies

  • can you show the code how you serialized? wondering which component you use for that

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Tuesday, February 24, 2015 10:25 PM
  • Here are both the code for serialization and the classes I serializes.

           private async Task writeJsonAsync()
            {
                // Notice that the write is ALMOST identical ... except for the serializer.
    
                //var myCars = buildObjectGraph();
                var groups = await WordsDataSource.GetGroupsAsync();
                this.DefaultViewModel["WordsGroups"] = groups;
    
    
                var serializer = new DataContractJsonSerializer(typeof(IEnumerable<WordsGroup>));
                using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
                              JSONFILENAME,
                              CreationCollisionOption.ReplaceExisting))
                {
                    serializer.WriteObject(stream, groups);
                }
    
                resultTextBlock.Text = "Write succeeded";
            }
       [DataContract]
        public class WordsItem
        {
            public WordsItem(String no, String en, String sp)
            {
                this.No = no;
                this.En = en;
                this.Sp = sp;
            }
    
            [DataMember]
            public string No { get; private set; }
            [DataMember]
            public string En { get; private set; }
            [DataMember]
            public string Sp { get; private set; }
        }
    
    
        [DataContract]
        public class WordsGroup
        {
            public WordsGroup(int id, String lesson, String info)
            {
                this.Id = id;
                this.Lesson = lesson;
                this.Info = info;
                this.Items = new ObservableCollection<WordsItem>();
            }
    
            [DataMember]
            public int Id { get; private set; }
            [DataMember]
            public string Lesson { get; private set; }
            [DataMember]
            public string Info { get; private set; }
            [DataMember]
            public ObservableCollection<WordsItem> Items { get; set; }
    
    
            public override string ToString()
            {
                return this.Lesson;
            }
        }
    Thanks, Sigurd F
    Tuesday, February 24, 2015 10:31 PM
  • Hi Sigurd,

    According to this page, https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx, you need to read the json file to MemoryStream object, then use ReadObject method to parse to the available class. Code snippet may look like the following.

    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
    
                DataContractJsonSerializer ser = new DataContractJsonSerializer((typeof(WordsItem));
    
                item = ser.ReadObject(ms) as WordsItem;
    

    Try and let me know the result.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Sunday, March 1, 2015 7:03 AM
    Moderator