Skip to content

Clojure Usage

Wagner Leonardi edited this page Oct 11, 2019 · 2 revisions

Although this project is written in Java, it was projected to be easily adopted by any JVM language. You don't need a clojure library or wrapper to work with jsonbox on clojure, just follow the steps:

  1. Set jsonbox library as a dependency in your project. If you are using Leiningen, this goes in your project.clj file:
[io.jsonbox/jsonbox "1.0.3"]
  1. Import jsonbox library in your code namespace:
(:import [io.jsonbox JsonBoxStorage])
  1. Create a variable for jsonbox instance:
(def jsonbox-storage (JsonBoxStorage. "clojurebox0000000000"))
  1. Now you can use any function from the documentation, example:
(def json "{\"name\": \"John\", \"age\": 27}")
(.create jsonbox-storage json)

Recommendations

You probably won't work with JSON plain strings in real life, so you can use a clojure JSON parsing library such as data.json, example:

(ns jsonbox-example.core
  (:import [io.jsonbox JsonBoxStorage])
  (:require [clojure.data.json :as json]))

(def jsonbox-storage (JsonBoxStorage. "clojurebox0000000000"))

(def json (json/write-str {:name "John" :age 28}))

(->
    (.create jsonbox-storage json) ;jsonbox return a json string result
    (json/read-str :key-fn keyword) ;convert string result to a clojure map
    (:_createdOn) ;extract a value from result 
    (println)))
Clone this wiki locally