From cb3ae1219fe0998dbebbff1d5df4dfceb4ca9bf6 Mon Sep 17 00:00:00 2001 From: Matthew Adams Date: Fri, 27 Sep 2024 14:51:50 +0100 Subject: [PATCH] Added support for JsonSerializerOptions in the Serialize() extension method. --- .../Corvus.Json/JsonValueExtensions.cs | 15 +++++++++- .../JsonModel/JsonSerialization.feature | 10 +++++++ .../Corvus.Json.Specs/Steps/JsonValueSteps.cs | 28 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Solutions/Corvus.Json.ExtendedTypes/Corvus.Json/JsonValueExtensions.cs b/Solutions/Corvus.Json.ExtendedTypes/Corvus.Json/JsonValueExtensions.cs index 54a7d8b88..3c6b5fd94 100644 --- a/Solutions/Corvus.Json.ExtendedTypes/Corvus.Json/JsonValueExtensions.cs +++ b/Solutions/Corvus.Json.ExtendedTypes/Corvus.Json/JsonValueExtensions.cs @@ -109,6 +109,19 @@ public static string Serialize(this TValue value) return JsonSerializer.Serialize(value); } + /// + /// Serialize the entity to a string. + /// + /// The type of . + /// The value to serialize. + /// The JSON serializer options. + /// A string representation fo the value. + public static string Serialize(this TValue value, JsonSerializerOptions options) + where TValue : struct, IJsonValue + { + return JsonSerializer.Serialize(value, options); + } + /// /// Gets a value indicating whether this value is null. /// @@ -117,7 +130,7 @@ public static string Serialize(this TValue value) /// True if the value is null. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsNull(this T value) - where T : struct, IJsonValue + where T : struct, IJsonValue { return value.ValueKind == JsonValueKind.Null; } diff --git a/Solutions/Corvus.Json.Specs/Features/JsonModel/JsonSerialization.feature b/Solutions/Corvus.Json.Specs/Features/JsonModel/JsonSerialization.feature index a758dd536..660b89222 100644 --- a/Solutions/Corvus.Json.Specs/Features/JsonModel/JsonSerialization.feature +++ b/Solutions/Corvus.Json.Specs/Features/JsonModel/JsonSerialization.feature @@ -80,6 +80,16 @@ Scenario: Write a dotnet-backed JsonObject to a string Then the round-tripped result should be an Object And the round-tripped result should be equal to the JsonAny {"foo": 3, "bar": "hello", "baz": null} +Scenario: Write a jsonelement-backed JsonObject to a string with pretty formatting + Given the JsonElement backed JsonObject {"foo": 3, "bar": "hello", "baz": null} + When the json value is serialized to a string using pretty formatting + Then the serialized string should equal {\r\n "foo": 3,\r\n "bar": "hello",\r\n "baz": null\r\n} + +Scenario: Write a dotnet-backed JsonObject to a string with pretty formatting + Given the dotnet backed JsonObject {"foo": 3, "bar": "hello", "baz": null} + When the json value is serialized to a string using pretty formatting + Then the serialized string should equal {\r\n "foo": 3,\r\n "bar": "hello",\r\n "baz": null\r\n} + Scenario: Write a jsonelement-backed JsonArray to a string Given the JsonElement backed JsonArray [1,2,"3",4.0] When the json value is round-tripped via a string diff --git a/Solutions/Corvus.Json.Specs/Steps/JsonValueSteps.cs b/Solutions/Corvus.Json.Specs/Steps/JsonValueSteps.cs index f5bc8f91f..5c13cf9f5 100644 --- a/Solutions/Corvus.Json.Specs/Steps/JsonValueSteps.cs +++ b/Solutions/Corvus.Json.Specs/Steps/JsonValueSteps.cs @@ -120,6 +120,24 @@ public void WhenTheJsonValueIsRound_TrippedViaAString() this.scenarioContext.Set(JsonAny.ParseValue(abw.WrittenSpan), SerializationResult); } + /// + /// Serializes an from the context variable , stores the resulting value in the context variable . + /// + [When("the json value is serialized to a string using pretty formatting")] + public void WhenTheJsonValueIsSerializedToAStringUsingPrettyFormatting() + { + try + { + IJsonValue sut = this.scenarioContext.Get(SubjectUnderTest); + string json = sut.AsAny.Serialize(new JsonSerializerOptions { WriteIndented = true }); + this.scenarioContext.Set(json, SerializationResult); + } + catch (Exception ex) + { + this.scenarioContext.Set(ex, SerializationException); + } + } + /// /// Serializes an from the context variable , deserializes and stores the resulting in the context variable . /// @@ -182,6 +200,16 @@ public void ThenTheRound_TrippedResultShouldBeEqualToTheJsonAny(string expected) Assert.AreEqual(JsonAny.Parse(expected), this.scenarioContext.Get(SerializationResult)); } + /// + /// Compares the string from the context variable with the expected value. + /// + /// The expected value. + [Then("the serialized string should equal (.*)")] + public void ThenTheSerializedStringShouldEqual(string expected) + { + Assert.AreEqual(expected.Replace("\\r", "\r").Replace("\\n", "\n"), this.scenarioContext.Get(SerializationResult)); + } + /* notAny */ ///