Skip to content

Commit

Permalink
Added support for JsonSerializerOptions in the Serialize() extension …
Browse files Browse the repository at this point in the history
…method.
  • Loading branch information
mwadams committed Sep 27, 2024
1 parent bf9b67f commit cb3ae12
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ public static string Serialize<TValue>(this TValue value)
return JsonSerializer.Serialize(value);
}

/// <summary>
/// Serialize the entity to a string.
/// </summary>
/// <typeparam name="TValue">The type of <see cref="IJsonValue"/>.</typeparam>
/// <param name="value">The value to serialize.</param>
/// <param name="options">The JSON serializer options.</param>
/// <returns>A string representation fo the value.</returns>
public static string Serialize<TValue>(this TValue value, JsonSerializerOptions options)
where TValue : struct, IJsonValue
{
return JsonSerializer.Serialize(value, options);
}

/// <summary>
/// Gets a value indicating whether this value is null.
/// </summary>
Expand All @@ -117,7 +130,7 @@ public static string Serialize<TValue>(this TValue value)
/// <returns><c>True</c> if the value is null.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNull<T>(this T value)
where T : struct, IJsonValue
where T : struct, IJsonValue
{
return value.ValueKind == JsonValueKind.Null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions Solutions/Corvus.Json.Specs/Steps/JsonValueSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ public void WhenTheJsonValueIsRound_TrippedViaAString()
this.scenarioContext.Set(JsonAny.ParseValue(abw.WrittenSpan), SerializationResult);
}

/// <summary>
/// Serializes an <see cref="IJsonValue"/> from the context variable <see cref="SubjectUnderTest"/>, stores the resulting value in the context variable <see cref="SerializationResult"/>.
/// </summary>
[When("the json value is serialized to a string using pretty formatting")]
public void WhenTheJsonValueIsSerializedToAStringUsingPrettyFormatting()
{
try
{
IJsonValue sut = this.scenarioContext.Get<IJsonValue>(SubjectUnderTest);
string json = sut.AsAny.Serialize(new JsonSerializerOptions { WriteIndented = true });
this.scenarioContext.Set(json, SerializationResult);
}
catch (Exception ex)
{
this.scenarioContext.Set(ex, SerializationException);
}
}

/// <summary>
/// Serializes an <see cref="IJsonValue"/> from the context variable <see cref="SubjectUnderTest"/>, deserializes and stores the resulting <see cref="JsonAny"/> in the context variable <see cref="SerializationResult"/>.
/// </summary>
Expand Down Expand Up @@ -182,6 +200,16 @@ public void ThenTheRound_TrippedResultShouldBeEqualToTheJsonAny(string expected)
Assert.AreEqual(JsonAny.Parse(expected), this.scenarioContext.Get<JsonAny>(SerializationResult));
}

/// <summary>
/// Compares the string from the context variable <see cref="SerializationResult"/> with the expected value.
/// </summary>
/// <param name="expected">The expected value.</param>
[Then("the serialized string should equal (.*)")]
public void ThenTheSerializedStringShouldEqual(string expected)
{
Assert.AreEqual(expected.Replace("\\r", "\r").Replace("\\n", "\n"), this.scenarioContext.Get<string>(SerializationResult));
}

/* notAny */

/// <summary>
Expand Down

0 comments on commit cb3ae12

Please sign in to comment.