Couchbase Lite 2 + JsonConvert [英] Couchbase Lite 2 + JsonConvert

查看:62
本文介绍了Couchbase Lite 2 + JsonConvert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码示例将一个简单的对象写到一个榻榻米Lite(版本2)数据库中,然后读取所有对象.您可以在此处

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

由于必须将每个对象的每个属性都转移到MutableObject,因此这是很多手动键入操作.

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; }
    }
}

根据我的研究,Couchbase lite在内部使用JsonConvert,因此可能有一种方法可以借助JsonConvert简化所有操作.

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

任何类似的东西

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

或者也许

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?

推荐答案

人们经常问这个问题,但是Couchbase Lite实际上并没有在数据库中存储JSON字符串.它们以不同的格式存储,因此不会带来您认为的好处(需要重新解析JSON,然后将其分解为其他格式).我一直在寻求一种直接序列化类的方法,而不是通过字典对象(这似乎是这里的最终目标),但是我们的工作重点是企业客户想要的东西,而这似乎不是其中之一.请注意,要使其投入使用,它需要在C#Java和Objective-C/Swift中实现.

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.

这篇关于Couchbase Lite 2 + JsonConvert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆