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

fix: better json support for KeyValueUrn #141

Merged
merged 2 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions src/Altinn.Urn/src/Altinn.Urn/KeyValueUrn.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Altinn.Urn;

Expand All @@ -12,6 +14,7 @@
/// To construct a <see cref="KeyValueUrn"/>, you need to know where the prefix ends and the value starts.
/// </remarks>
[DebuggerDisplay("{Urn}")]
[JsonConverter(typeof(JsonConverter))]
public readonly struct KeyValueUrn
: IKeyValueUrn
, IEquatable<KeyValueUrn>
Expand All @@ -22,7 +25,7 @@
/// </summary>
/// <param name="urn">The original <see cref="IKeyValueUrn"/>.</param>
/// <returns>A new <see cref="KeyValueUrn"/>.</returns>
public static KeyValueUrn Create(IKeyValueUrn urn)

Check warning on line 28 in src/Altinn.Urn/src/Altinn.Urn/KeyValueUrn.cs

View workflow job for this annotation

GitHub Actions / Analyze

All 'Create' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

Check warning on line 28 in src/Altinn.Urn/src/Altinn.Urn/KeyValueUrn.cs

View workflow job for this annotation

GitHub Actions / Analyze

All 'Create' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
=> new(urn.Urn, urn.KeySpan.Length + 1);

/// <summary>
Expand Down Expand Up @@ -104,14 +107,14 @@
public ReadOnlySpan<char> AsSpan() => _urn.AsSpan();

/// <inheritdoc/>
public bool Equals(KeyValueUrn other)

Check warning on line 110 in src/Altinn.Urn/src/Altinn.Urn/KeyValueUrn.cs

View workflow job for this annotation

GitHub Actions / Analyze

All 'Equals' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

Check warning on line 110 in src/Altinn.Urn/src/Altinn.Urn/KeyValueUrn.cs

View workflow job for this annotation

GitHub Actions / Analyze

All 'Equals' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
{
return _urn == other._urn
&& _valueIndex == other._valueIndex;
}

/// <inheritdoc/>
public string ToString(string? format, IFormatProvider? formatProvider)

Check warning on line 117 in src/Altinn.Urn/src/Altinn.Urn/KeyValueUrn.cs

View workflow job for this annotation

GitHub Actions / Analyze

All 'ToString' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

Check warning on line 117 in src/Altinn.Urn/src/Altinn.Urn/KeyValueUrn.cs

View workflow job for this annotation

GitHub Actions / Analyze

All 'ToString' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
{
return format.AsSpan() switch
{
Expand Down Expand Up @@ -160,4 +163,18 @@
/// <inheritdoc/>
public static bool operator !=(KeyValueUrn left, KeyValueUrn right)
=> !left.Equals(right);

private sealed class JsonConverter
: JsonConverter<KeyValueUrn>
{
public override KeyValueUrn Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException($"Deserialization of {nameof(KeyValueUrn)} is not supported.");
}

public override void Write(Utf8JsonWriter writer, KeyValueUrn value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.Urn);
}
}
}
19 changes: 19 additions & 0 deletions src/Altinn.Urn/test/Altinn.Urn.Tests/KeyValueUrnTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,23 @@ public void UrnJsonTypeValue_Equality()
(obj1 == obj2).Should().BeTrue();
(obj1 != obj2).Should().BeFalse();
}

[Fact]
public void KeyValueUrn_IsSerializeable()
{
var sut = KeyValueUrn.Create("urn:example:123", 12);
var json = JsonSerializer.Serialize(sut);

json.Should().Be(@"""urn:example:123""");
}

[Fact]
public void KeyValueUrn_Deserialize_ShouldThrow()
{
var json = @"""urn:example:123""";
Action act = () => JsonSerializer.Deserialize<KeyValueUrn>(json);

act.Should().Throw<NotSupportedException>()
.Which.Message.Should().StartWith($"Deserialization of {nameof(KeyValueUrn)} is not supported.");
}
}