├── .gitattributes ├── .gitignore ├── LICENSE ├── Palit.AspNetCore.JsonPatch.Extensions.Generate.Test ├── Comparers │ └── GenericDeepEqualityComparer.cs ├── JsonPatchDocumentDiffObserverTests.cs ├── JsonPatchDocumentGeneratorTests.cs ├── Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.csproj └── TestModels │ ├── InheritedClass.cs │ ├── NestedClass.cs │ └── TestClass.cs ├── Palit.AspNetCore.JsonPatch.Extensions.Generate.sln ├── Palit.AspNetCore.JsonPatch.Extensions.Generate ├── JsonPatchDocumentDiffObserver.cs ├── JsonPatchDocumentGenerator.cs └── Palit.AspNetCore.JsonPatch.Extensions.Generate.csproj └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.Test/Comparers/GenericDeepEqualityComparer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Linq; 5 | 6 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.Comparers 7 | { 8 | /// 9 | /// Generic comparer that does a simple deep equality comparison. 10 | /// 11 | /// 12 | /// 13 | public class GenericDeepEqualityComparer : IEqualityComparer 14 | { 15 | /// 16 | /// Determines whether the specified objects are equal. 17 | /// 18 | /// The first object of type T to compare. 19 | /// The second object of type T to compare. 20 | /// 21 | /// true if the specified objects are equal; otherwise, false. 22 | /// 23 | public bool Equals(T x, T y) 24 | { 25 | var props = typeof(T).GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance); 26 | foreach (var prop in props) 27 | { 28 | var expectedValue = prop.GetValue(x, null); 29 | var actualValue = prop.GetValue(y, null); 30 | 31 | // Avoid null reference errors. 32 | if (null == expectedValue && null == actualValue) 33 | { 34 | continue; 35 | } 36 | 37 | if (expectedValue.GetType() != typeof(string) && expectedValue is IEnumerable expectedEnumerable && actualValue is IEnumerable actualEnumerable) 38 | { 39 | // Gets the generic type of the expected value ienumerable. 40 | var genericType = expectedValue.GetType().GetInterfaces().Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>)).Select(t => t.GetGenericArguments()[0]).First(); 41 | var actualGenericType = actualValue.GetType().GetInterfaces().Where(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>)).Select(t => t.GetGenericArguments()[0]).First(); 42 | 43 | if (genericType != actualGenericType) 44 | { 45 | return false; 46 | } 47 | 48 | var actualEnumerator = actualEnumerable.GetEnumerator(); 49 | foreach (var val in expectedEnumerable) 50 | { 51 | actualEnumerator.MoveNext(); 52 | if (!val.Equals(actualEnumerator.Current)) 53 | { 54 | return false; 55 | } 56 | } 57 | } 58 | else if (!expectedValue.Equals(actualValue)) 59 | { 60 | return false; 61 | } 62 | } 63 | 64 | return true; 65 | } 66 | 67 | /// 68 | /// Returns a hash code for this instance. 69 | /// 70 | /// The object. 71 | /// 72 | /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. 73 | /// 74 | public int GetHashCode(T obj) 75 | { 76 | return obj.GetHashCode(); 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.Test/JsonPatchDocumentDiffObserverTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using Xunit; 5 | using Palit.AspNetCore.JsonPatch.Extensions.Generate; 6 | using Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.TestModels; 7 | using Microsoft.AspNetCore.JsonPatch.Operations; 8 | using Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.Comparers; 9 | 10 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate.Test 11 | { 12 | public class JsonPatchDocumentDiffObserverTests 13 | { 14 | [Fact] 15 | public void ItExists() 16 | { 17 | var instance = new TestClass(); 18 | var observer = new JsonPatchDocumentDiffObserver(instance); 19 | } 20 | 21 | [Fact] 22 | public void ItGeneratesNoChangesWithNoDiff() 23 | { 24 | var instance = new TestClass 25 | { 26 | Id = "id", 27 | Message = "message", 28 | GuidValue = Guid.Empty, 29 | DecimalValue = 1.23m, 30 | IntList = new List { 1, 2, 3 } 31 | }; 32 | 33 | var observer = new JsonPatchDocumentDiffObserver(instance); 34 | var patch = observer.Generate(); 35 | 36 | Assert.NotNull(patch); 37 | Assert.Empty(patch.Operations); 38 | } 39 | 40 | [Fact] 41 | public void ItGeneratesCorrectDiff() 42 | { 43 | var instance = new TestClass 44 | { 45 | Id = "id", 46 | Message = "message", 47 | GuidValue = Guid.Empty, 48 | DecimalValue = 1.23m, 49 | IntList = new List { 1, 2, 3 } 50 | }; 51 | 52 | var observer = new JsonPatchDocumentDiffObserver(instance); 53 | instance.Id = "new-id"; 54 | instance.Message = "new-message"; 55 | instance.GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"); 56 | instance.DecimalValue = 1.89m; 57 | instance.IntList = new List { 3, 2, 1 }; 58 | 59 | var patch = observer.Generate(); 60 | 61 | Assert.NotNull(patch); 62 | Assert.Equal(5, patch.Operations.Count); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.Test/JsonPatchDocumentGeneratorTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using Xunit; 4 | using Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.TestModels; 5 | using Microsoft.AspNetCore.JsonPatch.Operations; 6 | using Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.Comparers; 7 | using Newtonsoft.Json; 8 | 9 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate.Test 10 | { 11 | public class JsonPatchDocumentGeneratorTests 12 | { 13 | [Fact] 14 | public void ItExists() 15 | { 16 | var generator = new JsonPatchDocumentGenerator(); 17 | } 18 | 19 | [Fact] 20 | public void ItGetsNoDiffWithSameInstance() 21 | { 22 | var testInstance = new TestClass() { Id = "id" }; 23 | var reference = testInstance; 24 | reference.Id = "new id"; 25 | 26 | var generator = new JsonPatchDocumentGenerator(); 27 | var patch = generator.Generate(testInstance, reference); 28 | 29 | Assert.NotNull(patch); 30 | Assert.Empty(patch.Operations); 31 | } 32 | 33 | [Fact] 34 | public void ItGetsNoDiffWithIdenticalObjects() 35 | { 36 | var original = new TestClass() 37 | { 38 | Id = "id", 39 | Message = "message", 40 | DecimalValue = 1.43m, 41 | GuidValue = Guid.Empty, 42 | IntList = new List() { 1, 2, 3 } 43 | }; 44 | var modified = new TestClass() 45 | { 46 | Id = "id", 47 | Message = "message", 48 | DecimalValue = 1.43m, 49 | GuidValue = Guid.Empty, 50 | IntList = new List() { 1, 2, 3 } 51 | }; 52 | 53 | var generator = new JsonPatchDocumentGenerator(); 54 | var patch = generator.Generate(original, modified); 55 | 56 | Assert.NotNull(patch); 57 | Assert.Empty(patch.Operations); 58 | } 59 | 60 | [Fact] 61 | public void ItGeneratesSimpleCorrectPatchDocument() 62 | { 63 | var original = new TestClass() 64 | { 65 | Id = "id", 66 | Message = "message", 67 | DecimalValue = 1.43m, 68 | GuidValue = Guid.Empty, 69 | IntList = new List() { 1, 2, 3 } 70 | }; 71 | var modified = new TestClass() 72 | { 73 | Id = "new-id", 74 | Message = null, 75 | DecimalValue = 1.68m, 76 | GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"), 77 | IntList = new List() { 1, 3, 2 } 78 | }; 79 | 80 | var generator = new JsonPatchDocumentGenerator(); 81 | var patch = generator.Generate(original, modified); 82 | 83 | // Modify original with patch. 84 | patch.ApplyTo(original); 85 | 86 | Assert.NotNull(patch); 87 | Assert.Equal(5, patch.Operations.Count); 88 | Assert.Equal(original, modified, new GenericDeepEqualityComparer()); 89 | } 90 | 91 | [Fact] 92 | public void ItGeneratesUsingCustomJsonSerializer() 93 | { 94 | var original = new TestClass() 95 | { 96 | Id = null, 97 | Message = "message", 98 | DecimalValue = 1.43m, 99 | GuidValue = Guid.Empty, 100 | IntList = new List() { 1, 2, 3 } 101 | }; 102 | var modified = new TestClass() 103 | { 104 | Id = "new-id", 105 | Message = null, 106 | DecimalValue = 1.68m, 107 | GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"), 108 | IntList = new List() { 1, 3, 2 } 109 | }; 110 | 111 | var generator = new JsonPatchDocumentGenerator(); 112 | var jsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore }; 113 | var patch = generator.Generate(original, modified, jsonSerializer); 114 | 115 | // Modify original with patch. 116 | patch.ApplyTo(original); 117 | 118 | Assert.NotNull(patch); 119 | Assert.Equal(5, patch.Operations.Count); 120 | Assert.Contains(patch.Operations, op => op.OperationType == OperationType.Add); 121 | Assert.Contains(patch.Operations, op => op.OperationType == OperationType.Remove); 122 | Assert.Equal(original, modified, new GenericDeepEqualityComparer()); 123 | } 124 | 125 | [Fact] 126 | public void ItGeneratesInheritedCorrectPatchDocument() 127 | { 128 | var original = new InheritedClass() 129 | { 130 | Id = "id", 131 | Message = "message", 132 | DecimalValue = 1.43m, 133 | GuidValue = Guid.Empty, 134 | IntList = new List() { 1, 2, 3 }, 135 | ExtraIntValue = 23 136 | }; 137 | var modified = new InheritedClass() 138 | { 139 | Id = "new-id", 140 | Message = null, 141 | DecimalValue = 1.68m, 142 | GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"), 143 | IntList = new List() { 1, 3, 2 }, 144 | ExtraIntValue = 34 145 | }; 146 | 147 | var generator = new JsonPatchDocumentGenerator(); 148 | var patch = generator.Generate(original, modified); 149 | 150 | // Modify original with patch. 151 | patch.ApplyTo(original); 152 | 153 | Assert.NotNull(patch); 154 | Assert.Equal(6, patch.Operations.Count); 155 | Assert.Equal(original, modified, new GenericDeepEqualityComparer()); 156 | } 157 | 158 | [Fact] 159 | public void ItGeneratesNestedCorrectPatchDocument() 160 | { 161 | var original = new NestedTestClass 162 | { 163 | Id = "id", 164 | Message = "message", 165 | DecimalValue = 1.43m, 166 | GuidValue = Guid.Empty, 167 | IntList = new List() { 1, 2, 3 }, 168 | NestedClass = new NestedClass { NestedId = "nested-id", NestedIntValue = 465 }, 169 | NestedClassList = new List 170 | { 171 | new NestedClass { NestedId = "1", NestedIntValue = 1 }, 172 | new NestedClass { NestedId = "2", NestedIntValue = 2 }, 173 | new NestedClass { NestedId = "3", NestedIntValue = 3 } 174 | } 175 | }; 176 | var modified = new NestedTestClass 177 | { 178 | Id = "new-id", 179 | Message = "new-message", 180 | DecimalValue = 1.40m, 181 | GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"), 182 | IntList = new List() { 1, 2, 3, 4 }, 183 | NestedClass = new NestedClass { NestedId = "new-nested-id", NestedIntValue = 465 }, 184 | NestedClassList = new List 185 | { 186 | new NestedClass { NestedId = "1", NestedIntValue = 1 }, 187 | new NestedClass { NestedId = "2", NestedIntValue = 2 }, 188 | new NestedClass { NestedId = "345", NestedIntValue = 345 } 189 | } 190 | }; 191 | 192 | var generator = new JsonPatchDocumentGenerator(); 193 | var patch = generator.Generate(original, modified); 194 | 195 | // Modify original with patch. 196 | patch.ApplyTo(original); 197 | 198 | Assert.NotNull(patch); 199 | Assert.Equal(7, patch.Operations.Count); 200 | Assert.All(patch.Operations, op => Assert.Equal(OperationType.Replace, op.OperationType)); 201 | Assert.Contains(patch.Operations, op => op.path.Equals("/NestedClass/NestedId")); 202 | Assert.Equal(original, modified, new GenericDeepEqualityComparer()); 203 | } 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.Test/Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.Test/TestModels/InheritedClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.TestModels 6 | { 7 | public class InheritedClass : TestClass 8 | { 9 | public int ExtraIntValue { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.Test/TestModels/NestedClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.TestModels 6 | { 7 | public class NestedClass : IEquatable 8 | { 9 | public string NestedId { get; set; } 10 | public int NestedIntValue { get; set; } 11 | 12 | public override bool Equals(object obj) 13 | { 14 | if (!(obj is NestedClass castedObj)) 15 | return false; 16 | 17 | return Equals(castedObj); 18 | } 19 | 20 | public bool Equals(NestedClass other) 21 | { 22 | if (other == null) 23 | return false; 24 | 25 | return 26 | NestedId == other.NestedId 27 | && NestedIntValue == other.NestedIntValue; 28 | } 29 | 30 | public override int GetHashCode() 31 | { 32 | unchecked 33 | { 34 | int hash = 13; 35 | hash = (hash * 7) + NestedId.GetHashCode(); 36 | hash = (hash * 7) + NestedIntValue.GetHashCode(); 37 | return hash; 38 | } 39 | } 40 | } 41 | 42 | public class NestedTestClass : TestClass 43 | { 44 | public List NestedClassList { get; set; } 45 | public NestedClass NestedClass { get; set; } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.Test/TestModels/TestClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.TestModels 6 | { 7 | public class TestClass 8 | { 9 | public string Id { get; set; } 10 | public string Message { get; set; } 11 | public List IntList { get; set; } 12 | public decimal DecimalValue { get; set; } 13 | public Guid GuidValue { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27703.2026 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Palit.AspNetCore.JsonPatch.Extensions.Generate", "Palit.AspNetCore.JsonPatch.Extensions.Generate\Palit.AspNetCore.JsonPatch.Extensions.Generate.csproj", "{2210F51F-349B-4AB5-8991-2B24EF39C4CC}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Palit.AspNetCore.JsonPatch.Extensions.Generate.Test", "Palit.AspNetCore.JsonPatch.Extensions.Generate.Test\Palit.AspNetCore.JsonPatch.Extensions.Generate.Test.csproj", "{C258EBB3-6DE9-43F0-A122-B4DF8AA4BCBF}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {2210F51F-349B-4AB5-8991-2B24EF39C4CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {2210F51F-349B-4AB5-8991-2B24EF39C4CC}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {2210F51F-349B-4AB5-8991-2B24EF39C4CC}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {2210F51F-349B-4AB5-8991-2B24EF39C4CC}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {C258EBB3-6DE9-43F0-A122-B4DF8AA4BCBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {C258EBB3-6DE9-43F0-A122-B4DF8AA4BCBF}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {C258EBB3-6DE9-43F0-A122-B4DF8AA4BCBF}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {C258EBB3-6DE9-43F0-A122-B4DF8AA4BCBF}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {CE901B93-9ED0-43E0-A9B9-050CDDA3F12B} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate/JsonPatchDocumentDiffObserver.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.JsonPatch; 2 | using Newtonsoft.Json; 3 | 4 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate 5 | { 6 | /// 7 | /// Observer that tracks changes to an instance and can generate a JsonPatchDocument. 8 | /// 9 | /// 10 | public class JsonPatchDocumentDiffObserver where T : class 11 | { 12 | 13 | /// 14 | /// The watched instance 15 | /// 16 | private readonly T _watchedInstance; 17 | 18 | /// 19 | /// The original clone 20 | /// 21 | private readonly T _originalClone; 22 | 23 | /// 24 | /// The generator 25 | /// 26 | private readonly JsonPatchDocumentGenerator _generator = new JsonPatchDocumentGenerator(); 27 | 28 | /// 29 | /// Initializes a new instance of the class. 30 | /// 31 | /// The watchedInstance 32 | public JsonPatchDocumentDiffObserver(T watchedInstance) 33 | { 34 | _watchedInstance = watchedInstance; 35 | if (watchedInstance == null) 36 | { 37 | _originalClone = default(T); 38 | } 39 | else 40 | { 41 | _originalClone = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(_watchedInstance), new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace }); 42 | } 43 | } 44 | 45 | /// 46 | /// Generates the patch document. 47 | /// 48 | /// The 49 | public JsonPatchDocument Generate() 50 | { 51 | return _generator.Generate(_originalClone, _watchedInstance); 52 | } 53 | 54 | /// 55 | /// Generates the patch document using the specified json serializer. 56 | /// 57 | /// The json serializer. 58 | /// The 59 | public JsonPatchDocument Generate(JsonSerializer jsonSerializer) 60 | { 61 | return _generator.Generate(_originalClone, _watchedInstance, jsonSerializer); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate/JsonPatchDocumentGenerator.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.JsonPatch; 2 | using Newtonsoft.Json; 3 | using Newtonsoft.Json.Linq; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | 7 | namespace Palit.AspNetCore.JsonPatch.Extensions.Generate 8 | { 9 | /// 10 | /// Defines the 11 | /// 12 | public class JsonPatchDocumentGenerator 13 | { 14 | /// 15 | /// Generates a JsonPatchDocument by comparing two objects. 16 | /// 17 | /// 18 | /// The original object 19 | /// The modified object 20 | /// 21 | /// The 22 | /// 23 | public JsonPatchDocument Generate(T a, T b) where T : class 24 | { 25 | return GeneratePrivate(a, b); 26 | } 27 | 28 | /// 29 | /// Generates a JsonPatchDocument by comparing two objects using the input JsonSerializer. 30 | /// 31 | /// 32 | /// The original object 33 | /// The modified object 34 | /// The jsonSerializer 35 | /// 36 | /// The 37 | /// 38 | public JsonPatchDocument Generate(T a, T b, JsonSerializer jsonSerializer) where T : class 39 | { 40 | return GeneratePrivate(a, b, jsonSerializer); 41 | } 42 | 43 | /// 44 | /// Generates a JsonPatchDocument by comparing two objects. 45 | /// 46 | /// 47 | /// The original object 48 | /// The modified object 49 | /// The jsonSerializer 50 | /// The 51 | private JsonPatchDocument GeneratePrivate(T a, T b, JsonSerializer jsonSerializer = null) where T : class 52 | { 53 | var output = new JsonPatchDocument(); 54 | if (ReferenceEquals(a, b)) 55 | { 56 | return output; 57 | } 58 | 59 | if (null == jsonSerializer) 60 | { 61 | jsonSerializer = JsonSerializer.CreateDefault(); 62 | } 63 | 64 | var originalJson = JObject.FromObject(a, jsonSerializer); 65 | var modifiedJson = JObject.FromObject(b, jsonSerializer); 66 | 67 | FillJsonPatchValues(originalJson, modifiedJson, output); 68 | 69 | return output; 70 | } 71 | 72 | /// 73 | /// Fills the json patch values. 74 | /// 75 | /// The original json. 76 | /// The modified json. 77 | /// The patch. 78 | /// The current path. 79 | private static void FillJsonPatchValues(JObject originalJson, JObject modifiedJson, JsonPatchDocument patch, string currentPath = "/") 80 | { 81 | var originalPropertyNames = new HashSet(originalJson.Properties().Select(p => p.Name)); 82 | var modifiedPropertyNames = new HashSet(modifiedJson.Properties().Select(p => p.Name)); 83 | 84 | // Remove properties not in modified. 85 | foreach (var propName in originalPropertyNames.Except(modifiedPropertyNames)) 86 | { 87 | var prop = originalJson.Property(propName); 88 | patch.Remove(currentPath + prop.Name); 89 | } 90 | 91 | // Add properties not in original 92 | foreach (var propName in modifiedPropertyNames.Except(originalPropertyNames)) 93 | { 94 | var prop = modifiedJson.Property(propName); 95 | patch.Add(currentPath + prop.Name, prop.Value); 96 | } 97 | 98 | // Modify properties that exist in both. 99 | foreach (var propName in originalPropertyNames.Intersect(modifiedPropertyNames)) 100 | { 101 | var originalProp = originalJson.Property(propName); 102 | var modifiedProp = modifiedJson.Property(propName); 103 | 104 | if (originalProp.Value.Type != modifiedProp.Value.Type) 105 | { 106 | patch.Replace(currentPath + propName, modifiedProp.Value); 107 | } 108 | else if (!string.Equals(originalProp.Value.ToString(Formatting.None), modifiedProp.Value.ToString(Formatting.None))) 109 | { 110 | if (originalProp.Value.Type == JTokenType.Object) 111 | { 112 | // Recursively fill nested objects. 113 | FillJsonPatchValues(originalProp.Value as JObject, modifiedProp.Value as JObject, patch, $"{currentPath}{propName}/"); 114 | } 115 | else 116 | { 117 | // Simple Replace otherwise to make patches idempotent. 118 | patch.Replace(currentPath + propName, modifiedProp.Value); 119 | } 120 | } 121 | } 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /Palit.AspNetCore.JsonPatch.Extensions.Generate/Palit.AspNetCore.JsonPatch.Extensions.Generate.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0 5 | Morgan Abel 6 | Palit, LLC 7 | 8 | HttpPatch JsonPatch Json Patch Generator Builder 9 | Builds on Microsoft.AspNetCore.JsonPatch to generate JSON patch documents by comparing or observing changes to C# objects. 10 | https://github.com/morganabel/CSharpJsonPatchGenerator 11 | https://github.com/morganabel/CSharpJsonPatchGenerator 12 | true 13 | https://github.com/morganabel/CSharpJsonPatchGenerator/blob/master/LICENSE 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JsonPatch Generate Extension 2 | 3 | Builds on Microsoft.AspNetCore.JsonPatch to generate JSON patch documents by comparing or observing changes to C# objects. 4 | 5 | - Generate JSON patch by comparing two objects 6 | - Watch an instance of an object for changes and generate a JSON patch representing those changes 7 | 8 | ## Installation 9 | 10 | From the package manager console: 11 | 12 | PM> Install-Package Palit.AspNetCore.JsonPatch.Extensions.Generate 13 | 14 | or by simply searching for `Palit.AspNetCore.JsonPatch.Extensions.Generate` in the package manager UI. 15 | 16 | ## How it's used 17 | 18 | Use this library if you want to generate JSON patch documents from C#. I couldn't find a library for this already and it is not built in so I wrote one myself. It can generate the patch document by comparing two objects or by watching changes to an instance. 19 | 20 | ## Gotchas 21 | 22 | This library is currently fairly simple. It will not necessarily generate optimal JSON patch documents. Instead, it generates patch documents containing Add, Remove and Replace commands exclusively. In the future it would be nice to work in all the different operations in a more optimal way. 23 | 24 | Internally, this library relies on JSON.net serializer pretty heavily. You can pass in custom serializer settings, but definitely do thorough testing with your unique circumstances. The current tests are pretty simple and do not cover complex situations (like deep nesting). 25 | 26 | ## How-To generate a JSON patch by comparison 27 | 28 | ```csharp 29 | var original = new TestClass() 30 | { 31 | Id = "id", 32 | Message = "message", 33 | DecimalValue = 1.43m, 34 | GuidValue = Guid.Empty, 35 | IntList = new List() { 1, 2, 3 } 36 | }; 37 | var modified = new TestClass() 38 | { 39 | Id = "new-id", 40 | Message = null, 41 | DecimalValue = 1.68m, 42 | GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"), 43 | IntList = new List() { 1, 3, 2 } 44 | }; 45 | 46 | var generator = new JsonPatchDocumentGenerator(); 47 | var patch = generator.Generate(original, modified); 48 | 49 | // Modify original with patch. 50 | patch.ApplyTo(original); 51 | 52 | Assert.NotNull(patch); 53 | Assert.Equal(5, patch.Operations.Count); 54 | Assert.Equal(original, modified, new GenericDeepEqualityComparer()); 55 | ``` 56 | 57 | Alternatively, you can pass in your own custom JsonSerializer like so: 58 | ```csharp 59 | var jsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore }; 60 | var patch = generator.Generate(original, modified, jsonSerializer); 61 | ``` 62 | 63 | ## How to generate a patch document with an observer 64 | 65 | ```csharp 66 | var instance = new TestClass 67 | { 68 | Id = "id", 69 | Message = "message", 70 | GuidValue = Guid.Empty, 71 | DecimalValue = 1.23m, 72 | IntList = new List { 1, 2, 3 } 73 | }; 74 | 75 | var observer = new JsonPatchDocumentDiffObserver(instance); 76 | instance.Id = "new-id"; 77 | instance.Message = "new-message"; 78 | instance.GuidValue = Guid.Parse("64362fd9-a24a-4b4b-97cd-8ba9df24a1b5"); 79 | instance.DecimalValue = 1.89m; 80 | instance.IntList = new List { 3, 2, 1 }; 81 | 82 | // Point in time snapshot of the changes. 83 | var patch = observer.Generate(); 84 | 85 | Assert.NotNull(patch); 86 | Assert.Equal(5, patch.Operations.Count); 87 | ``` 88 | 89 | License 90 | ---- 91 | 92 | MIT 93 | 94 | --------------------------------------------------------------------------------