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

JsonSerializerOptions - Added JsonStringEnumConverter #380

Closed
wants to merge 1 commit into from

Conversation

snakefoot
Copy link
Contributor

@snakefoot snakefoot commented Apr 28, 2024

Makes easier for humans to query data in Elastic when Enum-Properties are saved as String-Values (instead of numeric values)

@snakefoot snakefoot closed this Apr 28, 2024
@sergiojrdotnet
Copy link
Contributor

sergiojrdotnet commented May 26, 2024

@snakefoot do you know of any alternative methods to resolve this?
I tried to set a custom event writer, but the JsonSerializerOptions isn't passed from EcsDocumentJsonConverter to EcsJsonConverterBase

@snakefoot
Copy link
Contributor Author

Not sure why JsonStringEnumConverter is not enabled by default in Ecs-Dotnet, as it would give a better user-experience. Maybe @Mpdreamz knows why it is important that Enum-values are represented using their integer value?

Made a work-around with #369, where it adds basic conversion of Enum-properties for NLog:

private static bool TryPopulateWhenSafe(IDictionary<string, object> propertyBag, string key, object value)
{
if (value is null or IConvertible || value.GetType().IsValueType)
{
if (value is Enum)
value = value.ToString();
Populate(propertyBag, key, value);
return true;
}
return false;

But this will not help on complex objects, with nested Enum-properties.

@Mpdreamz
Copy link
Member

Mpdreamz commented May 27, 2024

Its not important for storage in Elasticsearch.
I just wanted to rock with as plain of System.Text.Json configuration as possible.

E.g its not the default for System.Text.Json so it isn't for us either.

I think the main reason STJ doesn't is that it allows you to refactor enum names freely if you are rigorous enough with each enums integer value.

I tried to set a custom event writer, but the JsonSerializerOptions isn't passed from EcsDocumentJsonConverter to EcsJsonConverterBase

I think this is to prevent recursion into the same writer but let me double check.

@sergiojrdotnet
Copy link
Contributor

I think this is to prevent recursion into the same writer but let me double check.

As far as I can see, they are independent instances.
I've made the proposal to #388

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 this pull request may close these issues.

3 participants