TelegramBots / Telegram.Bot

.NET Client for Telegram Bot API
https://telegrambots.github.io/book
MIT License
3.22k stars 692 forks source link

Ability to use JsonStringEnumConverter included in System.Text.Json #1428

Closed saneks222 closed 1 week ago

saneks222 commented 1 week ago

I think the use of this: https://github.com/TelegramBots/Telegram.Bot/blob/master/src/Telegram.Bot/Serialization/EnumConverter.cs seems redundant to me.

Wouldn't this be a better solution ?

Public class JsonStringEnumSnakeCaseLowerConverter(): JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower);

usage

[JsonConverter(typeof(JsonStringEnumSnakeCaseLowerConverter))]

wiz0u commented 1 week ago

In fact I did considered it, and wrote exactly that line.

But then I had to switch back to this EnumConverter code due to different behaviour in case of invalid/unrecognized value (ours returns 0, theirs throw an exception)

wiz0u commented 1 week ago

Note that our EnumConverter code is largely inspired by JsonStringEnumConverter implementation, in order to be short and efficient, and has been tested to be compatible with Native AOT/Trimming.