Skip to content

[FEATURE] Support for custom JsonSerializerOptions #7238

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

Closed
ilya-scale opened this issue Feb 17, 2023 · 10 comments · Fixed by #7272
Closed

[FEATURE] Support for custom JsonSerializerOptions #7238

ilya-scale opened this issue Feb 17, 2023 · 10 comments · Fixed by #7272
Milestone

Comments

@ilya-scale
Copy link

Is your feature request related to a problem? Please describe.
I am trying to switch to the new library 8.x, but it does not seem it allows for custom format for Json serialization.

E.g. I need to add a date format, enum conversion, camel case, etc.

Before it was possible to do this:

var connectionSettings = new ConnectionSettings(pool, (serializer, values) =>
{
            return new JsonNetSerializer(serializer, values, () => MySettings); 
}

I did not find anything on how to do it in 8.x.

Describe the solution you'd like
I want a possibility to provide my own options. If required it should be merged with DefaultRequestResponseSerializer if it has some options that have to be present.

Describe alternatives you've considered
It seems it is possible to get to the Options of the default serialiezr like that:

var jsonSerializerOptions = ((SystemTextJsonSerializer) client.RequestResponseSerializer).Options;

I am not sure if it is intended, but it is not possible to replace the options completely, but possible to augment them. It seems a bit strange way of accessing the options and looks more like a side effect than a feature.

@stevejgordon
Copy link
Contributor

Hi, @ilya-scale. We don't currently have a good story for augmenting the existing options. That hook you found won't work for this scenario and shouldn't be exposed publicly. Right now, you'd have to implement a custom Serializer and new it up in the ElasticsearchClientSettings by providing a SourceSerializerFactory. However, you'd not get the built-in converters, so you may hit issues if you use any of our types on your POCO class for the source data.

I have a roadmap item to refine the experience to make it easier to configure JsonSerializerOptions in some form.

@ilya-scale
Copy link
Author

ilya-scale commented Feb 20, 2023

Thanks for the answer @stevejgordon , what do I risk if I use the workaround that you have described (since I would rather use it now than wait)? It is only one fairly simple class that I write to the database. There are no references to the library in there.

Do you also have some estimate as to when you will be able to implement this?

@stevejgordon
Copy link
Contributor

Hi, @ilya-scale. For simple POCOs, the custom Serializer will be fine. We'll likely expose most customisation that way but just look to simplify the experience. Deriving a custom serializer is a supported scenario, it's just hard to implement fully at the moment.

I can't provide a specific timeframe for customisation improvements. I can say that I hope to provide something sooner, rather than later as it's a common requirement we want to support more easily.

@ilya-scale
Copy link
Author

Thanks, I have written this class, that should support all of the default converters as a workaround:

public class DefaultElasticsearchSerializer : SystemTextJsonSerializer
{
    public DefaultElasticsearchSerializer(Serializer builtInSerializer)
    {
        var builtInSerializerOptions = ((SystemTextJsonSerializer)builtInSerializer).Options;
        
        Options = MyClass.GetDefaultOptions();

        foreach (var converter in builtInSerializerOptions.Converters)
        {
            Options.Converters.Add(converter);
        }
    }
}

@stevejgordon
Copy link
Contributor

@ilya-scale - Be wary of grabbing the converters from the buildInSerializer for now. I noticed a bug, and we're exposing the request/response serializer instead of the source serializer when we invoke the factory. For now, it will add converters that could potentially conflict with your own types. I'll fix that bug in the next patch.

@ilya-scale
Copy link
Author

ilya-scale commented Feb 20, 2023

Thanks, do you suggest I should for now just use only my converters instead?

@stevejgordon
Copy link
Contributor

@ilya-scale That might be best for now to avoid any breaks or unexpected behavioural changes until we provide a formal, supported way to configure this.

@ilya-scale
Copy link
Author

Thanks for help!

I will do this. It seems to work just fine for my case now, so hopefully you guys will release a fix before I need a more complicated document to be stored :)

@stevejgordon stevejgordon changed the title Support for custom JsonSerializerOptions in 8.x [FEATURE] Support for custom JsonSerializerOptions in 8.x Feb 21, 2023
@stevejgordon stevejgordon changed the title [FEATURE] Support for custom JsonSerializerOptions in 8.x [FEATURE] Support for custom JsonSerializerOptions Feb 27, 2023
@stevejgordon stevejgordon added this to the 8.0.6 milestone Mar 7, 2023
@KoalaBear84
Copy link

KoalaBear84 commented Mar 7, 2023

Great news! Thanks @stevejgordon

Hopefully can be tested soon 👍

Update:
Already released 👍, will hopefully test soon. From I have a roadmap item to Implemented in 2 weeks 😇✅

@stevejgordon
Copy link
Contributor

You're welcome @KoalaBear84. Hopefully, this has all the capabilities you need and the new docs cover common scenarios.

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

Successfully merging a pull request may close this issue.

3 participants