2

Our team has chosen Couchbase as the cache for our application. What we store in this cache are objects look like this

public class CatalogEntity
{
    public int Id { get; set; }
    public string Name { get; set; }

    // this property gives us trouble
    public Hashtable Attributes { get; set;}
}

In our code, after retrieve an object from the CouchBase cache, I found that properties of primary types(Id and Name) are properly deserialized, but the Attributes of Hashtable type is not deserialized and stay as JSON. For example, if I have something like

var entity = new CatalogEntity();
entity.Attributes["foo"] = new Foo();

The object from cache will have Attributes["foo"] property as JSON representation of the Foo class.

I am wondering how to have the Hashtable type properly serialize/deserialized? Should I serialize the object to binary and instead store binary stream in the CouchBase?

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
sean717
  • 11,759
  • 20
  • 66
  • 90

1 Answers1

0

Is there a reason you need to use Hashtable? Can you use, for instance, a Dictionary<string,string> instead?

I think the reason for what you're seeing is Json.NET's best attempt to serialize a hashtable (see also this question on StackOverflow: Serialize hashtable using Json.Net).

Community
  • 1
  • 1
Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
  • 1
    Thanks for the answer. Unfortunately I am dealing with a legacy code base and switching to Dictionary is not so easy. But the linked question explained why the HashTable values are not de-serialized. I end up manually de-serialize the values. – sean717 Jul 07 '16 at 21:32