Accessing CouchbaseLite document JSON

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();