Skip to content

Commit cc66a85

Browse files
authored
Merge pull request #472 from gregsdennis/patch/v2.1.0
Patch/v2.1.0
2 parents 29aff51 + 03fb89a commit cc66a85

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

JsonPatch.Tests/PatchExtensionTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,34 @@ public void ApplyPatch_Respect_SerializationOptions()
326326
Assert.AreEqual(5, final?.Numbers?[0]);
327327
}
328328

329+
[Test]
330+
public void CreatePatch_JsonContext()
331+
{
332+
var initial = new TestModel
333+
{
334+
Id = Guid.NewGuid(),
335+
Attributes = JsonDocument.Parse("[{\"test\":\"test123\"},{\"test\":\"test321\"},{\"test\":[1,2,3]},{\"test\":[1,2,4]}]").RootElement
336+
};
337+
var expected = new TestModel
338+
{
339+
Id = Guid.Parse("40664cc7-864f-4eed-939c-78076a252df0"),
340+
Attributes = JsonDocument.Parse("[{\"test\":\"test123\"},{\"test\":\"test32132\"},{\"test1\":\"test321\"},{\"test\":[1,2,3]},{\"test\":[1,2,3]}]").RootElement
341+
};
342+
var patchExpected =
343+
"[{\"op\":\"replace\",\"path\":\"/Id\",\"value\":\"40664cc7-864f-4eed-939c-78076a252df0\"}," +
344+
"{\"op\":\"replace\",\"path\":\"/Attributes/1/test\",\"value\":\"test32132\"}," +
345+
"{\"op\":\"remove\",\"path\":\"/Attributes/2/test\"}," +
346+
"{\"op\":\"add\",\"path\":\"/Attributes/2/test1\",\"value\":\"test321\"}," +
347+
"{\"op\":\"replace\",\"path\":\"/Attributes/3/test/2\",\"value\":3}," +
348+
"{\"op\":\"add\",\"path\":\"/Attributes/4\",\"value\":{\"test\":[1,2,3]}}]";
349+
350+
var patch = initial.CreatePatch(expected);
351+
// use source generated json serializer context
352+
var patchJson = JsonSerializer.Serialize(patch, TestJsonContext.Default.JsonPatch);
353+
354+
Assert.AreEqual(patchExpected, patchJson);
355+
}
356+
329357
private static void OutputPatch(JsonPatch patch)
330358
{
331359
Console.WriteLine(JsonSerializer.Serialize(patch, new JsonSerializerOptions { WriteIndented = true }));
@@ -338,4 +366,13 @@ private static void VerifyPatches(JsonPatch expected, JsonPatch actual)
338366

339367
Assert.AreEqual(expected, actual);
340368
}
369+
370+
371+
372+
}
373+
374+
[JsonSerializable(typeof(JsonPatch))]
375+
public partial class TestJsonContext : JsonSerializerContext
376+
{
377+
341378
}

JsonPatch/JsonPatch.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,27 @@ public override int GetHashCode()
8383
}
8484
}
8585

86-
internal class PatchJsonConverter : JsonConverter<JsonPatch>
86+
/// <summary>
87+
/// Provides JSON conversion logic for <see cref="JsonPatch"/>.
88+
/// </summary>
89+
public class PatchJsonConverter : JsonConverter<JsonPatch>
8790
{
91+
/// <summary>Reads and converts the JSON to type <see cref="JsonPatch"/>.</summary>
92+
/// <param name="reader">The reader.</param>
93+
/// <param name="typeToConvert">The type to convert.</param>
94+
/// <param name="options">An object that specifies serialization options to use.</param>
95+
/// <returns>The converted value.</returns>
8896
public override JsonPatch Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
8997
{
9098
var operations = JsonSerializer.Deserialize<List<PatchOperation>>(ref reader, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase })!;
9199

92100
return new JsonPatch(operations);
93101
}
94102

103+
/// <summary>Writes a specified value as JSON.</summary>
104+
/// <param name="writer">The writer to write to.</param>
105+
/// <param name="value">The value to convert to JSON.</param>
106+
/// <param name="options">An object that specifies serialization options to use.</param>
95107
public override void Write(Utf8JsonWriter writer, JsonPatch value, JsonSerializerOptions options)
96108
{
97109
JsonSerializer.Serialize(writer, value.Operations);

JsonPatch/JsonPatch.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>latest</LangVersion>
66
<RootNamespace>Json.Patch</RootNamespace>
7-
<Version>2.0.6</Version>
7+
<Version>2.1.0</Version>
88
<AssemblyVersion>2.0.0.0</AssemblyVersion>
9-
<FileVersion>2.0.6.0</FileVersion>
9+
<FileVersion>2.1.0.0</FileVersion>
1010
<PackageId>JsonPatch.Net</PackageId>
1111
<Authors>Greg Dennis</Authors>
1212
<Company>Greg Dennis</Company>

tools/ApiDocsGenerator/release-notes/rn-json-patch.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ title: JsonPatch.Net
44
icon: fas fa-tag
55
order: "8.08"
66
---
7+
# [2.1.0](https://github.com/gregsdennis/json-everything/pull/472) {#release-patch-2.1.0}
8+
9+
[#471](https://github.com/gregsdennis/json-everything/issues/397) - Make patch json converter public to support .Net source generation. Thanks to [@pwelter34](https://github.com/pwelter34) for highlighting this use case.
10+
711
# [2.0.6](https://github.com/gregsdennis/json-everything/pull/400) {#release-patch-2.0.6}
812

913
[#397](https://github.com/gregsdennis/json-everything/issues/397) - Fixed an issue where `replace` needs to check that the target location exists before proceeding with the `add` portion of its operation.

0 commit comments

Comments
 (0)