Skip to content

Commit b75fcd1

Browse files
authored
Added support for JsonSerializerOptions in the Serialize() extension method. (#438)
1 parent bf9b67f commit b75fcd1

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

Solutions/Corvus.Json.ExtendedTypes/Corvus.Json/JsonValueExtensions.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ public static string Serialize<TValue>(this TValue value)
109109
return JsonSerializer.Serialize(value);
110110
}
111111

112+
/// <summary>
113+
/// Serialize the entity to a string.
114+
/// </summary>
115+
/// <typeparam name="TValue">The type of <see cref="IJsonValue"/>.</typeparam>
116+
/// <param name="value">The value to serialize.</param>
117+
/// <param name="options">The JSON serializer options.</param>
118+
/// <returns>A string representation fo the value.</returns>
119+
public static string Serialize<TValue>(this TValue value, JsonSerializerOptions options)
120+
where TValue : struct, IJsonValue
121+
{
122+
return JsonSerializer.Serialize(value, options);
123+
}
124+
112125
/// <summary>
113126
/// Gets a value indicating whether this value is null.
114127
/// </summary>
@@ -117,7 +130,7 @@ public static string Serialize<TValue>(this TValue value)
117130
/// <returns><c>True</c> if the value is null.</returns>
118131
[MethodImpl(MethodImplOptions.AggressiveInlining)]
119132
public static bool IsNull<T>(this T value)
120-
where T : struct, IJsonValue
133+
where T : struct, IJsonValue
121134
{
122135
return value.ValueKind == JsonValueKind.Null;
123136
}

Solutions/Corvus.Json.Specs/Features/JsonModel/JsonSerialization.feature

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ Scenario: Write a dotnet-backed JsonObject to a string
8080
Then the round-tripped result should be an Object
8181
And the round-tripped result should be equal to the JsonAny {"foo": 3, "bar": "hello", "baz": null}
8282

83+
Scenario: Write a jsonelement-backed JsonObject to a string with pretty formatting
84+
Given the JsonElement backed JsonObject {"foo": 3, "bar": "hello", "baz": null}
85+
When the json value is serialized to a string using pretty formatting
86+
Then the serialized string should equal {\r\n "foo": 3,\r\n "bar": "hello",\r\n "baz": null\r\n}
87+
88+
Scenario: Write a dotnet-backed JsonObject to a string with pretty formatting
89+
Given the dotnet backed JsonObject {"foo": 3, "bar": "hello", "baz": null}
90+
When the json value is serialized to a string using pretty formatting
91+
Then the serialized string should equal {\r\n "foo": 3,\r\n "bar": "hello",\r\n "baz": null\r\n}
92+
8393
Scenario: Write a jsonelement-backed JsonArray to a string
8494
Given the JsonElement backed JsonArray [1,2,"3",4.0]
8595
When the json value is round-tripped via a string

Solutions/Corvus.Json.Specs/Steps/JsonValueSteps.cs

+28
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ public void WhenTheJsonValueIsRound_TrippedViaAString()
120120
this.scenarioContext.Set(JsonAny.ParseValue(abw.WrittenSpan), SerializationResult);
121121
}
122122

123+
/// <summary>
124+
/// Serializes an <see cref="IJsonValue"/> from the context variable <see cref="SubjectUnderTest"/>, stores the resulting value in the context variable <see cref="SerializationResult"/>.
125+
/// </summary>
126+
[When("the json value is serialized to a string using pretty formatting")]
127+
public void WhenTheJsonValueIsSerializedToAStringUsingPrettyFormatting()
128+
{
129+
try
130+
{
131+
IJsonValue sut = this.scenarioContext.Get<IJsonValue>(SubjectUnderTest);
132+
string json = sut.AsAny.Serialize(new JsonSerializerOptions { WriteIndented = true });
133+
this.scenarioContext.Set(json, SerializationResult);
134+
}
135+
catch (Exception ex)
136+
{
137+
this.scenarioContext.Set(ex, SerializationException);
138+
}
139+
}
140+
123141
/// <summary>
124142
/// 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"/>.
125143
/// </summary>
@@ -182,6 +200,16 @@ public void ThenTheRound_TrippedResultShouldBeEqualToTheJsonAny(string expected)
182200
Assert.AreEqual(JsonAny.Parse(expected), this.scenarioContext.Get<JsonAny>(SerializationResult));
183201
}
184202

203+
/// <summary>
204+
/// Compares the string from the context variable <see cref="SerializationResult"/> with the expected value.
205+
/// </summary>
206+
/// <param name="expected">The expected value.</param>
207+
[Then("the serialized string should equal (.*)")]
208+
public void ThenTheSerializedStringShouldEqual(string expected)
209+
{
210+
Assert.AreEqual(expected.Replace("\\r", "\r").Replace("\\n", "\n"), this.scenarioContext.Get<string>(SerializationResult));
211+
}
212+
185213
/* notAny */
186214

187215
/// <summary>

0 commit comments

Comments
 (0)