Giter VIP home page Giter VIP logo

clj-json-pointer's Introduction

cljs-json-pointer

Clojars Project cljdoc build codecov

Simple Clojure(Script) library for working with JSON Pointer and JSON Patch, with no external dependencies. The JSON Patch function provided passes all the tests from the JSON patch conformance test suite.

Usage instructions

At the heart of the library is the ->vec function, which may be used to transform a JSON pointer into a vector representing the path of an object or array. This vector is suitable for use with the standard Clojure functions for nested access or updates, like get-in, assoc-in and update-in:

(ns app
  (:require [clj-json-pointer.core :as jp]))

(def org
  {"department"
   {"tech"
    {"users"
     [{"name" "ted"  "roles" ["developer"]}
      {"name" "jane" "roles" ["platform" "devops"]}]}
    "finance"
    {"users"
     [{"name" "joe"  "roles" ["reports-writer"]}]}}})

(let [path (jp/->vec org "/department/tech/users/1/roles") ; => ["department" "tech" 1 "users" "roles"]
      roles (get-in org path)]                             ; => ["platform" "devops"]
  (do (something (with roles))))

These simple building blocks are used to implement the various operations of JSON patch:

(jp/patch {}                                        ; => {}
  [{"op" "add" "path" "/foo" "value" "bar"}         ; => {"foo" "bar"}
   {"op" "add" "path" "/bar" "value" "baz"}         ; => {"foo" "bar" "bar" "baz}
   {"op" "remove" "path" "/foo"}                    ; => {"bar" "baz"}
   {"op" "replace" "path" "/bar" "value" "foo"}     ; => {"bar" "foo"}          
   {"op" "copy" "from" "/bar" "path" "/baz"}        ; => {"bar" "foo" "baz" "foo"}                
   {"op" "move" "from" "/baz" "path" "/foo"}        ; => {"foo" "foo"}
   {"op" "test" "path" "/foo" "value" "foo"}])      ; => {"foo" "foo"}

Or if you so prefer, use the apply-patch function, which applies a single patch to the provided data structure:

(jp/apply-patch {} {"op" "add" "path" "/a" "value" 1}) ; => {"a" 1}

; or, more likely:
(reduce jp/apply-patch {} patches)

Development

Test

  • clj -X:test to run the unit and compliance tests
  • shadow-cljs compile test && node target/cljs-test.js for ClojureScript

Deploy

  • clj -T:build jar
  • clj -X:deploy

clj-json-pointer's People

Contributors

anderseknert avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.