Couchbase Lite 2 + JsonConvert

编程入门 行业动态 更新时间:2024-10-26 14:36:37
本文介绍了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

本文发布于:2023-10-07 10:30:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Couchbase   Lite   JsonConvert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!