Store JSON in sqlite field?

I am writing an application that communicates with a web API that responds with JSON. I am currently translating JSON objects into Java objects using gson (which, by the way, is awesome).

Now I want to save some of these objects in a SQLite database. However, they have many properties that will never be used in queries (i.e. I will not be ORDER ing, WHERE ing or anything like that with these properties), so I find it unnecessary to create columns for all of them. I think that:

  • There are only columns for the master data that will be used when querying the database
  • Is there a single TEXT or BLOB column (which do you recommend?) That stores the actual JSON, so I can recreate my Java object from it and access all the data.

This would make my life easier and simplify my code (I would not have to write completely different code when working with data from the API compared to data from the database).

However, although I see no flaws, he feels a little suspicious.

What problem would you encounter if I use this technique?

+6
source share
1 answer

The main thing that I would not like is to rely on the structure of the stored / received JSON in order to be valid, since it completely got out of the hands of the database. Not that you cannot take precautions regarding possible problems, but if JSON is somehow truncated or otherwise compromised in such a way that it causes a parser, then you will be missing the whole object, and not just one invalid or truncated property. If this is an acceptable risk, then this is probably a reasonable technique.

+3
source

Source: https://habr.com/ru/post/891669/


All Articles