2

For an application which will allow members of my organisation to see data on their mobile device i need to store pre-formatted data on the device so they can see it offline as well. How they get the data is trough a JSON request-response.

The response is formatted as follows (anonymised ofc):

[{
    "firstname":"John",
    "lastname":"Smith",
    "group":"1",
    "age":11,
    "installed":"Ja",
    "medical":"Is aan zijn linker zijde verlamd geweest.",
    "notes":"Heimee. \r\nBeschermend opgevoed. \r\nTerug getrokken persoonlijkheid.",
    "Insignes":["test", "Test2"],
    "Parents":[]
},
{
    "firstname":"Emely",
    "lastname":"Watson",
    "group":"33",
    "age":14,
    "installed":"Ja",
    "medical":"",
    "notes":"Test",
    "Insignes":["Veilig & Gezond I","CWO II","CWO III","Kampeertechnieken & Pionieren"],
    "Parents":[
        {
            "name":"ouder ouder",
            "address":"op | 0000AA Amsterdam",
            "phone1":"",
            "phone2":"0612345678",
            "mail":"example@google.com"
        }]
}]

I have read a couple of discussion on how to best store this:

Is it ok to save a JSON array in SharedPreferences?

How to save JSON Array in SharedPreferences?

Android: what is the best way to store JSON data offline for the app in android?

Android - how to add JSON object to sharedPreferences?

What is the advantage of Using SQLite rather than File?

From reading these I have gathered that SharedPreference files are "faster" than sqlite but are prone to corruption. SQLite is a database and since the data comes from one I am inclined to use that at the cost of processing speed.

Now I only need to store and then read the data, it wont be mutated unless there is an update on the "main server" in which case I will probably wipe the local data and repopulate it. In these threads i have read that storing JSON in sharedpreference is easy but difficult to read.

But after reading these (and more) discussions I am no closer to knowing/deciding what the best way to store my json is.

Any advice is greatly appreciated!

Community
  • 1
  • 1
superstienos
  • 124
  • 11
  • 2
    Um, why not just a regular JSON file? What are you expecting to get from either `SharedPreferences` or SQLite that will be helping you in this case? – CommonsWare Apr 14 '17 at 12:58
  • Support CommonsWare! (If i did understand correctly). @S.Holzhauer In your case I would simply save whole file to filesystem. And load it from there. There would be less code with this approach. – Eugene Laminskiy Apr 14 '17 at 13:01
  • Not sure to be honest, after reading around I came to the conclusion these two options are the accepted ones. The JSON response comes from an HTTPS request, is it still possible to then store it as a file? And how secure is that? @CommonsWare – superstienos Apr 14 '17 at 13:04
  • 1
    @S.Holzhauer You can easily convert json to a string and save it. About security, not sqlite, nor preferences does provide same security for stored data as file saved to inner storage. Btw, sqlite db and preferences xml are saved there. User have no access to inner storage by default. But he can root his device and got access to it. So if you need to provide security, you should crypt saved data. And for your case, save and restore file is more easy way to save/restore json as string (in my personal opinion), because you are processing it some where else (if you are storing it as json). – Eugene Laminskiy Apr 14 '17 at 13:29
  • "is it still possible to then store it as a file?" -- um, well, yes. "And how secure is that?" -- if you store it on [internal storage](https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage.html), it will be fairly secure. – CommonsWare Apr 14 '17 at 13:30
  • @S.Holzhauer, Storing JSON data attribute values in separate columns of one or more SQLite database table gives you the advantage of easily querying the data and the ability to re-purpose the data through other model classes. JDXA ORM, which allows persistence of JSON objects in SQLite, may be helpful in this case. Disclaimer: I am the architect of JDXA. – Damodar Periwal Apr 17 '17 at 01:36

4 Answers4

1

You can try the ORMs like Realm or sugarORM and store the json has object in the database. They also provide the Cipher options by which you can encrypt the data too, Which would be more flexible.

http://www.androidauthority.com/use-android-keystore-store-passwords-sensitive-information-623779/

http://www.androidhive.info/2016/05/android-working-with-realm-database-replacing-sqlite-core-data/

If you don't need to use any ORM by third parties then you can directly encrypt the JSON string with the android keystore keys and then can store the encrypted string in the normal sqlite.

PranavKAndro
  • 921
  • 7
  • 9
1

This is opinion based answer. Android no I wouldn't store JSON in a DB or preferences. Store the json in a file. A file can be accessed as stream the same way you stream access json. Virtually no upper limit on size in a file. I might store a link to the json file in a DB or preferences. Depending on application I might just extract json into abstract elements and store in DB for ordering selection.

The data you have provided depending on sensitivity I would extract and insert into the contacts database for the device.

danny117
  • 5,581
  • 1
  • 26
  • 35
0

The best option is to go with a database approach, for one it sounds your dataset might be large are rather expected to be large. Database persist large sets of information, however I do not recommend using SQLITE , for it is sql based, no ORM and the operations can get tedious and time consuming and mainly it is slow(debatable). Rather use the hottest and latest no-sql database for android and ios, which Realm, realm is super fast, it is based on java annotations and ORM(like hibernate). To work with json in android I recommend familiarizing yourself with GSON, A Java serialization/deserialization library that can convert Java Objects into JSON and back.

Shared Preferenences are really convenient to store small key value pairs, If you have a relatively small collection of key-values that you'd like to save, you should use the SharedPreferences APIs. A SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them. You would of to still work with gson here, I never heard of a shared preference getting corrupted, what I do know is that they are fragile,and live with the existence of your app installation.

Conclusion: If your dataset is large use a database approach to persist large dataset, maintenance and modifications are more fluent here, however if you know that whatever you are storing is relatively small, use a shared pref, and to manipulate json constructs in android to and fro use the google gson library.

Remario
  • 3,813
  • 2
  • 18
  • 25
0

SharedPreferences :

If you have small amount of data in Json then you have to store it in SharedPreferences.

You can easily store Json to SharedPreferences using Gson. Just using this :

Convert Json to String :

Gson gson = new Gson();

String jsonString = gson.toJson(jsonData);

Convert String to Json :

gson.fromJson(jsonString, TypeToConvert);

Sqlite Database :

If you have large amount of data in Json then just go with Sqlite.

SANAT
  • 8,489
  • 55
  • 66