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.
Not really enough information. However, this exception is often seen when cheshire does not recognise the data type of something it has been told to encode as json. The error looks like you might be trying to json encode a function rather than output from a function? I would add a println in your endpoint and print out what you believe is being returned. If the output looks like a function object rather than something which json can encode, that is probably the cause.
As to where this is happening, my guess is that your middleware is trying to encode the response body as json.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With