Export CBLModels to json file?

61 views
Skip to first unread message

Todd Freese

unread,
Sep 10, 2014, 5:27:27 PM9/10/14
to mobile-c...@googlegroups.com
I need to be able to dump some of my CBLModels to a json file. Is there any easy method to call that will output the formatted json?

Todd

Jeremy Kelley

unread,
Sep 10, 2014, 5:31:45 PM9/10/14
to mobile-c...@googlegroups.com
(CBLModel instance).document.properties is an NSDictionary.

NSDictionary *dict = (SOME CBLModel Instance).document.properties;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:0 error:nil];
NSString* jsonString = [[NSString alloc] initWithBytes:[jsonData
bytes] length:[jsonData length] encoding:NSUTF8StringEncoding];

That should work, haven't tried it though. I'm doing something
similar, but manually building a dict from my keys because I don't
want the whole shebang getting serialized for my purposes.

-jeremy
> --
> You received this message because you are subscribed to the Google Groups
> "Couchbase Mobile" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mobile-couchba...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/mobile-couchbase/ff7b7546-7197-4946-a578-aab846661891%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
The Christian ideal has not been tried and found wanting;
it has been found difficult and left untried – G. K. Chesterton

Jens Alfke

unread,
Sep 10, 2014, 5:32:29 PM9/10/14
to mobile-c...@googlegroups.com

On Sep 10, 2014, at 2:27 PM, Todd Freese <to...@filmworkers.com> wrote:

I need to be able to dump some of my CBLModels to a json file. Is there any easy method to call that will output the formatted json?

Call propertiesToSave and then use NSJSONSerialization (or CBLJSON) to convert that dictionary to JSON data.

—Jens

Jens Alfke

unread,
Sep 10, 2014, 5:33:27 PM9/10/14
to mobile-c...@googlegroups.com

On Sep 10, 2014, at 2:31 PM, Jeremy Kelley <jer...@33ad.org> wrote:

(CBLModel instance).document.properties   is an NSDictionary.

That will work, but it doesn't include any unsaved changes in the model.

—Jens

Todd Freese

unread,
Sep 10, 2014, 5:35:25 PM9/10/14
to mobile-c...@googlegroups.com
Awesome, thanks!

Todd
Reply all
Reply to author
Forward
0 new messages