locked
Two way binding using a Json file in Windows Phone 8.1

    Question

  • I have struggled for more than a month serializing and deserializing a json file.

    I can successfully add one line to a json file and deserialize one line. But I can not add more than two lines (or records) to the same json file. And, deserialize successfully. I can add more lines but then I can not deserialize as I seem to add straneous characters to the file. This is building from one of the channel6 examples.

    Here is my code:

    try

    {

               

    varmyPersons = newList<Person>();

                

    DataContractJsonSerializerdeserialize = newDataContractJsonSerializer(typeof(List<Person>));

                

    varmyStream = awaitApplicationData.Current.LocalFolder.OpenStreamForReadAsync(m_PersonFileName);

                 myPersons = (

    List<Person>)deserialize.ReadObject(myStream);              

                 myPersons.Add(

    newPerson() { Id = GUIserialization.App.myglobalVariables.iCount, Make = p.Make, Model = p.Model });

                 myStream.Dispose();

                   

    varstream = awaitApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(

                m_PersonFileName,

    CreationCollisionOption.OpenIfExists);

                  

    DataContractJsonSerializerser = newDataContractJsonSerializer(typeof(List<Person>));

                  ser.WriteObject(stream, myPersons);

              

    // txtResult.Text = ser.ToString();


                txtYear.Text =

    "Try"+ GUIserialization.App.myglobalVariables.iCount;

               stream.Dispose();

               GUIserialization.

    App.myglobalVariables.iCount += 1;

            }

               

    catch{           

               

    varmyPersons = newList<Person>();

                 myPersons.Add(

    newPerson() { Id = GUIserialization.App.myglobalVariables.iCount, Make = p.Make, Model = p.Model });

                  

    DataContractJsonSerializerser = newDataContractJsonSerializer(typeof(List<Person>));

       

                    ser.WriteObject(stream1, myPersons);

                stream1.Position = 0;

               

    StreamReadersr = newStreamReader(stream1);

                txtYear.Text =

    "Catch"+ GUIserialization.App.myglobalVariables.iCount;

                txtResult.Text = sr.ReadToEnd();

                stream1.Dispose();

                GUIserialization.

    App.myglobalVariables.iCount += 1;

                }

    This generates this error:

    System.Runtime.Serialization.SerializationException was unhandled by user code
      HResult=-2146233076
      Message='{"Id":0,"Make":"VW","Model":"Jetta1"},{"Id":1,"Make":"VW","Model":"Beetle1"}][{"Id":1,"Make":"BMW","Model":"M41"}][{"Id":2,"Make":"Mercedes","Model":"MLK 3501"}]' is not a valid JSON primitive. This error can also occur when extraneous data is present after the JSON data.
      Source=System.ServiceModel.Web
      StackTrace:
           at System.Runtime.Serialization.Json.JavaScriptObjectDeserializer.BasicDeserialize()
           at System.Runtime.Serialization.Json.DataContractJsonSerializer.ReadObject(Stream stream)
           at GUIserialization.readJson.<OnNavigatedTo>d__0.MoveNext()
      InnerException:

    Thursday, April 23, 2015 6:40 AM

Answers

All replies

  • Can you post a working sample for testing? Post it to Onedrive and share it here.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, April 23, 2015 12:17 PM
    Moderator
  • Matt,

    Thank you for your reply.  I am doing per your request. This is a different example that does the exact same thing. I have modified the example to create the two way binding experience.  The example has both a readjsonAsync and deserializejsonAsync function. The read function just returns a stream and that works.

    The deserializejsonAsync function gets the error. That is the function that I am calling when the read button is pressed.

    The readjsonAsync returns a string and that works fine.

    Final question, why is it that the json file that I am creating is not permanent. It disappears from the emulator when I stop the app and restart it.  I event tried loading the app it to my phone. And, the file disappears if I power down my phone. BTW, I can add more than one record to the json file, I just can't deserialize it back to an object.

    Thank you Matt for your help.

    https://onedrive.live.com/redir?resid=86a2fd89b7bf0a23!6055&authkey=!ABC_jWs86vEuuk0&ithint=folder%2ctxt


    • Edited by prebeta Tuesday, April 28, 2015 5:34 AM
    Tuesday, April 28, 2015 5:23 AM
  • I don't see the json file as part of the project - where is it located?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, April 28, 2015 6:58 PM
    Moderator
  • Matt,

    the json file is created in the app. I only have the object definition in the car.cs file and the name of the file that gets created when you run the app is:

    privateconststringJSONFILENAME = "data.json";

    But, my idea, maybe I am wrong, is that with the object definition and the file name, I can have my app create the file.

    Thank you for your help.

    Wednesday, April 29, 2015 1:14 AM
  • I can't get your code to work at all - it crashes with  System.FormatException.  Is this the problem that you're running into?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, April 29, 2015 4:26 PM
    Moderator
  • No, that is not the error.

    I recreated the zip file for the entire project.

    Here is the new link.

    https://onedrive.live.com/redir?resid=86a2fd89b7bf0a23!6108&authkey=!AER9PRHIpvgie64&ithint=folder%2czip

    Thursday, April 30, 2015 5:21 AM
  • Matt,

    Sorry, got any updates?

    Monday, May 04, 2015 5:13 PM
  • went back to channel9 and watched http://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-22-Storing-and-Retrieving-Serialized-Data

    I was adding using statements to the class definition. I removed them and somehow it seems to be working now. I tested it twice and it is working.

    • Marked as answer by prebeta Thursday, May 14, 2015 1:52 AM
    Thursday, May 14, 2015 1:38 AM