locked
DataContractJsonSerializer - modify json objects ? Not to replace the whole data?

    Question

  • Hello

    I'm struggling with the following problem.

    I've created an app which:

    1. Firstly, my app has a "Settings" frame, which gives possibility to save user data to JSON file. This action shouldn't be taken very often, user just provides this information to get some data from the web, just like an user & password data to log on. <- IT WORKS CORRECTLY.

    var myData = new List<Data>();
    myData.Add(New Data() { name = "SomeName", id = someid, id2 = someid2 });
    var serializer = new DataContractJsonSerializer(typeof(List<Data>));
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
    JSONFILENAME,
    CreateCollisionOption.ReplaceExisting))
    {
    serializer.WriteObject(stream,myData);
    }

    myData class has some additional 2 values - which user can't provide because, basically it is generated dynamically (after getting some data from the web).

    2. Main page of my application, reads json file (to get information which user provided in the settings frame). Then it connects to some website, uses data which user provided in the settings page and returns some data. <- IT WORKS CORRECTLY 

    3. In the last line of point1 I mentioned about 2 values which user can't provide because they are generated dynamically (after getting this data from the web). And that's my problem. After getting data I would like to save it in the JSON file. I I would like to display that data in the TextBoxes, when user runs the application again. It would be helpful when user is offline and he can't connect the website and retrieve updated data. Basically, I would like to store that downloaded data somewhere.

    I thought about Json file, to save it there.

    • Is it possible to modify just few values of my myData instance? I can't see other interesting CollisionOption than ReplaceExisting, so I don't know how to achieve that.
    • Maybe in the main frame, where data is gathered, I should read JSON file contents and create completely new objects basing on JSON + basing on data gathered from web. I know that it should work, but it seems to be a workaround and perhaps it's not a good practice of programming.
    • Other IDEAS???


    Thanks for all replies :)







    Friday, November 07, 2014 9:07 AM

All replies

  • Hi Luk86,

    Per my understanding, if you want to find a place to save and read information easily for your app, you can use LocalSettings as alternatives. You can use this object as a local backup of you JSON file. It can save your content until you remove your app.

    >Is it possible to modify just few values of my myData instance? I can't see other interesting CollisionOption than ReplaceExisting, so I don't know how to achieve that.

    Local settings is an array collection, you can use the following code snippet to modify the content.

    var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
    
    localSettings.Values["id"] = id1;
    

    Just try using LocalSettings, you will find it’s an easy way.

    If you still have questions, please free feel to let me know.

    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.

    Monday, November 10, 2014 3:23 AM
    Moderator
  • Hi Herro,

    Firstly, Thanks for your reply :)

    Yes, I knew about Local Settings, They are generally easier to implement.
    However, I decided to go on with json, although I've been thinking many times to give up....

    Maybe I will describe my app a little but more:

    1. SETTINGS PAGE
    -
    user can save his data like login and password there. Local json file is created there and replaces any other older file if existed previously.
    - when page is navigated (NavigatedTo) json file is being opened, read and user provided data is being placed in the textboxes on this page.

    2. MAIN PAGE
    - this page can connect to some website and download data basing on the user data saved in the "settings page". Then downloaded data is being placed in the textboxes. Moreover, this downloaded data is being saved to the same json file! So basing on my previous post  - it must read all over the json file again to get the data (login & pass), put this data to some temporary variables - in this case I create objects (there are also 4 values there - login, pass, gathered value1 & 2).... And save again new json file with gathered_value1 & gathered_value2.
    - when page is navigated (NavigatedTo) I would like to open json file again and read gathered_value1 & gathered_value2 and put them into the textboxes.

    The most important thing here is to display gathered values and keep and display them when user next time runs the application. Even when he doesn't have internet connection currently.

    3. What's saved to json ?
    login
    pass
    gathered_value1
    gathered_value2

    -------------

    Everything worked correctly on the emulator, but when I test my app on the device it crashes very often. Probably due to the fact that all Saves/Loads to json file are async and perhaps it fails as file is being used or something? Don't know.

    Also I have a general question about a thing which I do in the point 2 - everytime when I navigate to the main page, json is being read. I don't think that it's a good idea.... It would be better to gather that data only when application was closed and user executed it again, or when it was in the suspended mode. But how to achieve that? 


    • Edited by luk86 Sunday, November 16, 2014 9:04 PM
    Sunday, November 16, 2014 9:02 PM