1

I had a nosql server for the application backend and json is coming from that server through Retrofit.

now i want to store the Json for the offline use, can i use that json to store in couchdb database.

Json structure can be

player -|
        | id
        | name -|
                | firstname
                | lastname

and then i want to search through firstname

Can i use couchbase-lite for that in android

or is there any other good alternative solution for that :- note :- it is a complex json so I can not covert it into POJO again otherwise I will use the Realm for this.

Community
  • 1
  • 1
sumit matta
  • 744
  • 7
  • 8

2 Answers2

1

Here's an example of how to do this. If your JSON is in JSONString

ObjectMapper mapper = new ObjectMapper();  // Usually you only need one instance of this per app.
try {
    Map<String, Object> map = mapper.readValue(JSONString, Map.class);
    Document document = db.createDocument();
    document.putProperties(map);
} catch (IOException | CouchbaseLiteException ex) {
    ex.printStackTrace();
}

ObjectMapper comes from the Jackson JSON library. Currently Couchbase Lite uses Jackson, so you're not adding any extra dependencies.

For queries, you use Views to flexibly create indexes via map/reduce functions. You then build queries against the indexes you create.

Hod
  • 2,236
  • 1
  • 14
  • 22
0

You are only allowed to store "standard java objects" in document properties in couchbase lite. those are,

int/Integer
String
float/Float
Collections of the above (maps, lists, sets).

So even if you want to store JsonObject or JsonArray, then first of all you need to convert them in to string using toString() method and the store it in couchbase.

In that case it will be very inefficient for you to executing search queries etc.

So I think one way is by converting is to convert it into poJo and then use couchbase but as you have written

it is a complex json so I can not covert it into POJO again otherwise I will use the Realm for this.

I suggest to not to use couchbase.

Nikhil
  • 3,711
  • 8
  • 32
  • 43
  • This is not correct. Couchbase Lite stores JSON, not a subset. With Views and the map/reduce features querying can be easy and efficient. – Hod Sep 14 '16 at 15:55
  • Yes, it links through to a simple way to do this. Similar to shown in my answer. OP talks about storing JSON received as a string. It's very easy. Not sure what your criticism is. – Hod Sep 14 '16 at 16:26