How to get access to raw JSON document body?

260 views
Skip to first unread message

Tim Clarke

unread,
Oct 22, 2014, 7:43:00 AM10/22/14
to mobile-c...@googlegroups.com
Hi

We've got a setup working nicely with Couchbase documents going through Couchbase sync gateway into Couchbase Lite on iOS.  (v 1.0.2)

However, our Couchbase docs are all JSON and when we retrieve them from Couchbase Lite they are returned as a dictionary of properties with the JSON having been split into individual properties. This is clever, but not quite what we want. We need the raw JSON string.

Is there any way to access the raw JSON document in Couchbase Lite?  (And if not, could this be a new feature in a future release?)

thanks

Jens Alfke

unread,
Oct 22, 2014, 12:25:29 PM10/22/14
to mobile-c...@googlegroups.com

On Oct 22, 2014, at 4:43 AM, Tim Clarke <clark...@gmail.com> wrote:

However, our Couchbase docs are all JSON and when we retrieve them from Couchbase Lite they are returned as a dictionary of properties with the JSON having been split into individual properties. This is clever, but not quite what we want. We need the raw JSON string.

(I have a weird feeling that I already answered this exact question a week ago; are you reposting?)

If you want the document in JSON form, use NSJSONSerialization to encode the dictionary. (I'm not sure why you'd want that, though; are you just going to send it to some other API to parse it again?)

Is there any way to access the raw JSON document in Couchbase Lite?  (And if not, could this be a new feature in a future release?)

There's not really any such thing as the "raw" JSON. The JSON coming in from the server gets parsed and transformed during document insertion, and the version stored in the database file has had bits spliced out of it (all the metadata properties) that then get reconstituted when it's loaded. I have some future optimizations I've been prototyping in which the storage format won't even be even JSON at all anymore but a binary format that's more efficient to parse.

—Jens

Tim Clarke

unread,
Oct 23, 2014, 6:01:04 AM10/23/14
to mobile-c...@googlegroups.com
Thanks Jens, and massive apologies for the repost - I had a brain failure and didn't even see your original reply :(

We want the "raw" JSON because we already have a library of platform code that understands and deserialises our JSON. This platform code is currently on other OS's and is being ported to mobile. Couchbase Lite gives us everything we need to make our platform sync onto the mobile device as long as we can get at the raw JSON.

I would imagine that other users that already have code built that deserialises JSON into their own domain objects could benefit from access to the raw JSON.

But for now we have a workaround - we just javascript-encode the json when we insert into Couchbase so that it comes through into Lite as a property called "body". Not ideal, but it works for now.

Tim

Jens Alfke

unread,
Oct 23, 2014, 4:21:17 PM10/23/14
to mobile-c...@googlegroups.com

On Oct 23, 2014, at 3:01 AM, Tim Clarke <clark...@gmail.com> wrote:

We want the "raw" JSON because we already have a library of platform code that understands and deserialises our JSON. This platform code is currently on other OS's and is being ported to mobile. Couchbase Lite gives us everything we need to make our platform sync onto the mobile device as long as we can get at the raw JSON.

As I said, just use NSJSONSerialization to re-encode the document body. That'll be more efficient than trying to store quoted JSON strings in your documents, and it also lets you index properties of that JSON since it'll be easily available to the map functions.

—Jens
Reply all
Reply to author
Forward
0 new messages