Skip to content

This is an implementation of RFC 6902 JSON Patch written exclusively in Kotlin.

License

Notifications You must be signed in to change notification settings

ReidSync/kotlin-json-patch

Repository files navigation

[kotlin-json-patch]

CircleCI CircleCI Kotlin Apache License Maven Central

badge-support-kotlin-multiplatform badge-support-android-native badge-support-apple-silicon API
badge-platform-android badge-platform-ios badge-platform-jvm badge-platform-js

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:

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 : Maven Central

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, and patch below must be asserted as valid JsonElement 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!

About

This is an implementation of RFC 6902 JSON Patch written exclusively in Kotlin.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages