# How exactly to use a custom json serializer with NEST on .net

**URL:** <https://discuss.elastic.co/t/how-exactly-to-use-a-custom-json-serializer-with-nest-on-net/121729>\
**Category:** Elasticsearch\
**Created:** [February 27, 2018, 7:58pm UTC](https://discuss.elastic.co/t/how-exactly-to-use-a-custom-json-serializer-with-nest-on-net/121729 "2018-02-27T19:58:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![togakangaroo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/togakangaroo/32/28229_2.png) [@togakangaroo](https://discuss.elastic.co/u/togakangaroo)\
**Post date:** [February 27, 2018, 7:58pm UTC](https://discuss.elastic.co/t/how-exactly-to-use-a-custom-json-serializer-with-nest-on-net/121729/1 "2018-02-27T19:58:31Z")

</div>

Hi all, I was directed here by the Elasticsearch NEST client for .Net (really well written by the way).

I'm hitting a bit of a wall and the docs are too sparse on how exactly to do this.

Basically, my objects have private setters and I want deserialization to work anyways.

I'm aware that I can use [NEST.JsonNetSerializer](https://www.nuget.org/packages/NEST.JsonNetSerializer/) and that with this I seem to be able to pass in a `sourceSerializer` factory parameter into my `ConnectionSettings` which in turn can have the `jsonSerializerSettingsFactory` but from there things don't seem to work.

```
static ConnectionSettings settings = new ConnectionSettings (
    new SingleNodeConnectionPool(new Uri("http://localhost:9200")), 
    sourceSerializer: (builtin, settings) => {
	var serializer =new JsonNetSerializer(builtin, settings, jsonSerializerSettingsFactory: () => {
	   return new Newtonsoft.Json.JsonSerializerSettings {
		   ContractResolver = new Ducky(),	   	
	   };
	});
	return serializer;
})
static ElasticClient client = new ElasticClient(settings);

public class Ducky : DefaultContractResolver {
	protected override List<MemberInfo> GetSerializableMembers(Type objectType) {
		return objectType.GetMembers(BindingFlags.NonPublic | BindingFlags.Public).ToList();
	}
}

```

When I do this and then make a query, a breakpoint inside of `GetSerializableMembers` never seems to get hit and my properties with private setters are not populated. I'm not certain what else to do.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [February 28, 2018, 6:05am UTC](https://discuss.elastic.co/t/how-exactly-to-use-a-custom-json-serializer-with-nest-on-net/121729/2 "2018-02-28T06:05:43Z")

</div>

The breakpoint does not get hit because the `ContractResolver` property on `JsonSerializerSettings` is always assigned to in a private method:

> <https://github.com/elastic/elasticsearch-net/blob/fb279faa6d4e849c055c10f5ae353072220b96e6/src/Serializers/Nest.JsonNetSerializer/ConnectionSettingsAwareSerializerBase.Serializer.cs#L50-L59>

There's no easy way to change the `IContractResolver` right now, not at least without duplicating a lot of the code within [`ConnectionSettingsAwareSerializerBase`](https://github.com/elastic/elasticsearch-net/blob/6.x/src/Serializers/Nest.JsonNetSerializer/ConnectionSettingsAwareSerializerBase.Serializer.cs). This is because `IContractResolver` can be modified but not assigned to, and the reasoning behind this was to hide the complexity behind the resolver needing knowledge of the connection settings.

One way in which private members can be serialized easily is by attributing with `JsonPropertyAttribute`

```auto
private static void Main()
{
	var defaultIndex = "products";
    
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(pool, JsonNetSerializer.Default)
        .DefaultIndex(defaultIndex);
    var client = new ElasticClient(settings);

    client.IndexDocument(new Product("foo")
    {   
       Id = 1       
    });
}

public class Product
{
    [JsonProperty]
    private string Name;

    public Product(string name)
    {
        Name = name;
    }
    
    public int Id { get; set; }
}

```

which will serialize to

```json
{
  "name": "foo",
  "id": 1
}

```

---

<div class="post-metadata">

**Author:** ![togakangaroo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/togakangaroo/32/28229_2.png) [@togakangaroo](https://discuss.elastic.co/u/togakangaroo)\
**Post date:** [February 28, 2018, 2:39pm UTC](https://discuss.elastic.co/t/how-exactly-to-use-a-custom-json-serializer-with-nest-on-net/121729/3 "2018-02-28T14:39:38Z")

</div>

I'm not sure I understand...what is the point of being able to provide your own json serializer if you can't make changes to the contract resolver? That's really the only part of the serializer settings I've ever been interested in. What other sort of stuff do people use that nuget package for?

Going in and annotating tons of domain-model properties with attributes that are all about json which isn't even a domain concept just goes again all my DDD instincts.

Is this something that might be a good candidate for a PR? The ability to use private _setters_ shouldn't be considered out there, its pretty standard practice when you want to enforce validity on your domain objects. I wouldn't imagine implementing this would require all that much work, it should be possible to wrap the `ConnectionSettingsAwareSerializer` in a decorator or something...

---

<div class="post-metadata">

**Author:** ![togakangaroo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/togakangaroo/32/28229_2.png) [@togakangaroo](https://discuss.elastic.co/u/togakangaroo)\
**Post date:** [February 28, 2018, 4:15pm UTC](https://discuss.elastic.co/t/how-exactly-to-use-a-custom-json-serializer-with-nest-on-net/121729/4 "2018-02-28T16:15:19Z")

</div>

Also, I just tried adding `[JsonProperty]` to public properties with a private setter and it didn't deserialize...

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 28, 2018, 4:15pm UTC](https://discuss.elastic.co/t/how-exactly-to-use-a-custom-json-serializer-with-nest-on-net/121729/5 "2018-03-28T16:15:37Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
