GitHub - ReidSync/kotlin-json-patch: This is an implementation of RFC 6902 JSON Patch written exclusively in Kotlin. (original) (raw)
[kotlin-json-patch]
Kotlin JSON Patching Library
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).
Changes
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
Setup
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}") } } }
API Usage
The variables
source
,target
, andpatch
below must be asserted as validJsonElement
objects.
Generating JSON Diff as a patch
val diff: JsonArray = JsonDiff.asJson(source: JsonElement, target: JsonElement)
or
val diff: JsonElement = source.generatePatch(with: target)
Applying JSON patch
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!