How to pass JsonSerializerSettings() to Serializer in SDK 3.0.6?

Was using this for SDK 2.x

But, how and where do we apply those settings in 3.0.6?

Thanks,

Brian Davis

I managed to get the settings working by using this:

Only issue now is getting C# Object Properties with Proper Naming to be saved in all lower case when insert/upsert into Couchbase.

In 2.7 some how it was working with no settings I am aware of, but now seems to be using the literal Property Name in Couchbase.

Hi Brian. It looks like you figured it out.

As for getting your properties to serialize with lower case, that’s a common question around using Newtonsoft JSON.

It seems like there is no default LowerCaseContractResolver, but lots of adhoc implementations on StackOverflow.

The nuclear option is to use [JsonProperty("name")] attributes on your properties, explicitly.

1 Like

Before upgrading to 3.0.6, I thought some how that was a default action from Couchbase, but I guess some where some in our application it is how it was making everything lower case?

Because with 2.7 I did not do anything to make it lower case it just showed up that way on CouchBase server. So thought that was the “default” behavior on Couchbase side some how?

Was hoping for a solution that Couchbase does already :slight_smile:

Thanks for your help and the link.

@Brian_Davis -

In addition to what @Richard_Ponton states above, here is an example of how to change the serializer settings :

var options = new ClusterOptions
{
    Serializer =  new DefaultSerializer(new JsonSerializerSettings
        {
            ContractResolver = new DefaultContractResolver()
        }, new JsonSerializerSettings
        {
            ContractResolver = new DefaultContractResolver()
        });
}
var cluster = await Cluster.ConnectAsync(options); 

You would swap the DefaultContractResolver with your custom lower-case implementation. Note that the SDK uses the CamelCasePropertyNamesContractResolver as the default contract resolver which is a common format (ex: Google suggests it). However, you can use whatever format you would like :slight_smile:

-Jeff

Both SDKs use the same DefaultContractSerializer AFAIK out of the box.

-Jeff

Everyone thanks for your help :slight_smile:

Got it working!

public async Task Setup()
        {

            var settings = new DefaultSerializer();

            settings.SerializerSettings.Formatting = Formatting.Indented;
            settings.SerializerSettings.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii;

            settings.SerializerSettings.ContractResolver = new LowercaseContractResolver();

            var options = new ClusterOptions
            {
                UserName = "testing",
                Password = "1233456",
                Serializer = settings

            };

            Cluster = await Couchbase.Cluster.ConnectAsync("couchbase://localhost",options);

            Bucket = await Cluster.BucketAsync("test");
            Collection = Bucket.DefaultCollection();
        }

public class LowercaseContractResolver : DefaultContractResolver
    {
        protected override string ResolvePropertyName(string propertyName)
        {
            return propertyName.ToLower();
        }
    }
1 Like