Show / Hide Table of Contents

Class JsonMergePatch

Provides functionality for applying a JSON Merge Patch as defined in RFC 7396 to a JSON value.

Inheritance
System.Object
JsonMergePatch
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: JsonCons.Utilities
Assembly: JsonCons.Utilities.dll
Syntax
public static class JsonMergePatch
Examples

The following example borrowed from RFC 7396 shows how to apply a JSON Merge Patch to a JSON value

using System;
using System.Diagnostics;
using System.Text.Json;
using JsonCons.Utilities;

public class Example
{
   public static void Main()
   {
    using var doc = JsonDocument.Parse(@"
{
     ""title"": ""Goodbye!"",
     ""author"" : {
   ""givenName"" : ""John"",
   ""familyName"" : ""Doe""
     },
     ""tags"":[ ""example"", ""sample"" ],
     ""content"": ""This will be unchanged""
}
    ");

    using var patch = JsonDocument.Parse(@"
{
     ""title"": ""Hello!"",
     ""phoneNumber"": ""+01-123-456-7890"",
     ""author"": {
   ""familyName"": null
     },
     ""tags"": [ ""example"" ]
}
        ");

    using JsonDocument result = JsonMergePatch.ApplyMergePatch(doc.RootElement, patch.RootElement);

    var options = new JsonSerializerOptions() { WriteIndented = true };

    Console.WriteLine("The original document:\n");
    Console.WriteLine($"{JsonSerializer.Serialize(doc, options)}\n");
    Console.WriteLine("The patch:\n");
    Console.WriteLine($"{JsonSerializer.Serialize(patch, options)}\n");
    Console.WriteLine("The result:\n");
    Console.WriteLine($"{JsonSerializer.Serialize(result, options)}\n");
       ");
    }
}

The original document:

{
  "title": "Goodbye!",
  "author": {
    "givenName": "John",
    "familyName": "Doe"
  },
  "tags": [
    "example",
    "sample"
  ],
  "content": "This will be unchanged"
}

The patch:

{
  "title": "Hello!",
  "phoneNumber": "\u002B01-123-456-7890",
  "author": {
    "familyName": null
  },
  "tags": [
    "example"
  ]
}

The result:

{
  "title": "Hello!",
  "author": {
    "givenName": "John"
  },
  "tags": [
    "example"
  ],
  "content": "This will be unchanged",
  "phoneNumber": "\u002B01-123-456-7890"
}    

Methods

| Improve this Doc View Source

ApplyMergePatch(JsonElement, JsonElement)

Applies a JSON Merge Patch as defined in RFC 7396 to a source JSON value.

Declaration
public static JsonDocument ApplyMergePatch(JsonElement source, JsonElement patch)
Parameters
Type Name Description
System.Text.Json.JsonElement source

The source JSON value.

System.Text.Json.JsonElement patch

The JSON merge patch to be applied to the source JSON value.

Returns
Type Description
System.Text.Json.JsonDocument

The patched JSON value

Remarks

It is the users responsibilty to properly Dispose the returned System.Text.Json.JsonDocument value

| Improve this Doc View Source

FromDiff(JsonElement, JsonElement)

Builds a JSON Merge Patch as defined in RFC 7396 given two JSON values, a source and a target.

Declaration
public static JsonDocument FromDiff(JsonElement source, JsonElement target)
Parameters
Type Name Description
System.Text.Json.JsonElement source

The source JSON value.

System.Text.Json.JsonElement target

The target JSON value.

Returns
Type Description
System.Text.Json.JsonDocument

A JSON Merge Patch to convert the source JSON value to the target JSON value

Remarks

It is the users responsibilty to properly Dispose the returned System.Text.Json.JsonDocument value

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX