Couchbase Lite 2 + JsonConvert

本小妞迷上赌 提交于 2021-01-27 12:49:08

问题


The following code sample writes a simple object to a couchbase lite (version 2) database and reads all objects afterwards. This is what you can find in the official documentation here

This is quite a lot of manual typing since every property of every object must be transferred to the MutableObject.

class Program
{
    static void Main(string[] args)
    {
        Couchbase.Lite.Support.NetDesktop.Activate();

        const string DbName = "MyDb";
        var db = new Database(DbName);

        var item = new Item { Name = "test", Value = 5 };

        // Serialization HERE
        var doc = new MutableDocument();
        doc.SetString("Name", item.Name);
        doc.SetInt("Value", item.Value);
        db.Save(doc);

        using (var qry = QueryBuilder.Select(SelectResult.All())
                                     .From(DataSource.Database(db)))
        {
            foreach (var result in qry.Execute())
            {
                var resultItem = new Item
                {
                    // Deserialization HERE
                    Name = result[DbName].Dictionary.GetString("Name"),
                    Value = result[DbName].Dictionary.GetInt("Value")
                };

                Console.WriteLine(resultItem.Name);
            }
        }

        Console.ReadKey();
    }

    class Item
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
}

From my research Couchbase lite uses JsonConvert internally, so there might be a way to simplify all that with the help of JsonConvert.

Anything like:

var json = JsonConvert.SerializeObject(item);
var doc = new MutableDocument(json); // No overload to provide raw JSON

or maybe

var data = JsonConvert.SerializeToDict(item); // JsonConvert does not provide this
var doc = new MutableDocument(data);

Is there or is this some kind of optimization and the preferred approach is by intend?


回答1:


People ask about this quite often, but Couchbase Lite does not actually store JSON strings in the database. They are stored in a different format so this would not give the benefit that you think (the JSON would need to be reparsed and then broken down into the other format). I'd been pushing for a way to serialize classes directly instead of going through dictionary objects (which seems like the ultimate goal here) but our priority is on things that enterprise clients want and this doesn't seem to be one of them. Note that for it to make it in, it needs to be implemented in C# Java and Objective-C / Swift.




回答2:


I don't know about JsonConvert but there seems to be a constructor that takes IDictionary<string, object> as argument. So I would try something like this (brain-compiled):

MutableDocument CreateDocument(object data)
{
   if (data == null) return null;
   var propertyValues = new Dictionary<string, object>();
   foreach (var property in data.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
   {
       propertyValues[property.Name] = property.GetValue(data);
   }
   return new MutableDocument(propertyValues);
 }

See if this works.



来源:https://stackoverflow.com/questions/49949447/couchbase-lite-2-jsonconvert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!