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

Added dictionary key support #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 64 additions & 46 deletions src/Ulid/UlidJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,71 @@

namespace Cysharp.Serialization.Json
{
public class UlidJsonConverter : JsonConverter<Ulid>
public class UlidJsonConverter: JsonConverter<Ulid>
{
/// <summary>
/// Read a Ulid value represented by a string from JSON.
/// </summary>
public override Ulid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
try
{
if (reader.TokenType != JsonTokenType.String) throw new JsonException("Expected string");

if (reader.HasValueSequence)
{
// Parse using ValueSequence
var seq = reader.ValueSequence;
if (seq.Length != 26) throw new JsonException("Ulid invalid: length must be 26");
Span<byte> buf = stackalloc byte[26];
seq.CopyTo(buf);
Ulid.TryParse(buf, out var ulid);
return ulid;
}
else
{
// Parse usign ValueSpan
var buf = reader.ValueSpan;
if (buf.Length != 26) throw new JsonException("Ulid invalid: length must be 26");
Ulid.TryParse(buf, out var ulid);
return ulid;
}
}
catch (IndexOutOfRangeException e)
{
throw new JsonException("Ulid invalid: length must be 26", e);
}
catch (OverflowException e)
{
throw new JsonException("Ulid invalid: invalid character", e);
}
}

public override void Write(Utf8JsonWriter writer, Ulid value, JsonSerializerOptions options)
{
Span<byte> buf = stackalloc byte[26];
value.TryWriteStringify(buf);
writer.WriteStringValue(buf);
}
/// <summary>
/// Read a Ulid value represented by a string from JSON.
/// </summary>
public override Ulid Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
try
{
if (reader.TokenType != JsonTokenType.String && reader.TokenType != JsonTokenType.PropertyName) throw new JsonException("Expected string");

if (reader.HasValueSequence)
{
// Parse using ValueSequence
var seq = reader.ValueSequence;
if (seq.Length != 26) throw new JsonException("Ulid invalid: length must be 26");
Span<byte> buf = stackalloc byte[26];
seq.CopyTo(buf);
Ulid.TryParse(buf, out var ulid);
return ulid;
}
else
{
// Parse usign ValueSpan
var buf = reader.ValueSpan;
if (buf.Length != 26) throw new JsonException("Ulid invalid: length must be 26");
Ulid.TryParse(buf, out var ulid);
return ulid;
}
}
catch (IndexOutOfRangeException e)
{
throw new JsonException("Ulid invalid: length must be 26", e);
}
catch (OverflowException e)
{
throw new JsonException("Ulid invalid: invalid character", e);
}
}

public override void Write(Utf8JsonWriter writer, Ulid value, JsonSerializerOptions options)
{
Span<byte> buf = stackalloc byte[26];
value.TryWriteStringify(buf);
writer.WriteStringValue(buf);
}

public override void WriteAsPropertyName(
Utf8JsonWriter writer,
Ulid value,
JsonSerializerOptions options)
{
Span<byte> buf = stackalloc byte[26];
value.TryWriteStringify(buf);
writer.WritePropertyName(buf);
}

public override Ulid ReadAsPropertyName(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
return Read(ref reader, typeToConvert, options);
}
}
}

#endif
#endif