locked
Purpose of using SQLite vs storing JSON serialized text file? RRS feed

  • Question

  • User197547 posted

    Hi, I'm not to sure about how databases work, I just know that they store information and if you need access to the information it's there. But, my question is what is the point in using SQLite to store data on the phone VS just serializing the object I want into JSON format, and then storing that string on a text file. Then when I need to retrieve it I deserialize and access it? Does the database provide a much higher speed or is it another reason?

    Thanks

    Tuesday, March 1, 2016 1:31 AM

Answers

  • User12403 posted

    It all depends on the use case. If you have a lot of data and there are a lot of objects that are shared then a relational database is much more efficient. It also provides an option to use transactions to reverse changes in case something goes wrong (think about what happens if the JSON file errors out in the mid way and only writes half of the data). Then there is the issue of minor changes to the data. With a relational database you can update a single record quickly where as with JSON files you will most likely re-write the whole file.

    I usually opt for the middle road and use SQLite together with a JSON serializer. This way you don't have to create the whole db structure for every single class type. The serializer will convert the object into a byte array and it gets stored in place of the object. Then it is deserialized when read. SQLite.Net.Async package has support for IBlobSerializer and the test cases show how to use it: https://github.com/oysteinkrog/SQLite.Net-PCL/blob/master/tests/BlobSerializationTest.cs

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, March 1, 2016 2:49 AM

All replies

  • User12403 posted

    It all depends on the use case. If you have a lot of data and there are a lot of objects that are shared then a relational database is much more efficient. It also provides an option to use transactions to reverse changes in case something goes wrong (think about what happens if the JSON file errors out in the mid way and only writes half of the data). Then there is the issue of minor changes to the data. With a relational database you can update a single record quickly where as with JSON files you will most likely re-write the whole file.

    I usually opt for the middle road and use SQLite together with a JSON serializer. This way you don't have to create the whole db structure for every single class type. The serializer will convert the object into a byte array and it gets stored in place of the object. Then it is deserialized when read. SQLite.Net.Async package has support for IBlobSerializer and the test cases show how to use it: https://github.com/oysteinkrog/SQLite.Net-PCL/blob/master/tests/BlobSerializationTest.cs

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, March 1, 2016 2:49 AM
  • User197547 posted

    Thanks @SKall that really cleared things up.

    Monday, March 7, 2016 10:09 PM