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

Please add possibility to use Ulid as dictionary key #86

Open
zorgoz opened this issue Nov 21, 2024 · 0 comments
Open

Please add possibility to use Ulid as dictionary key #86

zorgoz opened this issue Nov 21, 2024 · 0 comments

Comments

@zorgoz
Copy link

zorgoz commented Nov 21, 2024

The current implementation of the JsonConverter does not allow the use of Ulid as a dictionary key, resulting in an exception: The type 'System.Ulid' is not a supported dictionary key using converter of type 'Cysharp.Serialization.Json.UlidJsonConverter'. The solution is to override WriteAsPropertyName and ReadAsPropertyName. Here is a no-nonsense extension of your converter class, but would be much better not having such an extension and just relying on yours.

public sealed class MyUlidJsonConverter : UlidJsonConverter
{
	public override void WriteAsPropertyName(
		Utf8JsonWriter writer,
		Ulid ulid,
		JsonSerializerOptions options) => writer.WritePropertyName(ulid.ToString());

	public override Ulid ReadAsPropertyName(
		ref Utf8JsonReader reader, 
		Type typeToConvert, 
		JsonSerializerOptions options) => Ulid.Parse(reader.GetString());
}
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

1 participant