This is an implementation of RFC 6902 JSON Patch written exclusively in Kotlin.
It is based on the Apache 2.0 licensed library from Flipkart, zjsonpatch.
This project is a fork of KJsonPatch (with the latest commit referenced).
This code has been modified from the original library in the following ways:
- Ported from Java to Kotlin
- Changed package names
- Substituted Gson dependency with
kotlinx.serialization.json
- Added extensions for convenient usage of
kotlinx.serialization.json
Add the dependency to your app module’s build.gradle
file:
repositories {
mavenCentral()
}
dependencies {
// e.g., implementation("io.github.reidsync:kotlin-json-patch:1.0.0")
implementation("io.github.reidsync:kotlin-json-patch:${kotliln_json_patch_version}")
}
Check the kotlin-json-patch versions
latest version :
You can add the dependency to sourceSets.commonMain.dependecies
for your Kotlin Multiplatform project.
kotlin {
sourceSets {
commonMain.dependencies {
//put your multiplatform dependencies here
implementation("io.github.reidsync:kotlin-json-patch:${kotliln_json_patch_version}")
}
}
}
The variables
source
,target
, andpatch
below must be asserted as validJsonElement
objects.
val diff: JsonArray = JsonDiff.asJson(source: JsonElement, target: JsonElement)
or
val diff: JsonElement = source.generatePatch(with: target)
val result: JsonElement = JsonPatch.apply(patch: JsonElement, source: JsonElement)
or
val result: JsonElement = source.apply(patch: patch)
This operation is performed on a clone of the source object.
These changes mostly involve porting from Java to Kotlin to transform it into a pure Kotlin library that can be imported into Kotlin Multiplatform. If you have any specific preferences or further adjustments, feel free to let me know!