JSON储存在一个sqlite的领域呢?领域、JSON、sqlite

2023-09-07 04:49:07 作者:那时天空的云

我正在写与Web API,它响应JSON通信的应用程序。目前,我使用翻译JSON对象的Java对象 GSON (这是真棒,由的方式)。

I'm writing an application that communicates with a web API, which responds with JSON. Currently, I'm translating the JSON objects to Java objects using gson (which is awesome, by the way).

现在,我想存储一些这些对象的SQLite数据库。但是,他们有很多是绝不会在查询(使用性能,即我不会订单 ING,,其中 ING,或类似的东西与那些属性),所以我觉得这是不必要的创建列所有。我在想什么做的是:

Now, I want to store some of these objects in an SQLite database. However, they have lots of properties that would never be used in queries (i.e. I won't be ORDERing, WHEREing, or anything like that with those properties), so I feel it's unnecessary to create columns for all of them. What I'm thinking of doing is:

只有具有查询数据库时,将用于列,用于将基本数据 有一个文本 BLOB 列(其中一个你建议吗?),存储实际的JSON,所以我可以从它重建我的Java对象,并访问所有数据。 Only have columns for the essential data that will be used when querying the database Have one TEXT or BLOB column (which one do you recommend?) that stores the actual JSON, so I can recreate my Java object from it and access all the data.

这两个都会让我的生活更轻松,并简化我的code(我也不会写的非常的,当数据从API从数据库中处理与数据的不同code )。

This would both make my life easier and streamline my code (I would not have to write very different code when dealing with data from the API vs. data from the database).

不过,虽然我看不出有什么缺点,感觉有点腥。

However, although I see no downsides, it feels a bit fishy.

您觉得什么样的麻烦我会遇到,如果我使用这个技术?

What kind of trouble do you think I would run into if I use this technique?

推荐答案

主要的事情,我不会喜欢它是依靠存储/检索JSON的结构是有效的,因为它是完全脱离的手中数据库。这并不是说你不能把precautions对可能出现的问题,但如果JSON是莫名其妙地被截断或,也让解析器的方式存在其他安全风险,你再错过整个对象,而不是只是一个无效或截断财产。如果这是一个可以接受的风险,那么它可能是一个合理的方法。

The main thing I wouldn't like about it is relying on the structure of the stored/retrieved JSON to be valid, since it's completely out of the hands of the database. Not that you can't take precautions against possible issues, but if the JSON is somehow truncated or otherwise compromised in a way that trips up the parser, you're then missing the entire object instead of just one invalid or truncated property. If that's an acceptable risk, then it's probably a reasonable technique.