locked
Serializing JSON object array when application suspends RRS feed

  • Question

  • Hi,

    I'm trying to build a Metro app using HTML/JS. The application data is stored in an array of JSON objects. What I want to do is persist that data when the application suspends. Is there an inbuilt function which will let me serialize this array or do I need to write my own code (or search for some JS serialization function).

    Thanks

    Gaurav

    Monday, December 26, 2011 3:39 PM

Answers

  • You can use JSON.stringify(object) where object is your javascript object to turn an object into its string representation.

    You can then use JSON.parse(string) to turn that string back into a javascript object later.

    • Proposed as answer by Joe Levy_ Tuesday, December 27, 2011 3:58 AM
    • Marked as answer by Gaurav Mantri Tuesday, December 27, 2011 7:36 AM
    Tuesday, December 27, 2011 3:57 AM
  • JSON.stringify and JSON.parse are standard functions (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/stringify) so you can use them without any external library.

    I personally think JSON would be better as it can be easily compiled by javascript into objects and is more lightweight than XML. However, you can't store functions in JSON, as far as I know, so if you need to store functions you will have to find another solution.

    • Marked as answer by Gaurav Mantri Tuesday, December 27, 2011 7:36 AM
    Tuesday, December 27, 2011 7:20 AM

All replies

  • You can use JSON.stringify(object) where object is your javascript object to turn an object into its string representation.

    You can then use JSON.parse(string) to turn that string back into a javascript object later.

    • Proposed as answer by Joe Levy_ Tuesday, December 27, 2011 3:58 AM
    • Marked as answer by Gaurav Mantri Tuesday, December 27, 2011 7:36 AM
    Tuesday, December 27, 2011 3:57 AM
  • Thanks Joe. Just a quick question (since I am not in front of a computer with Win 8, otherwise I would have tried it myself :)): Is this part of standard JSON implementation (in the browser or Metro apps) or do I need to download any external library. One link that I stubled upon is: https://github.com/douglascrockford/JSON-js.

    Also do you think it would be better to persist the application data in serialized JSON format or should I serialize that array into an XML and save it. Your recommendations would be highly appreciated.

    Regards

    Gaurav

    Tuesday, December 27, 2011 4:19 AM
  • JSON.stringify and JSON.parse are standard functions (https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/JSON/stringify) so you can use them without any external library.

    I personally think JSON would be better as it can be easily compiled by javascript into objects and is more lightweight than XML. However, you can't store functions in JSON, as far as I know, so if you need to store functions you will have to find another solution.

    • Marked as answer by Gaurav Mantri Tuesday, December 27, 2011 7:36 AM
    Tuesday, December 27, 2011 7:20 AM