Noir.response and custom java object

86 views
Skip to first unread message

arekanderu

unread,
Oct 4, 2012, 1:04:26 PM10/4/12
to clo...@googlegroups.com
Hello,

I am not sure if i am supposed to ask a noir-related question in the group but the noir issues at github is closed so i thought someone here might know. I am trying to use a custom encoder with noir.response but i am failing to understand how.  

This is what I am trying to do which at the moment fails of course:

(ns my-app.views.my-page
  (:require [noir.response :as resp]))

(defpage "/my-page" []
  (let [java-object (do-something)]
  (resp/json java-object)))

The error message

Cannot JSON encode object of class: class-name-here - (class org.codehaus.jackson.JsonGenerationException) 
 
It does work however if i use gson on my java-object before i pass it to resp/json but i prefer to do it without gson. I am pretty sure its possible :)

Thank you for any replies
 

gaz jones

unread,
Oct 4, 2012, 1:26:11 PM10/4/12
to clo...@googlegroups.com
It seems to be using cheshire under the covers, did you add an encoder
for your class? e.g:

(add-encoder java.awt.Color
(fn [c jsonGenerator]
(.writeString jsonGenerator (str c))))
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your
> first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

arekanderu

unread,
Oct 4, 2012, 1:32:25 PM10/4/12
to clo...@googlegroups.com
Hi Gaz

That's exactly the part that I can't figure out. Where exactly am I supposed to do that? Inside the clj where I am going to use noir.response? 
My apologies if my question seems silly but I am a bit brain-blocked with this.

arekanderu

unread,
Oct 4, 2012, 1:54:17 PM10/4/12
to clo...@googlegroups.com
In fact, on the latest commit, response.clj does not seem to use chesire.custom anymore even though this commit indicates otherwise.

arekanderu

unread,
Oct 4, 2012, 2:26:56 PM10/4/12
to clo...@googlegroups.com
OK, the problem is that the custom encoders isn't merged with the master tree yet so, you need to download this branch, run lein jar at the root directory of the project and then use that jar file in your project dependencies. I did that by having a local maven repo in my project

...unfortunately though a bunch of deps are required though or else lein run blows up. I am still working on it to see what can i do. What a mess

arekanderu

unread,
Oct 4, 2012, 2:40:05 PM10/4/12
to clo...@googlegroups.com
After adding the dependencies of noir to my own project.clj, everything worked fine :)

I hope that noir developers will make it a bit easier soon...
Reply all
Reply to author
Forward
0 new messages