CouchbaseClient.StoreJson does not store the Id property

86 Views Asked by At

The 3rd assert fails. Is it a CouchbaseClient bug?

   private class StoreJsonTest
    {
        public int Id { get; set; }
        public string StrProp { get; set; }
        public int IntProp { get; set; }
    }

    [Test]
    public void CouchBase()
    {
        var storeJsonTest = new StoreJsonTest() {Id = 1, StrProp = "Test str prop", IntProp = 10};
        string key ="testStoreJson"+ Guid.NewGuid().ToString();
        CouchbaseClient couchbaseClient = new CouchbaseClient();
        couchbaseClient.StoreJson(StoreMode.Add, key, storeJsonTest);
        var extractedJson = couchbaseClient.GetJson<StoreJsonTest>(key);
        Assert.That(extractedJson.StrProp == storeJsonTest.StrProp);
        Assert.That(extractedJson.IntProp == storeJsonTest.IntProp);
        Assert.That(extractedJson.Id == storeJsonTest.Id);
    }

How do I store the Id property to couchbase?

1

There are 1 best solutions below

0
On BEST ANSWER

Add the following line somewhere in your application startup:

CouchbaseClientExtensions.JsonSerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

The default contract resolver is an internal implementation that serializes everything except ID properties.

'CamelCasePropertyNamesContractResolver' comes from the Newtonsoft.Json.Serialization namespace.