cheshire
Clojure JSON and JSON SMILE (binary json format) encoding/decoding
dakrone/cheshire · GitHub cheshire - clojure json and json smile (binary json format) encoding/decoding
I need to encode a Clojure byte array to JSON. I have been trying to do this using Cheshire's add-encoder function, like this:
(add-encoder [Ljava.lang.Byte encode-seq)
The problem is the reader always complains about an unmatched delimiter. I'm trying to encode something like the following:
{:bytes #<byte[] [B@9de27c>}
But this always gives me
JsonGenerationException Cannot JSON encode object of class: class [B: [B@9de27c cheshire.generate/generate (generate.clj:147)
So I'm trying to add a custom encoder. Am I even doing this the right way?
Source: (StackOverflow)
I am trying to create a node in my Neo4j datastore using clojure neocons and I am running into an error that I think is related to formatting json:
Exception: com.fasterxml.jackson.core.JsonGenerationException: Cannot JSON encode object of class: class recursiftion.dao_graph$create_node: recursiftion.dao_graph$create_node@32693b5
generate.clj:148 cheshire.generate/generate
generate.clj:119 cheshire.generate/generate
core.clj:31 cheshire.core/generate-string
core.clj:21 cheshire.core/generate-string
cypher.clj:51 clojurewerkz.neocons.rest.cypher/query
core.clj:2440 clojure.core/comp[fn]
dao_graph.clj:428 recursiftion.dao-graph/create-node
model.clj:131 recursiftion.model/createNode
controller.clj:206 recursiftion.controller/fn
core.clj:99 compojure.core/make-route[fn]
core.clj:45 compojure.core/if-route[fn]
core.clj:30 compojure.core/if-method[fn]
core.clj:112 compojure.core/routing[fn]
core.clj:2570 clojure.core/some
core.clj:112 compojure.core/routing
RestFn.java:139 clojure.lang.RestFn.applyTo
core.clj:632 clojure.core/apply
core.clj:117 compojure.core/routes[fn]
keyword_params.clj:32 ring.middleware.keyword-params/wrap-keyword-params[fn]
...
I suspect this may be related to Cheshire based on the error report. But I am confused because I am not including the Cheshire library in the file where the error occurs.
I have this as my wrapper because I am making cors POST & GET requests and I need to return JSON from my controller.clj
(def app
(-> (handler/api app-routes)
(middleware/wrap-json-body {:keywords? true})
(middleware/wrap-json-response)
(wrap-cors routes #"^http://localhost:9000$")))
Below are the libraries referenced in my controller.clj
(ns recursiftion.controller
(:use [ring.adapter.jetty :only [run-jetty]]
[recursiftion.websocket :only [wamp-handler]]
[recursiftion.config :only [conf]]
)
(:require [compojure.core :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]
[clojure.java.io :as io]
[ring.util.io :refer [string-input-stream]]
[ring.util.response :as resp]
[ring.util.response :refer [response]]
[ring.middleware.json :as middleware]
[ring.middleware.cors :refer [wrap-cors]]
[environ.core :refer [env]]
[cheshire.core :refer :all]
[recursiftion.model :as model]
[monger.json]
[clojure.pprint :refer [pprint]]))
Here is my POST endpoint code in my controller.clj
(POST "/node/create" request
(let [node_object (or (get-in request [:params :data])
(get-in request [:body :data])
"1ROUTER_ERROR")]
{:status 200
:headers {"Content-Type" "application/json"}
:body (recursiftion.model/createNode node_object)
}
))
Here are the referenced libraries that pass through my model and are found in a dao_graph.clj
(ns recursiftion.dao_graph
(:require [clojure.string]
[clojurewerkz.neocons.rest :as nr]
[clojurewerkz.neocons.rest.cypher :as cy]
[clojure.pprint :refer [pprint]])
(:import [org.bson.types.ObjectId])
)
Here is the definition of the function that is invoked inside dao_graph.clj:
(defn create-node [ nodeobj ]
(let [ _nodeobj nodeobj ]
(cy/tquery conn create-node { :_nodetype (get-in _nodeobj [:type]) })))
And here is the cypher query that returns a map with "..." as its keys
(def create-node "CREATE (m:{_nodetype})
RETURN M;")
I am very grateful for your help in solving this issue.
Source: (StackOverflow)
Cheshire's custom encoders seem suitable for this problem and I wrote a little helper function:
(defn add-rec-encoder [Rec type-token]
(add-encoder Rec
(fn [rec jg] (.writeString jg
(str (encode-map (assoc rec :type type-token) jg))))))
(defrecord A [a])
(add-rec-encoder A "A")
(encode (->A "abc"))
But it produces a strange trailing ""
.
=> {"a":"abc","type":"A"} ""
What is causing this? And is there another approach worth considering (I also need to be able to decode back to a record based on this type-token)?
Source: (StackOverflow)
I'm using Cheshire to generate some JSON for data structures like this:
(require '[cheshire.core :refer [generate-string])
(generate-string {:id 123, :foo "something", :bar nil})
Which produces JSON like this:
{"id": 123, "foo": "something", "bar": null}
What I'd like is for the JSON to omit the keys without values; e.g.
{"id": 123, "foo": "something"}
Can Cheshire do this? I can certainly pre-filter the map before calling generate-string
, but since Cheshire has to traverse my data structure anyway, I thought it would be more performant to instruct Cheshire to do the filtering.
Source: (StackOverflow)