Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modifying NEST JsonSerializer settings does not seem to work #535

Closed
crunchie84 opened this issue Mar 20, 2014 · 6 comments
Closed

Modifying NEST JsonSerializer settings does not seem to work #535

crunchie84 opened this issue Mar 20, 2014 · 6 comments

Comments

@crunchie84
Copy link

Hello,

I am using NEST NuGet package v 0.12.0.0 to connect to my ElasticSearch. I want my Enum values to be send to ElasticSearch as their ToString() instead of their index/int value.

I have tried the following:

  var settings = new ConnectionSettings(elasticSearchUri)
    .SetDefaultIndex("my-index")
    .SetJsonSerializerSettingsModifier(m =>
    {
      if (m.Converters == null)
        m.Converters = new List<JsonConverter>(); //required because of nullrefs?
      m.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
    });

But it looks like it keeps sending the Enum values as ints.

I have also tried to modify the serialiserSettings after constructing the ConnectionSettings like so:

  connectionSettings.ModifyJsonSerializerSettings(new JsonSerializerSettings
  {
    Converters = new[] {new Newtonsoft.Json.Converters.StringEnumConverter()}
  });

but both seem to be ignored. Am i missing something here? Could you please provide some more details how to modify the JsonSerializerSettings?

@Mpdreamz
Copy link
Member

Try this:

settings.AddContractJsonConverters(t => typeof (Enum).IsAssignableFrom(t) ? new StringEnumConverter() : null);

@crunchie84
Copy link
Author

👍 that did the trick! THX!

@Mpdreamz
Copy link
Member

🍻

@crunchie84
Copy link
Author

Hey @Mpdreamz i'm trying to upgrade nest-1.0.0-beta1 to 1.0.2 but it seems my contractjsonconverters no longer work. Any clue? I made a minimal testcase which fails: https://gist.github.com/crunchie84/4df1bcdb3071a0b575b7

Mpdreamz added a commit that referenced this issue Sep 11, 2014
@Mpdreamz
Copy link
Member

This was a bug isolated in the TermFilter converter that did a WriteValue on the object instead of preferring to do serialization of the object value.

This should be fixed now!

@crunchie84
Copy link
Author

😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants