I was wondering if it is possible to access the raw JSON of the documents using the .NET implementation of the Couchbase Lite library? If not, are there plans to support this kind of document access in the future?
No, there are no plans to implement this kind of functionality. This has been asked before. See this answer
Can you describe why you want this? Then maybe we can show a way to get what you want.
I basically just wanted to be able to deserialize the document into an instance of a strongly typed object without having to do a bunch of manual work.
I have noticed that when a top level property contains a complex object graph, it comes across as a JObject or JToken. These we can just convert directly to the desired strongly typed object, but we have to write code for each property in the document to read it from the property collection and convert it to the desired type, and then put it back into the dictionary as a JObject.
This seems to work ok, it would just be easier to deserialize.
Here is an example (MobileDocument is just an abstraction around the CouchbaseLite Document class, and it provides base methods for reading properties from the CBL document and casting them to the desired type):
public class ComplexObjectGraphDocument : MobileDocument
{
public Foo FooProperty
{
get
{
JObject propValue = base.GetCast<JObject>("fooProperty", null);
return propValue != null ? propValue.ToObject<Foo>() : null;
}
set
{
base.Properties["fooProperty"] = value != null ? JObject.FromObject(value) : null;
}
}
}
public class Foo
{
public string StringProperty { get; set; }
public Bar BarProperty { get; set; }
}
public class Bar
{
public bool BoolProperty { get; set; }
}
ComplexObjectGraphDocument doc = new ComplexObjectGraphDocument();
doc.FooProperty = new Foo()
{
BarProperty = new Bar()
{
BoolProperty = false
},
StringProperty = "Hello"
};
doc.Save();
So it sounds like you want to use a JSON object modeling library to convert the JSON data into your custom objects?
Object modeling of this sort is one of the features we’ll be adding in Couchbase Lite 2.0.
Yes, that is what I’m after.
What is the release timeline for CBL 2.0?
We don’t have a public release schedule yet, but it should be safe to say “this year”. We’ll release developer previews well in advance, so you’ll be able to watch its progress.
I am working on iOS app. Swift 4 introduced Codable protocol which could be very useful in combination with Couchbase.
Consider following simple model:
struct CustomObject: Codable {
let objectID: String
let title: String
}
Trying to initialize this model based on query result I have to do something similar to this:
do {
for result in try myQuery.execute() {
guard let objectID = result.string(forKey: "objectID"), let title = result.string(forKey: "title") else { return }
let myCustomObject = CustomObject(objectID: objectID, title: title)
}
} catch {
print(error)
}
If I would be able to access result as JSON data directly, I would be able to do something like this:
do {
for result in try myQuery.execute() {
let myCusomObject = JSONDecoder().decode(CustomObject.self, from: result.toJSON)
}
} catch {
print(error)
}
Is it somehow possible to access document as raw JSON data right now? We are using couchbase-lite 2.0. I wasn’t able to find anything related to this in the documentation.
Or eventually do you have any plans to support something like this in future?
Yes, that would be useful for working w/ swift Codables but we don’t have that support right now. We had actually explored supporting custom encoder/decoder that will allow you to map from to/from your native types to CBL storage representation. But it will not be making it’s way into 2.0 .
I am not sure if we have any immediate plans of directly returning JSON data.
We will be evaluating the various options in future in light of broader cross-platform discussion around native object (de)serializers.
Has there been any further thoughts on this topic?
Specifically mapping between Swift objects and CB objects/documents?
Hi Priya, Is there any update on supporting JSON with Swift codables? I am exploring that option to build an iOS application. Any input in this regard would be helpful.
Unfortunately we do not have any out of box solution for that. It is on our radar though. If you do make progress on that front, community contributions are welcome.
FYI :
Here are some general examples of working with swift codables. Slightly dated but could help - it does not discuss transforming to Document
format