Skip to content

Instantly share code, notes, and snippets.

@crunchie84
Last active August 29, 2015 13:59
Show Gist options
  • Save crunchie84/10536724 to your computer and use it in GitHub Desktop.
Save crunchie84/10536724 to your computer and use it in GitHub Desktop.
Different ways to add custom JsonConverters to ElasticSearch Nest (.net client)
var settings = new ConnectionSettings("http://localhost:9200)
.AddContractJsonConverters(type =>
{
//TypeCheck to return StringEnumConverter for Enums and Nullable<T> where T : Enum
if (type.IsEnum || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) && type.GetGenericArguments().First().IsEnum))
return new Newtonsoft.Json.Converters.StringEnumConverter();
return null;
});
var settings = new ConnectionSettings("http://localhost:9200)
.SetJsonSerializerSettingsModifier(jsonSerializerSettings =>
{
if (jsonSerializerSettings.Converters == null)
jsonSerializerSettings.Converters = new List<JsonConverter>();
//by default will serialize all enum and nullable<T> where T : Enum to strings
jsonSerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
});
@crunchie84
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment