├── .github ├── dependabot.yml └── workflows │ ├── pr-workflow.yaml │ ├── publish-workflow.yaml │ └── push-workflow.yaml ├── .gitignore ├── ASL-2.0.txt ├── BUILD.md ├── CONTRIBUTORS.md ├── LGPL-3.0.txt ├── LICENSE ├── README.md ├── RELEASE-NOTES.md ├── build.gradle ├── dorelease.sh ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── misc ├── rfc6902.txt └── rfc7386.txt ├── project.gradle ├── settings.gradle └── src ├── main ├── java │ └── com │ │ └── gravity9 │ │ └── jsonpatch │ │ ├── AddOperation.java │ │ ├── CopyOperation.java │ │ ├── DualPathOperation.java │ │ ├── Iterables.java │ │ ├── JsonPatch.java │ │ ├── JsonPatchException.java │ │ ├── JsonPatchMessages.java │ │ ├── JsonPatchOperation.java │ │ ├── JsonPathParser.java │ │ ├── MoveOperation.java │ │ ├── Patch.java │ │ ├── PathDetails.java │ │ ├── PathParser.java │ │ ├── PathValueOperation.java │ │ ├── RemoveOperation.java │ │ ├── ReplaceOperation.java │ │ ├── TestOperation.java │ │ ├── diff │ │ ├── DiffOperation.java │ │ ├── DiffProcessor.java │ │ ├── JsonDiff.java │ │ └── package-info.java │ │ ├── jackson │ │ └── JsonNumEquals.java │ │ ├── mergepatch │ │ ├── JsonMergePatch.java │ │ ├── JsonMergePatchDeserializer.java │ │ ├── NonObjectMergePatch.java │ │ ├── ObjectMergePatch.java │ │ └── package-info.java │ │ └── package-info.java └── resources │ ├── META-INF │ ├── ASL-2.0.txt │ ├── LGPL-3.0.txt │ └── LICENSE │ └── com │ └── gravity9 │ └── jsonpatch │ └── messages.properties └── test ├── java └── com │ └── gravity9 │ └── jsonpatch │ ├── AddOperationTest.java │ ├── CopyOperationTest.java │ ├── JsonPatchOperationTest.java │ ├── JsonPatchTest.java │ ├── JsonPatchTests.java │ ├── JsonPathParserTest.java │ ├── MoveOperationTest.java │ ├── PathParserTest.java │ ├── RemoveOperationTest.java │ ├── ReplaceOperationTest.java │ ├── TestOperationTest.java │ ├── diff │ ├── JsonDiffTest.java │ └── UnchangedTest.java │ ├── mergepatch │ ├── NonObjectMergePatchTest.java │ ├── ObjectMergePatchTest.java │ └── SerializationTest.java │ ├── model │ ├── DataTypesTest.java │ ├── EmbeddedModel.java │ └── SimpleModel.java │ └── serialization │ ├── AddOperationSerializationTest.java │ ├── CopyOperationSerializationTest.java │ ├── JsonPatchOperationSerializationTest.java │ ├── MoveOperationSerializationTest.java │ ├── RemoveOperationSerializationTest.java │ ├── ReplaceOperationSerializationTest.java │ └── TestOperationSerializationTest.java └── resources └── jsonpatch ├── add.json ├── copy.json ├── diff ├── diff.json └── unchanged.json ├── mergepatch ├── patch-nonobject.json ├── patch-object.json ├── serdeser-nonobject.json └── serdeser-object.json ├── move.json ├── remove.json ├── replace.json ├── test.json └── testsuite.json /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "gradle" 4 | directory: "/" 5 | schedule: 6 | interval: "daily" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "daily" 11 | -------------------------------------------------------------------------------- /.github/workflows/pr-workflow.yaml: -------------------------------------------------------------------------------- 1 | name: Measure coverage 2 | 3 | on: 4 | pull_request: 5 | 6 | jobs: 7 | build: 8 | runs-on: ubuntu-latest 9 | permissions: 10 | pull-requests: write 11 | steps: 12 | - uses: actions/checkout@v3 13 | - name: Set up JDK 11 14 | uses: actions/setup-java@v3 15 | with: 16 | java-version: '11' 17 | distribution: 'zulu' 18 | - name: Run Coverage 19 | run: | 20 | chmod +x gradlew 21 | ./gradlew jacocoTestReport 22 | 23 | - name: Add coverage to PR 24 | id: jacoco 25 | uses: madrapps/jacoco-report@v1.6.1 26 | with: 27 | paths: | 28 | ${{ github.workspace }}/**/build/reports/jacoco/**/jacocoTestReport.xml 29 | token: ${{ secrets.GITHUB_TOKEN }} 30 | min-coverage-overall: 40 31 | min-coverage-changed-files: 60 32 | title: Code Coverage 33 | update-comment: true 34 | 35 | - name: Fail PR if overall coverage is less than 80% 36 | if: ${{ steps.jacoco.outputs.coverage-overall < 80.0 }} 37 | uses: actions/github-script@v6 38 | with: 39 | script: | 40 | core.setFailed('Overall coverage is less than 80%!') -------------------------------------------------------------------------------- /.github/workflows/publish-workflow.yaml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | 6 | # GitHub recommends pinning actions to a commit SHA. 7 | # To get a newer version, you will need to update the SHA. 8 | # You can also reference a tag or branch, but the action may change without warning. 9 | 10 | name: Publish package to the Staging Maven Central Repository 11 | on: 12 | release: 13 | types: [created] 14 | jobs: 15 | publish: 16 | runs-on: ubuntu-latest 17 | steps: 18 | - uses: actions/checkout@v4 19 | - name: Set up Java 20 | uses: actions/setup-java@v3 21 | with: 22 | java-version: '11' 23 | distribution: 'temurin' 24 | 25 | - name: Setup Gradle 26 | uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0 27 | 28 | - name: Publish package 29 | run: ./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository 30 | env: 31 | OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} 32 | OSSRH_PASSWORD: ${{ secrets.OSSRH_TOKEN }} 33 | SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }} 34 | SIGNING_KEY: ${{ secrets.SIGNING_KEY }} 35 | SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }} -------------------------------------------------------------------------------- /.github/workflows/push-workflow.yaml: -------------------------------------------------------------------------------- 1 | # This workflow uses actions that are not certified by GitHub. 2 | # They are provided by a third-party and are governed by 3 | # separate terms of service, privacy policy, and support 4 | # documentation. 5 | 6 | # GitHub recommends pinning actions to a commit SHA. 7 | # To get a newer version, you will need to update the SHA. 8 | # You can also reference a tag or branch, but the action may change without warning. 9 | 10 | name: Java CI 11 | 12 | on: [push] 13 | 14 | jobs: 15 | build: 16 | runs-on: ubuntu-latest 17 | 18 | steps: 19 | - uses: actions/checkout@v3 20 | - name: Set up JDK 11 21 | uses: actions/setup-java@v3 22 | with: 23 | java-version: '11' 24 | distribution: 'zulu' 25 | - name: Validate Gradle wrapper 26 | uses: gradle/wrapper-validation-action@v1 27 | - name: Build with Gradle 28 | uses: gradle/gradle-build-action@v2 29 | with: 30 | arguments: build 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | target 3 | .idea 4 | META-INF 5 | out 6 | build 7 | .gradle 8 | .sdkmanrc 9 | -------------------------------------------------------------------------------- /ASL-2.0.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /BUILD.md: -------------------------------------------------------------------------------- 1 | ## Preamble 2 | 3 | All instructions in this file use the Linux (or other Unix) conventions for 4 | build. If you happen to use Windows, replace `./gradlew` with `gradlew.bat`. 5 | 6 | ## Building instructions 7 | 8 | ### Gradle usage 9 | 10 | You may be fortunate enough that your IDE has Gradle support. Should it not 11 | be the case, first report a bug to your vendor; then refer to the cheat sheet 12 | below: 13 | 14 | ``` 15 | # List the list of tasks 16 | ./gradlew tasks 17 | # Build, test the package 18 | ./gradlew test 19 | # Install in your local maven repository 20 | ./gradlew build publishToMavenLocal 21 | ``` 22 | 23 | If you try and play around with Gradle configuration files, in order to be 24 | _really sure_ that your modifications are accounted for, add the 25 | `--recompile-scripts` option before the task name; for instance: 26 | 27 | ``` 28 | ./gradlew --recompile-scripts test 29 | ``` 30 | 31 | ## Note to Maven users 32 | 33 | There exists a possiblity to generate a `pom.xml` (using `./gradlew pom`), which 34 | is there for convenience. However, this is not supported by the author. 35 | 36 | -------------------------------------------------------------------------------- /CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | ## Ryan Lopopolo (https://github.com/lopopolo) 2 | 3 | * Fix bug in JSON Patch add where target path is not a container node 4 | 5 | ## Randal Watler (watler@wispertel.net) 6 | 7 | * "JSON Diff" 8 | 9 | -------------------------------------------------------------------------------- /LGPL-3.0.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | 167 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This software is dual-licensed under: 2 | 3 | - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 4 | later version; 5 | - the Apache Software License (ASL) version 2.0. 6 | 7 | The text of both licenses is included (under the names LGPL-3.0.txt and 8 | ASL-2.0.txt respectively). 9 | 10 | Direct link to the sources: 11 | 12 | - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 13 | - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 14 | 15 | -------------------------------------------------------------------------------- /RELEASE-NOTES.md: -------------------------------------------------------------------------------- 1 | ## 2.0.2 2 | 3 | * Fix bug with wrong parsing tokens containing numbers detected by @laurent-gougeon. 4 | See [issue 61](https://github.com/gravity9-tech/json-patch-path/issues/61). 5 | 6 | ## 2.0.1 7 | 8 | * Added public access to JsonPathParser 9 | 10 | ## 2.0.0 11 | 12 | The project has been taken over by [gravity9](https://www.gravity9.com). 13 | 14 | * Changed groupId and artifactId 15 | * The library now uses Java 11 as base 16 | * Added support for JSON Path 17 | * Added support for ignoring fields in JSON diff 18 | * Added support for defining a custom ObjectMapper for JsonMergePatch 19 | * Added more context to JsonPatchException thrown in all operations 20 | * Added more test cases and examples 21 | * Upgraded versions of most libraries and tools used in the project 22 | * Fixed outstanding CVE vulnerabilities where possible 23 | * Multiple bugfixes 24 | 25 | ## 1.10 26 | 27 | * First release at java-json-tools. 28 | * Update Gradle to 3.5. 29 | 30 | ## 1.9 31 | 32 | * Completely new JSON diff implementation; less smart than the previous one but 33 | bug free 34 | * Depend on AssertJ. 35 | 36 | ## 1.8 37 | 38 | * JSON Merge Patch is now RFC 7386 compliant. 39 | * Merge gradle files; use Spring's propdeps plugin. 40 | * Fix issue #12: name and description now appear in generated site pom. 41 | 42 | ## 1.7 43 | 44 | * Fix bug with diffs and multiple array removals; detected by @royclarkson, fixed by 45 | @rwatler. See [issue 11](https://github.com/fge/json-patch/issues/11). 46 | 47 | ## 1.6 48 | 49 | * Update jackson-coreutils dependency. 50 | * Change license file placement/text. 51 | * Make all tests run from the command line. 52 | * Disable propdeps plugin for the moment. 53 | 54 | ## 1.5 55 | 56 | * Full Jackson serialization/deserialization support. 57 | * JSON merge-patch support. 58 | (http://tools.ietf.org/html/draft-ietf-appsawg-json-merge-patch-02) 59 | * Fix bug in add operation where the parent node of the path to add to was not 60 | a container node. 61 | * Update to gradle 1.11. 62 | 63 | ## 1.4 64 | 65 | * Use gradle for build 66 | * Many backwards-compatible code changes to diff code 67 | * Use msg-simple 68 | * Update TestNG dependency 69 | * Update jackson-coreutils dependency; use new method for "split pointers" 70 | 71 | 72 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | buildscript { 21 | repositories { 22 | mavenCentral() 23 | } 24 | dependencies { 25 | classpath 'biz.aQute.bnd:biz.aQute.bnd.gradle:4.2.0' 26 | classpath 'org.owasp:dependency-check-gradle:8.2.1' 27 | } 28 | }; 29 | 30 | plugins { 31 | id("net.ltgt.errorprone") version "3.0.1" apply false 32 | id "org.sonarqube" version "4.2.1.3168" 33 | id "jacoco" 34 | id "io.github.gradle-nexus.publish-plugin" version "2.0.0-rc-2" 35 | } 36 | 37 | apply(plugin: "java-library"); 38 | apply(plugin: "maven-publish"); 39 | apply(plugin: "signing"); 40 | apply(plugin: "biz.aQute.bnd.builder"); 41 | apply(plugin: "net.ltgt.errorprone"); 42 | apply(plugin: 'org.owasp.dependencycheck'); 43 | apply(plugin: 'java-library') 44 | apply(from: "project.gradle"); 45 | 46 | group = "com.gravity9"; 47 | 48 | /* 49 | * Repositories to use 50 | */ 51 | repositories { 52 | mavenCentral(); 53 | } 54 | 55 | /* 56 | * Add errorprone checking. 57 | */ 58 | dependencies { 59 | errorprone('com.google.errorprone:error_prone_core:2.20.0') 60 | } 61 | 62 | /* 63 | * Necessary! Otherwise TestNG will not be used... 64 | * 65 | */ 66 | test { 67 | useTestNG() { 68 | useDefaultListeners = true; 69 | }; 70 | finalizedBy jacocoTestReport // report is always generated after tests run 71 | } 72 | 73 | jacocoTestReport { 74 | dependsOn test // tests are required to run before generating the report 75 | } 76 | 77 | /* 78 | * Necessary to generate the source and javadoc jars 79 | */ 80 | task sourcesJar(type: Jar, dependsOn: classes) { 81 | from sourceSets.main.allSource; 82 | archiveClassifier.set("sources"); 83 | } 84 | 85 | /* 86 | * Lint all the things! 87 | */ 88 | allprojects { 89 | gradle.projectsEvaluated { 90 | tasks.withType(JavaCompile) { 91 | options.compilerArgs << "-Xlint:all" << "-Xlint:-serial" << "-Werror" 92 | } 93 | tasks.withType(Javadoc) { 94 | options.addStringOption('Xwerror', '-quiet') 95 | } 96 | } 97 | } 98 | 99 | task javadocJar(type: Jar, dependsOn: javadoc) { 100 | from javadoc.destinationDir; 101 | archiveClassifier.set("javadoc"); 102 | } 103 | 104 | artifacts { 105 | archives jar; 106 | archives sourcesJar; 107 | archives javadocJar; 108 | } 109 | 110 | jacocoTestReport{ 111 | reports { 112 | xml.enabled true 113 | html.enabled true 114 | } 115 | } 116 | 117 | wrapper { 118 | gradleVersion = "7.6.1"; 119 | distributionUrl = "https://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"; 120 | } 121 | 122 | java { 123 | withJavadocJar() 124 | withSourcesJar() 125 | } 126 | 127 | publishing { 128 | publications { 129 | mavenJava(MavenPublication) { 130 | artifactId = 'json-patch-path' 131 | from components.java 132 | pom { 133 | name = 'json-patch-path' 134 | description = 'JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7386) implementation in Java, using extended TMF620 JsonPath syntax' 135 | url = 'https://github.com/gravity9-tech/json-patch' 136 | 137 | licenses { 138 | license { 139 | name = 'The Apache License, Version 2.0' 140 | url = 'http://www.apache.org/licenses/LICENSE-2.0.txt' 141 | } 142 | } 143 | developers { 144 | developer { 145 | id = 'Piotr-Dytkowski' 146 | name = 'Piotr Dytkowski' 147 | email = 'piotr.dytkowski@gravity9.com' 148 | organization = 'gravity9' 149 | organizationUrl = 'https://www.gravity9.com/' 150 | } 151 | developer { 152 | id = 'Bartlomiej-Styczynski' 153 | name = 'Bartlomiej Styczynski' 154 | email = 'bartlomiej.styczynski@gravity9.com' 155 | organization = 'gravity9' 156 | organizationUrl = 'https://www.gravity9.com/' 157 | } 158 | developer { 159 | id = 'Piotr-Bugara' 160 | name = 'Piotr Bugara' 161 | email = 'piotr.bugara@gravity9.com' 162 | organization = 'gravity9' 163 | organizationUrl = 'https://www.gravity9.com/' 164 | } 165 | developer { 166 | id = 'Garry-Newball' 167 | name = 'Garry Newball' 168 | email = 'garry.newball@gravity9.com' 169 | organization = 'gravity9' 170 | organizationUrl = 'https://www.gravity9.com/' 171 | } 172 | developer { 173 | id = 'Mateusz-Zaremba' 174 | name = 'Mateusz Zaremba' 175 | email = 'mateusz.zaremba@gravity9.com' 176 | organization = 'gravity9' 177 | organizationUrl = 'https://www.gravity9.com/' 178 | } 179 | } 180 | scm { 181 | connection = 'scm:git:https://github.com/java-json-tools/json-patch.git' 182 | developerConnection = 'scm:git:ssh://github.com:java-json-tools/json-patch.git' 183 | url = 'https://github.com/gravity9-tech/json-patch' 184 | } 185 | } 186 | } 187 | } 188 | } 189 | 190 | nexusPublishing { 191 | repositories { 192 | sonatype { //only for users registered in Sonatype after 24 Feb 2021 193 | nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/")) 194 | snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")) 195 | username = System.getenv("OSSRH_USERNAME") 196 | password = System.getenv("OSSRH_PASSWORD") 197 | } 198 | } 199 | } 200 | 201 | signing { 202 | required { gradle.taskGraph.hasTask("publish") } 203 | def signingKeyId = System.getenv("SIGNING_KEY_ID") 204 | def signingKey = System.getenv("SIGNING_KEY") 205 | def signingPassword = System.getenv("SIGNING_PASSWORD") 206 | useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword) 207 | sign publishing.publications.mavenJava 208 | } -------------------------------------------------------------------------------- /dorelease.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # 4 | # This will build everything that is needed and push to Maven central. 5 | # 6 | 7 | ./gradlew --refresh-dependencies clean test publish 8 | 9 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | version=2.0.3 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gravity9-tech/json-patch-path/8739ea97b9fafdfa2980290dbe2a5366100eb864/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | # Determine the Java command to use to start the JVM. 86 | if [ -n "$JAVA_HOME" ] ; then 87 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 88 | # IBM's JDK on AIX uses strange locations for the executables 89 | JAVACMD="$JAVA_HOME/jre/sh/java" 90 | else 91 | JAVACMD="$JAVA_HOME/bin/java" 92 | fi 93 | if [ ! -x "$JAVACMD" ] ; then 94 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 95 | 96 | Please set the JAVA_HOME variable in your environment to match the 97 | location of your Java installation." 98 | fi 99 | else 100 | JAVACMD="java" 101 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 102 | 103 | Please set the JAVA_HOME variable in your environment to match the 104 | location of your Java installation." 105 | fi 106 | 107 | # Increase the maximum file descriptors if we can. 108 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 109 | MAX_FD_LIMIT=`ulimit -H -n` 110 | if [ $? -eq 0 ] ; then 111 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 112 | MAX_FD="$MAX_FD_LIMIT" 113 | fi 114 | ulimit -n $MAX_FD 115 | if [ $? -ne 0 ] ; then 116 | warn "Could not set maximum file descriptor limit: $MAX_FD" 117 | fi 118 | else 119 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 120 | fi 121 | fi 122 | 123 | # For Darwin, add options to specify how the application appears in the dock 124 | if $darwin; then 125 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 126 | fi 127 | 128 | # For Cygwin or MSYS, switch paths to Windows format before running java 129 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 130 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 131 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 132 | JAVACMD=`cygpath --unix "$JAVACMD"` 133 | 134 | # We build the pattern for arguments to be converted via cygpath 135 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 136 | SEP="" 137 | for dir in $ROOTDIRSRAW ; do 138 | ROOTDIRS="$ROOTDIRS$SEP$dir" 139 | SEP="|" 140 | done 141 | OURCYGPATTERN="(^($ROOTDIRS))" 142 | # Add a user-defined pattern to the cygpath arguments 143 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 144 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 145 | fi 146 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 147 | i=0 148 | for arg in "$@" ; do 149 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 150 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 151 | 152 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 153 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 154 | else 155 | eval `echo args$i`="\"$arg\"" 156 | fi 157 | i=$((i+1)) 158 | done 159 | case $i in 160 | (0) set -- ;; 161 | (1) set -- "$args0" ;; 162 | (2) set -- "$args0" "$args1" ;; 163 | (3) set -- "$args0" "$args1" "$args2" ;; 164 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 165 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 166 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 167 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 168 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 169 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 170 | esac 171 | fi 172 | 173 | # Escape application args 174 | save () { 175 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 176 | echo " " 177 | } 178 | APP_ARGS=$(save "$@") 179 | 180 | # Collect all arguments for the java command, following the shell quoting and substitution rules 181 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 182 | 183 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 184 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 185 | cd "$(dirname "$0")" 186 | fi 187 | 188 | exec "$JAVACMD" "$@" 189 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 33 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 34 | 35 | @rem Find java.exe 36 | if defined JAVA_HOME goto findJavaFromJavaHome 37 | 38 | set JAVA_EXE=java.exe 39 | %JAVA_EXE% -version >NUL 2>&1 40 | if "%ERRORLEVEL%" == "0" goto init 41 | 42 | echo. 43 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 44 | echo. 45 | echo Please set the JAVA_HOME variable in your environment to match the 46 | echo location of your Java installation. 47 | 48 | goto fail 49 | 50 | :findJavaFromJavaHome 51 | set JAVA_HOME=%JAVA_HOME:"=% 52 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 53 | 54 | if exist "%JAVA_EXE%" goto init 55 | 56 | echo. 57 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 58 | echo. 59 | echo Please set the JAVA_HOME variable in your environment to match the 60 | echo location of your Java installation. 61 | 62 | goto fail 63 | 64 | :init 65 | @rem Get command-line arguments, handling Windows variants 66 | 67 | if not "%OS%" == "Windows_NT" goto win9xME_args 68 | 69 | :win9xME_args 70 | @rem Slurp the command line arguments. 71 | set CMD_LINE_ARGS= 72 | set _SKIP=2 73 | 74 | :win9xME_args_slurp 75 | if "x%~1" == "x" goto execute 76 | 77 | set CMD_LINE_ARGS=%* 78 | 79 | :execute 80 | @rem Setup the command line 81 | 82 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 83 | 84 | @rem Execute Gradle 85 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 86 | 87 | :end 88 | @rem End local scope for the variables with windows NT shell 89 | if "%ERRORLEVEL%"=="0" goto mainEnd 90 | 91 | :fail 92 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 93 | rem the _cmd.exe /c_ return code! 94 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 95 | exit /b 1 96 | 97 | :mainEnd 98 | if "%OS%"=="Windows_NT" endlocal 99 | 100 | :omega 101 | -------------------------------------------------------------------------------- /project.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | /* 21 | * Project-specific settings. Unfortunately we cannot put the name in there! 22 | */ 23 | group = "com.gravity9"; 24 | sourceCompatibility = JavaVersion.VERSION_11 25 | targetCompatibility = JavaVersion.VERSION_11 26 | project.ext.description = "JSON Patch (RFC 6902) and JSON Merge Patch (RFC 7386) implementation in Java, using extended TMF620 JsonPath syntax"; 27 | 28 | /* 29 | * List of dependencies 30 | */ 31 | dependencies { 32 | compileOnly(group: "com.google.code.findbugs", name: "jsr305", version: "3.0.2"); 33 | implementation(group: "com.fasterxml.jackson.core", name: "jackson-databind", version: "2.15.2"); 34 | api(group: 'com.jayway.jsonpath', name: 'json-path', version: '2.9.0') 35 | api(group: "com.github.java-json-tools", name: "msg-simple", version: "1.2"); 36 | api(group: "com.github.java-json-tools", name: "jackson-coreutils", version: "2.0"); 37 | testImplementation(group: "org.testng", name: "testng", version: "7.1.0") { 38 | exclude(group: "junit", module: "junit"); 39 | exclude(group: "org.beanshell", module: "bsh"); 40 | exclude(group: "org.yaml", module: "snakeyaml"); 41 | }; 42 | testImplementation(group: "org.mockito", name: "mockito-core", version: "2.28.2"); 43 | testImplementation(group: "org.assertj", name: "assertj-core", version: "3.24.2"); 44 | testImplementation(group: "com.google.guava", name: "guava", version: "32.0.1-android"); 45 | } 46 | 47 | javadoc.options { 48 | links("https://docs.oracle.com/javase/11/docs/api/"); 49 | links("https://www.javadoc.io/doc/com.google.code.findbugs/jsr305/3.0.2/"); 50 | links("https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-databind/2.15.2/index.html"); 51 | links("https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-core/2.15.2/index.html"); 52 | links("https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-annotations/2.15.2/index.html"); 53 | links("https://www.javadoc.io/doc/com.google.guava/guava/32.0.1-android/"); 54 | links("https://java-json-tools.github.io/msg-simple/"); 55 | links("https://java-json-tools.github.io/jackson-coreutils/"); 56 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | rootProject.name = "json-patch-path"; 21 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/AddOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | package com.gravity9.jsonpatch; 21 | 22 | import com.fasterxml.jackson.annotation.JsonCreator; 23 | import com.fasterxml.jackson.annotation.JsonProperty; 24 | import com.fasterxml.jackson.databind.JsonNode; 25 | import com.fasterxml.jackson.databind.node.ArrayNode; 26 | import com.jayway.jsonpath.DocumentContext; 27 | import com.jayway.jsonpath.JsonPath; 28 | 29 | 30 | /** 31 | * JSON Patch {@code add} operation 32 | * 33 | *

For this operation, {@code path} is the JSON Pointer where the value 34 | * should be added, and {@code value} is the value to add.

35 | * 36 | *

Note that if the target value pointed to by {@code path} already exists, 37 | * it is replaced. In this case, {@code add} is equivalent to {@code replace}. 38 | *

39 | * 40 | *

Note also that a value will be created at the target path if and only 41 | * if the immediate parent of that value exists (and is of the correct 42 | * type).

43 | * 44 | *

Finally, if the last reference token of the JSON Pointer is {@code -} and 45 | * the immediate parent is an array, the given value is added at the end of the 46 | * array. For instance, applying:

47 | * 48 | *
 49 |  *     { "op": "add", "path": "/-", "value": 3 }
 50 |  * 
51 | * 52 | *

to:

53 | * 54 | *
 55 |  *     [ 1, 2 ]
 56 |  * 
57 | * 58 | *

will give:

59 | * 60 | *
 61 |  *     [ 1, 2, 3 ]
 62 |  * 
63 | */ 64 | public final class AddOperation extends PathValueOperation { 65 | 66 | public static final String LAST_ARRAY_ELEMENT_SYMBOL = "-"; 67 | 68 | @JsonCreator 69 | public AddOperation(@JsonProperty("path") final String path, 70 | @JsonProperty("value") final JsonNode value) { 71 | super("add", path, value); 72 | } 73 | 74 | @Override 75 | public JsonNode applyInternal(final JsonNode node) throws JsonPatchException { 76 | if (path.isEmpty()) { 77 | return value; 78 | } 79 | PathDetails pathDetails = PathParser.getParentPathAndNewNodeName(path); 80 | final String pathToParent = pathDetails.getPathToParent(); 81 | final String newNodeName = pathDetails.getNewNodeName(); 82 | 83 | final DocumentContext nodeContext = JsonPath.parse(node.deepCopy()); 84 | final JsonNode evaluatedJsonParents = nodeContext.read(pathToParent); 85 | if (!evaluatedJsonParents.isContainerNode()) { 86 | throw JsonPatchException.parentNotContainer(pathToParent); 87 | } 88 | 89 | if (pathDetails.doesContainFiltersOrMultiIndexesNotation()) { // json filter result is always a list 90 | for (int i = 0; i < evaluatedJsonParents.size(); i++) { 91 | JsonNode parentNode = evaluatedJsonParents.get(i); 92 | if (!parentNode.isContainerNode()) { 93 | throw JsonPatchException.parentNotContainer(pathToParent + " at index " + i); 94 | } 95 | DocumentContext containerContext = JsonPath.parse(parentNode); 96 | if (parentNode.isArray()) { 97 | addToArray(containerContext, "$", newNodeName); 98 | } else { 99 | addToObject(containerContext, "$", newNodeName); 100 | } 101 | } 102 | return nodeContext.read("$"); 103 | } else { 104 | return evaluatedJsonParents.isArray() 105 | ? addToArray(nodeContext, pathToParent, newNodeName) 106 | : addToObject(nodeContext, pathToParent, newNodeName); 107 | } 108 | } 109 | 110 | private JsonNode addToArray(final DocumentContext node, String jsonPath, String newNodeName) throws JsonPatchException { 111 | if (newNodeName.equals(LAST_ARRAY_ELEMENT_SYMBOL)) { 112 | return node.add(jsonPath, value).read("$", JsonNode.class); 113 | } 114 | 115 | final int size = node.read(jsonPath, JsonNode.class).size(); 116 | final int index = verifyAndGetArrayIndex(newNodeName, size); 117 | 118 | ArrayNode updatedArray = node.read(jsonPath, ArrayNode.class).insert(index, value); 119 | return "$".equals(jsonPath) ? updatedArray : node.set(jsonPath, updatedArray).read("$", JsonNode.class); 120 | } 121 | 122 | private JsonNode addToObject(final DocumentContext node, String jsonPath, String newNodeName) { 123 | return node 124 | .put(jsonPath, newNodeName, value) 125 | .read("$", JsonNode.class); 126 | } 127 | 128 | private int verifyAndGetArrayIndex(String stringIndex, int size) throws JsonPatchException { 129 | int index; 130 | try { 131 | index = Integer.parseInt(stringIndex); 132 | } catch (NumberFormatException ignored) { 133 | throw JsonPatchException.notAnIndex(stringIndex); 134 | } 135 | 136 | if (index < 0 || index > size) { 137 | throw JsonPatchException.noSuchIndex(index); 138 | } 139 | 140 | return index; 141 | } 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/CopyOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | package com.gravity9.jsonpatch; 21 | 22 | import com.fasterxml.jackson.annotation.JsonCreator; 23 | import com.fasterxml.jackson.annotation.JsonProperty; 24 | import com.fasterxml.jackson.databind.JsonNode; 25 | import com.jayway.jsonpath.JsonPath; 26 | 27 | /** 28 | * JSON Patch {@code copy} operation 29 | * 30 | *

For this operation, {@code from} is the JSON Pointer of the value to copy, 31 | * and {@code path} is the destination where the value should be copied.

32 | * 33 | *

As for {@code add}:

34 | * 35 | * 40 | * 41 | *

It is an error if {@code from} fails to resolve to a JSON value.

42 | */ 43 | public final class CopyOperation extends DualPathOperation { 44 | 45 | @JsonCreator 46 | public CopyOperation(@JsonProperty("from") final String from, @JsonProperty("path") final String path) { 47 | super("copy", from, path); 48 | } 49 | 50 | @Override 51 | public JsonNode applyInternal(final JsonNode node) throws JsonPatchException { 52 | final String jsonPath = JsonPathParser.parsePathToJsonPath(from); 53 | final JsonNode dupData = JsonPath.parse(node.deepCopy()).read(jsonPath); 54 | return new AddOperation(path, dupData).apply(node); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/DualPathOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | package com.gravity9.jsonpatch; 21 | 22 | import com.fasterxml.jackson.core.JsonGenerator; 23 | import com.fasterxml.jackson.databind.SerializerProvider; 24 | import com.fasterxml.jackson.databind.annotation.JsonSerialize; 25 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 26 | import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; 27 | 28 | import java.io.IOException; 29 | 30 | /** 31 | * Base class for JSON Patch operations taking two JSON Pointers as arguments 32 | */ 33 | public abstract class DualPathOperation extends JsonPatchOperation { 34 | 35 | @JsonSerialize(using = ToStringSerializer.class) 36 | protected final String from; 37 | 38 | /** 39 | * Protected constructor 40 | * 41 | * @param op operation name 42 | * @param from source path 43 | * @param path destination path 44 | */ 45 | protected DualPathOperation(final String op, final String from, final String path) { 46 | super(op, path); 47 | this.from = from; 48 | } 49 | 50 | @Override 51 | public final void serialize(final JsonGenerator jgen, final SerializerProvider provider) throws IOException { 52 | jgen.writeStartObject(); 53 | jgen.writeStringField("op", op); 54 | jgen.writeStringField("path", path); 55 | jgen.writeStringField("from", from); 56 | jgen.writeEndObject(); 57 | } 58 | 59 | @Override 60 | public final void serializeWithType(final JsonGenerator jgen, final SerializerProvider provider, 61 | final TypeSerializer typeSer) throws IOException { 62 | serialize(jgen, provider); 63 | } 64 | 65 | public final String getFrom() { 66 | return from; 67 | } 68 | 69 | @Override 70 | public final String toString() { 71 | return "op: " + op + "; from: \"" + from + "\"; path: \"" + path + '"'; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/Iterables.java: -------------------------------------------------------------------------------- 1 | package com.gravity9.jsonpatch; 2 | 3 | import java.util.Iterator; 4 | import java.util.NoSuchElementException; 5 | 6 | /** 7 | * Iterables utility class 8 | * 9 | * @author {@literal @}soberich on 30-Nov-18 10 | */ 11 | public final class Iterables { 12 | 13 | private Iterables() { 14 | } 15 | 16 | /** 17 | * Returns the last element of {@code iterable}. 18 | * 19 | * @param underlying type being iterated 20 | * @param iterable type of iterable 21 | * @return the last element of {@code iterable} 22 | * @throws NoSuchElementException if the iterable is empty 23 | */ 24 | public static T getLast(Iterable iterable) { 25 | Iterator iterator = iterable.iterator(); 26 | while (true) { 27 | T current = iterator.next(); 28 | if (!iterator.hasNext()) { 29 | return current; 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/JsonPatch.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | package com.gravity9.jsonpatch; 21 | 22 | import com.fasterxml.jackson.annotation.JsonCreator; 23 | import com.fasterxml.jackson.core.JsonGenerator; 24 | import com.fasterxml.jackson.databind.JsonNode; 25 | import com.fasterxml.jackson.databind.JsonSerializable; 26 | import com.fasterxml.jackson.databind.SerializerProvider; 27 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 28 | import com.github.fge.jackson.JacksonUtils; 29 | import com.github.fge.msgsimple.bundle.MessageBundle; 30 | import com.github.fge.msgsimple.load.MessageBundles; 31 | import java.io.IOException; 32 | import java.util.ArrayList; 33 | import java.util.Collections; 34 | import java.util.List; 35 | 36 | /** 37 | * Implementation of JSON Patch 38 | * 39 | *

JSON 40 | * Patch, as its name implies, is an IETF RFC describing a mechanism to 41 | * apply a patch to any JSON value. This implementation covers all operations 42 | * according to the specification; however, there are some subtle differences 43 | * with regards to some operations which are covered in these operations' 44 | * respective documentation.

45 | * 46 | *

An example of a JSON Patch is as follows:

47 | * 48 | *
 49 |  *     [
 50 |  *         {
 51 |  *             "op": "add",
 52 |  *             "path": "/-",
 53 |  *             "value": {
 54 |  *                 "productId": 19,
 55 |  *                 "name": "Duvel",
 56 |  *                 "type": "beer"
 57 |  *             }
 58 |  *         }
 59 |  *     ]
 60 |  * 
61 | * 62 | *

This patch contains a single operation which adds an item at the end of 63 | * an array. A JSON Patch can contain more than one operation; in this case, all 64 | * operations are applied to the input JSON value in their order of appearance, 65 | * until all operations are applied or an error condition is encountered.

66 | * 67 | *

The main point where this implementation differs from the specification 68 | * is initial JSON parsing. The draft says:

69 | * 70 | *
 71 |  *     Operation objects MUST have exactly one "op" member
 72 |  * 
73 | * 74 | *

and:

75 | * 76 | *
 77 |  *     Additionally, operation objects MUST have exactly one "path" member.
 78 |  * 
79 | * 80 | *

However, obeying these to the letter forces constraints on the JSON 81 | * parser. Here, these constraints are not enforced, which means:

82 | * 83 | *
 84 |  *     [ { "op": "add", "op": "remove", "path": "/x" } ]
 85 |  * 
86 | * 87 | *

is parsed (as a {@code remove} operation, since it appears last).

88 | * 89 | *

IMPORTANT NOTE: the JSON Patch is supposed to be VALID when the 90 | * constructor for this class ({@link JsonPatch#fromJson(JsonNode)} is used.

91 | */ 92 | public final class JsonPatch implements JsonSerializable, Patch { 93 | 94 | private static final MessageBundle BUNDLE 95 | = MessageBundles.getBundle(JsonPatchMessages.class); 96 | 97 | /** 98 | * List of operations 99 | */ 100 | private final List operations; 101 | 102 | /** 103 | * Constructor 104 | * 105 | *

Normally, you should never have to use it.

106 | * 107 | * @param operations the list of operations for this patch 108 | * @see JsonPatchOperation 109 | */ 110 | @JsonCreator 111 | public JsonPatch(final List operations) { 112 | this.operations = Collections.unmodifiableList(new ArrayList(operations)); 113 | } 114 | 115 | /** 116 | * Static factory method to build a JSON Patch out of a JSON representation 117 | * 118 | * @param node the JSON representation of the generated JSON Patch 119 | * @return a JSON Patch 120 | * @throws IOException input is not a valid JSON patch 121 | * @throws NullPointerException input is null 122 | */ 123 | public static JsonPatch fromJson(final JsonNode node) 124 | throws IOException { 125 | BUNDLE.checkNotNull(node, "jsonPatch.nullInput"); 126 | return JacksonUtils.getReader().forType(JsonPatch.class) 127 | .readValue(node); 128 | } 129 | 130 | /** 131 | * Apply this patch to a JSON value 132 | * 133 | * @param node the value to apply the patch to 134 | * @return the patched JSON value 135 | * @throws JsonPatchException failed to apply patch 136 | * @throws NullPointerException input is null 137 | */ 138 | @Override 139 | public JsonNode apply(final JsonNode node) 140 | throws JsonPatchException { 141 | BUNDLE.checkNotNull(node, "jsonPatch.nullInput"); 142 | JsonNode ret = node; 143 | for (final JsonPatchOperation operation : operations) 144 | ret = operation.apply(ret); 145 | 146 | return ret; 147 | } 148 | 149 | public final List getOperations() { 150 | return operations; 151 | } 152 | 153 | @Override 154 | public String toString() { 155 | return operations.toString(); 156 | } 157 | 158 | @Override 159 | public void serialize(final JsonGenerator jgen, 160 | final SerializerProvider provider) 161 | throws IOException { 162 | jgen.writeStartArray(); 163 | for (final JsonPatchOperation op : operations) 164 | op.serialize(jgen, provider); 165 | jgen.writeEndArray(); 166 | } 167 | 168 | @Override 169 | public void serializeWithType(final JsonGenerator jgen, 170 | final SerializerProvider provider, final TypeSerializer typeSer) 171 | throws IOException { 172 | serialize(jgen, provider); 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/JsonPatchException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | package com.gravity9.jsonpatch; 21 | 22 | import static com.gravity9.jsonpatch.JsonPatchOperation.BUNDLE; 23 | 24 | public final class JsonPatchException extends Exception { 25 | 26 | public JsonPatchException(final String message) { 27 | super(message); 28 | } 29 | 30 | public JsonPatchException(final String message, final Throwable cause) { 31 | super(message, cause); 32 | } 33 | 34 | public static JsonPatchException valueTestFailure(Object expected, Object found) { 35 | return new JsonPatchException(BUNDLE.getMessage("jsonPatch.valueTestFailure") + 36 | ": expected '" + expected + "' but found '" + found + "'"); 37 | } 38 | 39 | public static JsonPatchException notAnIndex(String index) { 40 | return new JsonPatchException(BUNDLE.getMessage("jsonPatch.notAnIndex") + ": " + index); 41 | } 42 | 43 | public static JsonPatchException noSuchIndex(Integer index) { 44 | return new JsonPatchException(BUNDLE.getMessage("jsonPatch.noSuchIndex") + ": " + index); 45 | } 46 | 47 | public static JsonPatchException parentNotContainer(String path) { 48 | return new JsonPatchException(BUNDLE.getMessage("jsonPatch.parentNotContainer") + ": " + path); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/JsonPatchMessages.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | package com.gravity9.jsonpatch; 21 | 22 | import com.github.fge.msgsimple.bundle.MessageBundle; 23 | import com.github.fge.msgsimple.bundle.PropertiesBundle; 24 | import com.github.fge.msgsimple.load.MessageBundleLoader; 25 | 26 | public final class JsonPatchMessages implements MessageBundleLoader { 27 | 28 | @Override 29 | public MessageBundle getBundle() { 30 | return PropertiesBundle.forPath("/com/gravity9/jsonpatch/messages"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/gravity9/jsonpatch/JsonPatchOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Francis Galiegue (fgaliegue@gmail.com) 3 | * 4 | * This software is dual-licensed under: 5 | * 6 | * - the Lesser General Public License (LGPL) version 3.0 or, at your option, any 7 | * later version; 8 | * - the Apache Software License (ASL) version 2.0. 9 | * 10 | * The text of this file and of both licenses is available at the root of this 11 | * project or, if you have the jar distribution, in directory META-INF/, under 12 | * the names LGPL-3.0.txt and ASL-2.0.txt respectively. 13 | * 14 | * Direct link to the sources: 15 | * 16 | * - LGPL 3.0: https://www.gnu.org/licenses/lgpl-3.0.txt 17 | * - ASL 2.0: http://www.apache.org/licenses/LICENSE-2.0.txt 18 | */ 19 | 20 | package com.gravity9.jsonpatch; 21 | 22 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties; 23 | import com.fasterxml.jackson.annotation.JsonSubTypes; 24 | import com.fasterxml.jackson.annotation.JsonTypeInfo; 25 | import com.fasterxml.jackson.databind.JsonNode; 26 | import com.fasterxml.jackson.databind.JsonSerializable; 27 | import com.github.fge.msgsimple.bundle.MessageBundle; 28 | import com.github.fge.msgsimple.load.MessageBundles; 29 | import com.jayway.jsonpath.Configuration; 30 | import com.jayway.jsonpath.JsonPathException; 31 | import com.jayway.jsonpath.Option; 32 | import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider; 33 | import com.jayway.jsonpath.spi.json.JsonProvider; 34 | import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; 35 | import com.jayway.jsonpath.spi.mapper.MappingProvider; 36 | import java.util.EnumSet; 37 | import java.util.Set; 38 | 39 | import static com.fasterxml.jackson.annotation.JsonSubTypes.Type; 40 | import static com.fasterxml.jackson.annotation.JsonTypeInfo.As; 41 | import static com.fasterxml.jackson.annotation.JsonTypeInfo.Id; 42 | 43 | @JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "op") 44 | 45 | @JsonSubTypes({ 46 | @Type(name = "add", value = AddOperation.class), 47 | @Type(name = "copy", value = CopyOperation.class), 48 | @Type(name = "move", value = MoveOperation.class), 49 | @Type(name = "remove", value = RemoveOperation.class), 50 | @Type(name = "replace", value = ReplaceOperation.class), 51 | @Type(name = "test", value = TestOperation.class) 52 | }) 53 | 54 | /* 55 | * Base abstract class for one patch operation 56 | * 57 | *

Two more abstract classes extend this one according to the arguments of 58 | * the operation:

59 | * 60 | *
    61 | *
  • {@link DualPathOperation} for operations taking a second pointer as 62 | * an argument ({@code copy} and {@code move});
  • 63 | *
  • {@link PathValueOperation} for operations taking a value as an 64 | * argument ({@code add}, {@code replace} and {@code test}).
  • 65 | *
66 | * 67 | */ 68 | @JsonIgnoreProperties(ignoreUnknown = true) 69 | public abstract class JsonPatchOperation implements JsonSerializable { 70 | 71 | protected static final MessageBundle BUNDLE = MessageBundles.getBundle(JsonPatchMessages.class); 72 | 73 | static { 74 | Configuration.setDefaults(new Configuration.Defaults() { 75 | @Override 76 | public JsonProvider jsonProvider() { 77 | return new JacksonJsonNodeJsonProvider(); 78 | } 79 | 80 | @Override 81 | public Set