Skip to content
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
19 changes: 19 additions & 0 deletions src/EFCore.PG/Types/LTree.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System.Text.Json;
using System.Text.Json.Serialization;

// ReSharper disable once CheckNamespace

namespace Microsoft.EntityFrameworkCore;

/// <summary>
/// Represents a PostgreSQL ltree type. This type is implicitly convertible to a .NET <see cref="string" />.
/// Serialized as a JSON string by <see cref="JsonSerializer" />.
/// </summary>
/// <remarks>See https://www.postgresql.org/docs/current/ltree.html</remarks>
[JsonConverter(typeof(LTreeJsonConverter))]
public readonly struct LTree : IEquatable<LTree>
{
private readonly string _value;
Expand Down Expand Up @@ -173,4 +178,18 @@ public override int GetHashCode()
/// <inheritdocs />
public override string ToString()
=> _value;

/// <summary>
/// Converts <see cref="LTree" /> values to and from JSON strings.
/// </summary>
public sealed class LTreeJsonConverter : JsonConverter<LTree>
{
/// <inheritdoc />
public override LTree Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> new(reader.GetString()!);

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, LTree value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.ToString());
}
}
57 changes: 56 additions & 1 deletion test/EFCore.PG.Tests/Types/LTreeTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,63 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Npgsql.EntityFrameworkCore.PostgreSQL.Types;

public class LTreeTest
public partial class LTreeTest
{
[ConditionalFact]
public void ToString_works()
=> Assert.Equal("Top.Sub", ((LTree)"Top.Sub").ToString());

[ConditionalTheory]
[InlineData("Top.Sub", "\"Top.Sub\"")]
[InlineData("", "\"\"")]
[InlineData(null, "null")]
public void Json_roundtrips(string value, string json)
{
LTree ltree = value;

Assert.Equal(json, JsonSerializer.Serialize(ltree));
Assert.Equal(ltree, JsonSerializer.Deserialize<LTree>(json));
}

[ConditionalFact]
public void Json_roundtrips_property()
{
var node = new Node("Top.Sub");
var json = JsonSerializer.Serialize(node);

Assert.Equal("""{"Path":"Top.Sub"}""", json);
Assert.Equal(node, JsonSerializer.Deserialize<Node>(json));
}

[ConditionalFact]
public void Json_roundtrips_nullable()
{
LTree? ltree = "Top.Sub";

Assert.Equal("\"Top.Sub\"", JsonSerializer.Serialize(ltree));
Assert.Equal(ltree, JsonSerializer.Deserialize<LTree?>("\"Top.Sub\""));
Assert.Equal("null", JsonSerializer.Serialize((LTree?)null));
Assert.Null(JsonSerializer.Deserialize<LTree?>("null"));
}

[ConditionalFact]
public void Json_roundtrips_with_source_generation()
{
LTree ltree = "Top.Sub";
var typeInfo = LTreeJsonContext.Default.LTree;

Assert.Equal("\"Top.Sub\"", JsonSerializer.Serialize(ltree, typeInfo));
Assert.Equal(ltree, JsonSerializer.Deserialize("\"Top.Sub\"", typeInfo));
}

[ConditionalFact]
public void NLevel_throws_on_client()
=> Assert.Throws<InvalidOperationException>(() => ((LTree)"Top.Sub").NLevel);

private record Node(LTree Path);

[JsonSerializable(typeof(LTree))]
private partial class LTreeJsonContext : JsonSerializerContext;
}
Loading