Hi, I’m designing some class we’re going to reffer as Base, Base is defined this way:
[Serializable]
public class Base
{
[JsonProperty("id")]
protected string id;
[JsonProperty("type")]
protected string type;
//[DataMember(IsRequired = false, EmitDefaultValue = false)]
[JsonProperty(PropertyName = "deleted",Required = Required.AllowNull, DefaultValueHandling = DefaultValueHandling.Ignore)]
protected bool deleted;
public Base(string t)
{
type = t;
}
}
that’s according with the explanation on .Net resources section for Couchbase.
Then I’m trying to run this code:
Base b = new Base(“noelamadre”);
CouchbaseClientConfiguration Conf = new CouchbaseClientConfiguration();
Conf.Bucket = "MyExampleBucket";
Conf.BucketPassword = "MyExampleSaslPassword";
Conf.Urls.Add(new Uri("http://127.0.0.1:8091/pools/", UriKind.Absolute));
CouchbaseClient client = new CouchbaseClient(Conf);
client.Store(Enyim.Caching.Memcached.StoreMode.Add,"hi", b);
And all that I can get stored is an stream of bytes in the way this pic explains.
Any though you guys?
Hi -
You need to need to use ExecuteStoreJson(…) to store a POCO as JSON.
Thanks,
Jeff
Hi Jeff, thanks for your response, really.
Sadly I changed the Store method for
IStoreOperationResult _result = client.ExecuteStore(Enyim.Caching.Memcached.StoreMode.Add,"hi", u);
but the resulting register stored looks like the same on the first post. I wonder if I’m doing something wrong designing “Base” class.
My usings are
using System;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
Eh, make your instance fields automatic properties with get;set; and then remove the [Serializable] attribute; you shouldn’t need it:
public class Base
{
[JsonProperty(“id”)]
public string Id{get;set;}
[JsonProperty(“type”)]
public string Type {get;set;}
public bool Deleted{get;set;}
public Base(string t) { type = t; }
}
The serializer requires public fields or properties.
-Jeff
Ok, Now my Base.cs looks like this
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace MyNamespace
{
public class Base
{
[JsonProperty(“id”)]
public string Id { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
public bool Deleted { get; set; }
public Base(string t) { Type = t; } }
}
and I invoke the storing method as:
Base _poco = new Base("noelamadre");
CouchbaseClientConfiguration Conf = new CouchbaseClientConfiguration();
Conf.Bucket = "MyBucket";
Conf.BucketPassword = "MySaslPassword";
Conf.Urls.Add(new Uri("http://127.0.0.1:8091/pools/", UriKind.Absolute));
CouchbaseClient client = new CouchbaseClient(Conf);
IStoreOperationResult _result = client.ExecuteStore(Enyim.Caching.Memcached.StoreMode.Add,"hi", _poco);
And I feel kinda obnoxious but can you be so kind to take a look at this result please?.
Finally I got two scenarios:
- Adding Serializable Attribute (throws binary stored data)
- Getting an Exception of serializacion when Serializable Attribute is not present
Hi -
You need an empty public constructor on Base. Sorry, I overlooked that. The Serializable attribute shouldn’t hurt, nor should it be needed AFAIK.
Here is some examples: https://github.com/couchbase/couchbase-net-client/blob/release13/src/Couchbase.Tests/CouchbaseJsonExtensionTests.cs#L44
Thanks,
Jeff
Hi Jeff, Finally, but as I concern about the added value of solving every issue let me tell you what I was doing wrong:
1st: I wasn’t adding the using
using Couchbase.Extensions;
2nd: The need of the public main constructor
public Base(){}
3rd: I was calling
client.ExecuteStore(Enyim.Caching.Memcached.StoreMode.Add,“hi”, _poco);
instead of
client.ExecuteStoreJson(Enyim.Caching.Memcached.StoreMode.Add,“hi”, _poco);
In the first answer when you told me to use that method I wasn’t implementing Couchbase.Extensions using, that’s why Visual Studio didn’t recognize the function and instead of working with ExecuteStoreJson I did assume you were referring to ExecuteStore.
Feel so sorry for my common mistake, anyway I’m very grateful for you time Jeff.
I hope all of this served you as a good experience for the future people using couchbase with .Net.
Really pleased with your time and dedication,
Ram.
Oh!!! One more.
4th: Dont use [JsonProperty(“id”)] public string Id { get; set; }
, somehow public string doesn’t store the JsonProperty “id”, you have to use a different name.
The outcoming of using [JsonProperty(“id”)] public string Id { get; set; }
will be a json like this {“someotherproperty”:“value”}
.
Instead try something like [JsonProperty(“id”)] public string _idBase { get; set; }
, you’ll get this same Json {“id”:“YourIdValueIn_idBase”, “someotherproperty”:“value”}
.
Greetings!