Skip to content

Commit

Permalink
Merge pull request #472 from gregsdennis/patch/v2.1.0
Browse files Browse the repository at this point in the history
Patch/v2.1.0
  • Loading branch information
gregsdennis committed Jun 12, 2023
2 parents 29aff51 + 03fb89a commit cc66a85
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
37 changes: 37 additions & 0 deletions JsonPatch.Tests/PatchExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,34 @@ public void ApplyPatch_Respect_SerializationOptions()
Assert.AreEqual(5, final?.Numbers?[0]);
}

[Test]
public void CreatePatch_JsonContext()
{
var initial = new TestModel
{
Id = Guid.NewGuid(),
Attributes = JsonDocument.Parse("[{\"test\":\"test123\"},{\"test\":\"test321\"},{\"test\":[1,2,3]},{\"test\":[1,2,4]}]").RootElement
};
var expected = new TestModel
{
Id = Guid.Parse("40664cc7-864f-4eed-939c-78076a252df0"),
Attributes = JsonDocument.Parse("[{\"test\":\"test123\"},{\"test\":\"test32132\"},{\"test1\":\"test321\"},{\"test\":[1,2,3]},{\"test\":[1,2,3]}]").RootElement
};
var patchExpected =
"[{\"op\":\"replace\",\"path\":\"/Id\",\"value\":\"40664cc7-864f-4eed-939c-78076a252df0\"}," +
"{\"op\":\"replace\",\"path\":\"/Attributes/1/test\",\"value\":\"test32132\"}," +
"{\"op\":\"remove\",\"path\":\"/Attributes/2/test\"}," +
"{\"op\":\"add\",\"path\":\"/Attributes/2/test1\",\"value\":\"test321\"}," +
"{\"op\":\"replace\",\"path\":\"/Attributes/3/test/2\",\"value\":3}," +
"{\"op\":\"add\",\"path\":\"/Attributes/4\",\"value\":{\"test\":[1,2,3]}}]";

var patch = initial.CreatePatch(expected);
// use source generated json serializer context
var patchJson = JsonSerializer.Serialize(patch, TestJsonContext.Default.JsonPatch);

Assert.AreEqual(patchExpected, patchJson);
}

private static void OutputPatch(JsonPatch patch)
{
Console.WriteLine(JsonSerializer.Serialize(patch, new JsonSerializerOptions { WriteIndented = true }));
Expand All @@ -338,4 +366,13 @@ private static void VerifyPatches(JsonPatch expected, JsonPatch actual)

Assert.AreEqual(expected, actual);
}



}

[JsonSerializable(typeof(JsonPatch))]
public partial class TestJsonContext : JsonSerializerContext
{

}
14 changes: 13 additions & 1 deletion JsonPatch/JsonPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,27 @@ public override int GetHashCode()
}
}

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

return new JsonPatch(operations);
}

/// <summary>Writes a specified value as JSON.</summary>
/// <param name="writer">The writer to write to.</param>
/// <param name="value">The value to convert to JSON.</param>
/// <param name="options">An object that specifies serialization options to use.</param>
public override void Write(Utf8JsonWriter writer, JsonPatch value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value.Operations);
Expand Down
4 changes: 2 additions & 2 deletions JsonPatch/JsonPatch.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>Json.Patch</RootNamespace>
<Version>2.0.6</Version>
<Version>2.1.0</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<FileVersion>2.0.6.0</FileVersion>
<FileVersion>2.1.0.0</FileVersion>
<PackageId>JsonPatch.Net</PackageId>
<Authors>Greg Dennis</Authors>
<Company>Greg Dennis</Company>
Expand Down
4 changes: 4 additions & 0 deletions tools/ApiDocsGenerator/release-notes/rn-json-patch.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ title: JsonPatch.Net
icon: fas fa-tag
order: "8.08"
---
# [2.1.0](https://github.com/gregsdennis/json-everything/pull/472) {#release-patch-2.1.0}

[#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.

# [2.0.6](https://github.com/gregsdennis/json-everything/pull/400) {#release-patch-2.0.6}

[#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.
Expand Down

0 comments on commit cc66a85

Please sign in to comment.