Custom converter not used

I’m using the Cluster constructor overload that takes a string to load the configuration from the config file.

        // Cluster configuration is red from the config file
        cluster = new Cluster("couchbaseClients/couchbase");

Next I specify that I need to use the default serializer with a custom converter :

        JsonSerializerSettings defaultDeserializationSettings = new JsonSerializerSettings
        {
            TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto,
            NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
        };

        JsonSerializerSettings defaultSerializationSettings = new JsonSerializerSettings
        {
            Formatting = Newtonsoft.Json.Formatting.Indented,
            TypeNameHandling = TypeNameHandling.Auto,
            NullValueHandling = NullValueHandling.Ignore
        };

        JsonConverter myConverter = new CustomJsonConverter();

        defaultDeserializationSettings.Converters.Add(myConverter);
        defaultSerializationSettings.Converters.Add(myConverter);

        cluster.Configuration.Serializer = () => new DefaultSerializer(defaultDeserializationSettings, defaultSerializationSettings);

Finally I open the bucket.

       // Open the connection
        bucket = cluster.OpenBucket();

All of this works fine except that my custom converter is not used at all.

But, with exactly the same code and a harcoded configuration, it works :flushed:

        var config = new ClientConfiguration
        {
            Serializer = () => new DefaultSerializer(defaultDeserializationSettings, defaultSerializationSettings),
            Servers = new List<Uri>
      {
        new Uri("http://localhost:8091/pools"),
      },
            UseSsl = false,
            BucketConfigs = new Dictionary<string, BucketConfiguration>
      {
        {"default", new BucketConfiguration
        {
          BucketName = "default",
          UseSsl = false,
          Password = "",
          PoolConfiguration = new PoolConfiguration
          {
            MaxSize = 10,
            MinSize = 5
          }
        }}
      }

        };

        var cluster = new Cluster(config);
        bucket = cluster.OpenBucket();

In this scenario, the custom converter is used.
If I’m not messing with the code, I suspect that the factory to create the serializer is already invoked after the configuration is loaded from the config file. Replacing it seems to have has no effect.

Any help appreciated.

@omatrot -

Which version of the SDK are you using? The reason I ask is that in v2.1.0 and greater, the ClientConfiguration.Serialization and ClientConfiguration.Deserialization have been deprecated and a new, more extensible mechanism is used for modifying the default serialization/de-serialization settings:

  1. Implement the Couchbase.Core.Serialization.ITypeSerializer or override the Couchbase.Core.Serialization.DefaultSerializer with your custom implementation
  2. Instantiate the ITypeSerializer by assigning a Func
    to the ClientConfiguration.Serializer property which creates the custom ITypeSerializer.

I don’t have a working example, but will try to get one up in Coucbase .NET Contrib project: https://github.com/couchbaselabs/couchbase-net-contrib and write a blog post on how do it.

-Jeff

@omatrot -

I just looked deeper at your example and in the second example (which works) you are indeed using the correct mechanism for overriding the default serialization settings by passing the custom SerializationSettings into the constructor!

-Jeff

@jmorris Are you saying that I can’t read the configuration from a config file and override the serialization mechanism next ?

I found a solution to my problem.
I’m now using a Custom Serializer that inherit the DefaultSerializer and call the base constructor with parameters.
I can now use this serializer in the configuration like the following

<serializer name="CustomJsonSerializer" type="MyNameSpace.CustomJsonSerializer, MyAssembly" />
1 Like

@omatrot -

Good to hear you found a solution! On a similar vein, here is a blog post I did on creating a custom serializer: http://blog.couchbase.com/2015/june/using-jil-for-custom-json-serialization-in-the-couchbase-.net-sdk

-Jeff

I mean as an alternative you can override the default Serializer factory and provide the parameters in the ctor:

var config = new ClientConfiguration();
config.Serializer = ()=>
{
    var serializerSettings = new SerializationSettings(); //customize as needed
    var deserializerSettings = new SerializationSettings(); //customize as needed

    return new DefaultSerializer(serializerSettings , deserializerSettings);
}

var cluster = new Cluster(config);

That should give you the same effect.

-Jeff