Question

I am using SQL Korma to run some simple examples on a DB and am trying to convert this into JSON using Cheshire.

This works well when I have only 1 record returned but throws an error when I have more than 1 result.

Here are the 2 functions:

(defn get-room [id]
    (first (select room
             (where {:id id})
             (limit 1))))

(defn get-rooms []
    (select room))

and data:

(def x get-rooms)

(def y (get-room 1))

X is of type testproj.models.db:

(x)
=> [{:created_on "2014-04-05 13:19:47", :id 1, :description "Room 1"} {:created_on "2014-04-05 13:20:17", :id 2, :description "Room 2"} {:created_on "2014-04-05 13:20:20", :id 3, :description "Room 3"}]

Because y is a Hashmap:

(pr-str y)
=> "{:created_on \"2014-04-05 13:19:47\", :id 1, :description \"Room 1\"}"

Trying to convert to Json:

(cheshire.core/generate-string x)
JsonGenerationException Cannot JSON encode object of class: class testproj.models.db$get_rooms: testproj.models.db$get_rooms@507501ff  cheshire.generate/generate (generate.clj:147)

(cheshire.core/generate-string y)
=> "{\"created_on\":\"2014-04-05 13:19:47\",\"id\":1,\"description\":\"Room 1\"}"

Why is korma returning different types based on the amount of records (this would help me understand this better) and secondly - how should I go about this?

Was it helpful?

Solution

It seems you're missing a function call. Try this:

(cheshire.core/generate-string (x))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top