Skip to content

Commit

Permalink
Refactor and fix serializers for FunctionReference and DynamicReference.
Browse files Browse the repository at this point in the history
The serializers for FunctionReference and DynamicReference have been refactored and fixed. This includes updating conditions for properties and handling potential null values. Removed redundant converter in TestUtil following restructuring of serialization tests.
  • Loading branch information
Ygg01 committed Jan 21, 2024
1 parent d9f2757 commit f58f3bf
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 271 deletions.
56 changes: 0 additions & 56 deletions Linguini.Serialization.Test/AttributeSerializerTest.cs

This file was deleted.

71 changes: 0 additions & 71 deletions Linguini.Serialization.Test/CallArgumentsSerializerTest.cs

This file was deleted.

84 changes: 0 additions & 84 deletions Linguini.Serialization.Test/DynamicReferenceSerializerTest.cs

This file was deleted.

86 changes: 86 additions & 0 deletions Linguini.Serialization.Test/SerializeAndDeserializeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System.Diagnostics;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using Linguini.Serialization.Converters;
using Linguini.Syntax.Ast;
using NUnit.Framework;
using Attribute = Linguini.Syntax.Ast.Attribute;


namespace Linguini.Serialization.Test;

[TestFixture]
public class SerializeAndDeserializeTest
{
[Test]
[TestCaseSource(nameof(AstExamples))]
[Parallelizable]
public void SerializeDeserializeTest(object x)
{
SerializeAndDeserializeTest.SerializeDeserializeTest(x);
}

public static IEnumerable<object> AstExamples()
{
yield return new CallArgumentsBuilder()
.AddPositionalArg(InlineExpressionBuilder.CreateMessageReference("x"))
.AddNamedArg("y", 3)
.Build();
yield return new Attribute("desc", new PatternBuilder("description"));
yield return new DynamicReference("dyn", "attr", new CallArgumentsBuilder()
.AddPositionalArg(InlineExpressionBuilder.CreateMessageReference("x"))
.AddNamedArg("y", 3));
yield return new FunctionReference("foo", new CallArgumentsBuilder()
.AddPositionalArg(3)
.AddNamedArg("test", InlineExpressionBuilder.CreateTermReference("x", "y"))
.Build()
);
yield return new Identifier("test");
}

private static void SerializeDeserializeTest<T>(T expected)
{
// Serialize the object to JSON string.
var jsonString = JsonSerializer.Serialize(expected, Options);

// Deserialize the JSON string back into an object.
Debug.Assert(expected != null, nameof(expected) + " != null");
var deserializedObject = JsonSerializer.Deserialize(jsonString, expected.GetType(), Options);

// Now you have a 'deserializedObject' which should be equivalent to the original 'expected' object.
Assert.That(deserializedObject, Is.Not.Null);
Assert.That(deserializedObject, Is.EqualTo(expected));
}

private static readonly JsonSerializerOptions Options = new()
{
IgnoreReadOnlyFields = false,
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters =
{
new AttributeSerializer(),
new CallArgumentsSerializer(),
new CommentSerializer(),
new FunctionReferenceSerializer(),
new IdentifierSerializer(),
new JunkSerializer(),
new MessageReferenceSerializer(),
new MessageSerializer(),
new DynamicReferenceSerializer(),
new NamedArgumentSerializer(),
new ParseErrorSerializer(),
new PatternSerializer(),
new PlaceableSerializer(),
new ResourceSerializer(),
new PlaceableSerializer(),
new SelectExpressionSerializer(),
new TermReferenceSerializer(),
new TermSerializer(),
new VariantSerializer(),
new VariableReferenceSerializer(),
}
};
}
35 changes: 5 additions & 30 deletions Linguini.Serialization.Test/TestUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,15 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Linguini.Serialization.Converters;
using NUnit.Framework;

namespace Linguini.Serialization.Test
{
public static class TestUtil
{
public static readonly JsonSerializerOptions Options = new()
{
IgnoreReadOnlyFields = false,
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters =
{
new AttributeSerializer(),
new CallArgumentsSerializer(),
new CommentSerializer(),
new FunctionReferenceSerializer(),
new IdentifierSerializer(),
new JunkSerializer(),
new MessageReferenceSerializer(),
new MessageSerializer(),
new DynamicReferenceSerializer(),
new NamedArgumentSerializer(),
new ParseErrorSerializer(),
new PatternSerializer(),
new PlaceableSerializer(),
new ResourceSerializer(),
new PlaceableSerializer(),
new SelectExpressionSerializer(),
new TermReferenceSerializer(),
new TermSerializer(),
new VariantSerializer(),
new VariableReferenceSerializer(),
}
};




}
}
10 changes: 5 additions & 5 deletions Linguini.Serialization/Converters/DynamicReferenceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class DynamicReferenceSerializer : JsonConverter<DynamicReference>
public override DynamicReference? Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
throw new NotImplementedException();
var el = JsonSerializer.Deserialize<JsonElement>(ref reader, options);
return ProcessDynamicReference(el, options);
}

public override void Write(Utf8JsonWriter writer, DynamicReference dynRef, JsonSerializerOptions options)
Expand Down Expand Up @@ -40,9 +41,8 @@ public override void Write(Utf8JsonWriter writer, DynamicReference dynRef, JsonS
public static DynamicReference ProcessDynamicReference(JsonElement el,
JsonSerializerOptions options)
{
Identifier? identifier = null;
if (!el.TryGetProperty("id", out var jsonId) &&
!IdentifierSerializer.TryGetIdentifier(jsonId, options, out identifier))
if (!el.TryGetProperty("id", out var jsonId) ||
!IdentifierSerializer.TryGetIdentifier(jsonId, options, out var identifier))
{
throw new JsonException("Dynamic reference must contain at least `id` field");
}
Expand All @@ -59,7 +59,7 @@ public static DynamicReference ProcessDynamicReference(JsonElement el,
CallArgumentsSerializer.TryGetCallArguments(jsonArgs, options, out arguments);
}

return new DynamicReference(identifier!, attribute, arguments);
return new DynamicReference(identifier, attribute, arguments);
}
}
}
Loading

0 comments on commit f58f3bf

Please sign in to comment.