datafly icon indicating copy to clipboard operation
datafly copied to clipboard

possible issue using encode-json to encode a retrieve-all result consisting of a single result row

Open Failproofshark opened this issue 10 years ago • 0 comments

I technically found this while using caveman2 and traced it to the encode-json function imported from datafly.

If I have a table called "test" structured as such:

id foo
1 "baz"
2 "baz"
3 "bar"
4 "baz

and run the following code

(encode-json 
  (retrieve-one
    (select :*
      (from :test)
      (where (:= :foo "bar")))))

The result is as follows "{\"id\":[1,\"foo\",\"bar\"]}" Which is looks to be problematic. I was instead expecting something along the lines of "[{\"id\":1,\"foo\":\"bar\"}]

After looking at encode-json and then modifying the convert-for-json function to try to trace the code's flow, I found that the function identifies the returned list as an association-list instead of a regular list of embedded plists. Running (convert-for-json '((:id 1 :foo "bar"))) yields ((:ID . #(1 :FOO "bar"))) instead of something like the following #(((:ID . 1) (:FOO . "bar")))

While this is consistent with how association-list-p works, it causes strange problems when processing data on the receiving end when the it expects an array of objects.

I've wrote a test for the expected behavior and a copy of the modified "verbose" version of convert-for-json that I used to trace the problem and sent a pull request for your perusal.

Failproofshark avatar Mar 29 '15 18:03 Failproofshark