【发布时间】:2015-09-02 21:10:12
【问题描述】:
我一直在跨平台 (Xamarin) 应用程序项目中使用 Couchbase Lite 作为本地数据存储。我一直在从 Amazon Web 服务后端检索对象,使用 Newtonsoft JSON 进行序列化。自从几周前我对this 问题的回答以来,我的对象中的所有内容都正确序列化。但是,将我的食谱的“组”属性添加到 couchbase lite 中的文档中并不成功。保存文档时不会引发异常,但是当从数据库中检索文档时,“IngredientsWithHeaders”和“InstructionsWithHeaders”都返回 null。以下是保存/检索代码:
public void SaveRecipe (Recipe recipe)
{
var document = db.GetDocument (recipe.RecipeId);
var properties = new Dictionary<string, object> (){
{ "UserId",recipe.UserId },
{ "Title",recipe.Title },
{ "IngredientsList",recipe.IngredientsList },
{ "Notes",recipe.Notes },
{ "Tags",recipe.Tags },
{ "Yield",recipe.Yield },
{ "IngredientsWithHeaders",recipe.IngredientsWithHeaders },
{ "InstructionsWithHeaders",recipe.InstructionsWithHeaders }
};
var rev = document.PutProperties(properties);
Debug.Assert (rev != null, "Revision is null");
var newRev = document.CurrentRevision.CreateRevision ();
newRev.SetAttachment ("image.jpg", "image/jpeg", recipe.Image);
newRev.Save ();
}
public IEnumerable<Recipe> GetRecipes ()
{
var query = db.CreateAllDocumentsQuery ();
query.AllDocsMode = AllDocsMode.AllDocs;
var rows = query.Run ();
Debug.WriteLine ("Recipes in Query: "+rows.Count);
return rows.Select (recipe => ToRecipe (recipe.Document));
}
private Recipe ToRecipe(Document doc){
Recipe recipe = new Recipe(doc.Id);
recipe.UserId = doc.GetProperty<string> ("UserId");
recipe.Title = doc.GetProperty<string> ("Title");
recipe.IngredientsList = doc.GetProperty<List<string>> ("IngredientsList");
recipe.Notes = doc.GetProperty<List<string>> ("Notes");
recipe.Tags = doc.GetProperty<ISet<string>> ("Tags");
recipe.Yield = doc.GetProperty<int> ("Yield");
recipe.IngredientsWithHeaders = doc.GetProperty<List<Group<string,IngredientJson>>> ("IngredientsWithHeaders");
recipe.InstructionsWithHeaders = doc.GetProperty<List<Group<string,string>>> ("InstructionsWithHeaders");
var rev = doc.CurrentRevision;
var image = rev.GetAttachment ("image.jpg");
if (image != null) {
Debug.WriteLine ("There is an image here");
recipe.Image = image.Content.ToArray();
}
return recipe;
}
【问题讨论】:
-
听起来和上一个问题中提到的序列化问题一模一样。你采取了哪种方法来解决它?我建议使用
Dictionary而不是Group -
我确实考虑过这一点,但它似乎正确地序列化到了 couchbase lite 数据库中。由于某种原因,它只是没有反序列化为所需的类型。
标签: c# json xamarin couchbase couchbase-lite