-
Notifications
You must be signed in to change notification settings - Fork 1
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:
- 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"]
- Import jsonbox library in your code namespace:
(:import [io.jsonbox JsonBoxStorage])
- Create a variable for jsonbox instance:
(def jsonbox-storage (JsonBoxStorage. "clojurebox0000000000"))
- Now you can use any function from the documentation, example:
(def json "{\"name\": \"John\", \"age\": 27}")
(.create jsonbox-storage json)
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)))