Skip to content

Please add possibility to use Ulid as dictionary key #86

Open
@zorgoz

Description

@zorgoz

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());
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions