使用 HashTable 属性 序列化一个对象并存储在 CouchBase 中
Serialize an object with HashTable property and store in CouchBase
我们的团队已选择 Couchbase 作为我们应用程序的缓存。我们在此缓存中存储的对象如下所示
public class CatalogEntity
{
public int Id { get; set; }
public string Name { get; set; }
// this property gives us trouble
public Hashtable Attributes { get; set;}
}
在我们的代码中,从 CouchBase 缓存中检索对象后,我发现主要类型(Id
和 Name
)的属性已正确反序列化,但是 Attributes
Hashtable
类型未反序列化并保持为 JSON。例如,如果我有类似
var entity = new CatalogEntity();
entity.Attributes["foo"] = new Foo();
来自缓存的对象将具有 Attributes["foo"]
属性 作为 Foo
class 的 JSON 表示。
我想知道如何正确输入 Hashtable
serialize/deserialized?我应该将对象序列化为二进制文件,而不是将二进制流存储在 CouchBase 中吗?
是否有需要使用 Hashtable
的原因?例如,您可以使用 Dictionary<string,string>
代替吗?
我认为您看到的原因是 Json.NET 序列化哈希表的最佳尝试(另请参阅 Whosebug 上的这个问题:)。
我们的团队已选择 Couchbase 作为我们应用程序的缓存。我们在此缓存中存储的对象如下所示
public class CatalogEntity
{
public int Id { get; set; }
public string Name { get; set; }
// this property gives us trouble
public Hashtable Attributes { get; set;}
}
在我们的代码中,从 CouchBase 缓存中检索对象后,我发现主要类型(Id
和 Name
)的属性已正确反序列化,但是 Attributes
Hashtable
类型未反序列化并保持为 JSON。例如,如果我有类似
var entity = new CatalogEntity();
entity.Attributes["foo"] = new Foo();
来自缓存的对象将具有 Attributes["foo"]
属性 作为 Foo
class 的 JSON 表示。
我想知道如何正确输入 Hashtable
serialize/deserialized?我应该将对象序列化为二进制文件,而不是将二进制流存储在 CouchBase 中吗?
是否有需要使用 Hashtable
的原因?例如,您可以使用 Dictionary<string,string>
代替吗?
我认为您看到的原因是 Json.NET 序列化哈希表的最佳尝试(另请参阅 Whosebug 上的这个问题: