Ah finally, thanks John, the "with-open" pattern was indeed the missing 
piece: it works!

Just to summarize, here's what works for me:

(extend-type org.postgresql.jdbc4.Jdbc4Array
  json/JSONWriter
  (-write [o out]
    (json/-write (.getArray o) out)))

; plus any other additional required custom JSON writers 
(java.sql.Timestamp, Date, etc)

(with-open [conn (jdbc/get-connection *db*)]
  (json/write-str
    (jdbc/query {:connection conn}
                ["select * from <table with PG array in it>.."])))



On Wednesday, October 2, 2013 12:06:44 AM UTC-4, John Hume wrote:
>
> I don't use clojure.java.jdbc, so this may be non-idiomatic or just wrong, 
> but have you tried something like
>
> (with-open [connection (jdbc/db-connection *db*)]
>   (json/write-str
>     (jdbc/query {:connection connection} 
>       ["SELECT * FROM..."])))
>
>
>
> On Tue, Oct 1, 2013 at 8:13 PM, Christian Jauvin 
> <cja...@gmail.com<javascript:>
> > wrote:
>
>> Hi Philippe,
>>
>> The Jdbc4Array that's causing me trouble is not the "outer" one, 
>> returned from jdbc/query: that one seems to be properly handled by 
>> json/write-str.
>>
>> The problem happens with a Postgres table containing a text[] column:
>>
>> create table something (
>>     something_id serial primary key,
>>     list_of_something text[]
>> )
>>
>> In that case the query returns an ("inner") array for each record, and it 
>> gets converted to a Jdbc4Array, which, when passed to the JSON writer, 
>> causes the problem.
>>
>> Querying a table without a PG array works perfectly fine though.
>>
>>
>>
>> On Tuesday, October 1, 2013 4:21:33 PM UTC-4, Philippe Guillebert wrote:
>>
>>> Hi, 
>>>
>>> You probably need to realize your query using (doall (jdbc/query ...))
>>>
>>> Also, I was wondering, depending on your needs, you could convert 
>>> Jdbc4Array into a native type (vector ?) as a post-processing function 
>>> of your query and forget about registering JSON writers.
>>>
>>>
>>>
>>> On Tue, Oct 1, 2013 at 9:00 PM, Christian Jauvin <cja...@gmail.com>wrote:
>>>
>>>> Hi Roman,
>>>>
>>>> This approach works for java.sql.Timestamp, which was another type for 
>>>> which a JSON writer wasn't defined in my case.
>>>>
>>>> For org.postgresql.jdbc4.**Jdbc4Array however, there's something 
>>>> missing, because I get:
>>>>
>>>>     *org.postgresql.util.PSQLException: This connection has been 
>>>> closed.*
>>>>
>>>> As the person answering me on Stack Overflow suggested, it seems that 
>>>> the resultset should be somehow processed before the connection is closed, 
>>>> but I really don't know how to do given the construct I'm currently using:
>>>>
>>>> (json/write-str
>>>>   (jdbc/query *db*
>>>>     ["SELECT * FROM .."]))
>>>>
>>>> where and how could I intercept the resultset that way? Thanks.
>>>>
>>>>
>>>>
>>>> On Tuesday, October 1, 2013 12:49:10 PM UTC-4, r0man wrote:
>>>>>
>>>>> I think you need to implement the JSONWriter protocol for the
>>>>> Jdbc4Array class, and possibly for the datatypes that are in the
>>>>> array. This for example makes the json library aware of
>>>>> java.util.Date classes. 
>>>>>
>>>>> (extend-type java.util.Date
>>>>>   JSONWriter
>>>>>   (-write [date out]
>>>>>     (-write (str date) out)))
>>>>>
>>>>> Something like this (not tested):
>>>>>
>>>>> (extend-type org.postgresql.jdbc4.**Jdbc4Arra**y
>>>>>   JSONWriter
>>>>>   (-write [array out]
>>>>>     (-write (seq (.getArray array)) out)))
>>>>>
>>>>> Roman
>>>>>  
>>>>>
>>>>> On Tuesday, October 1, 2013 3:57:02 PM UTC+2, Christian Jauvin wrote:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> I asked this question on Stack Overflow yesterday:
>>>>>>
>>>>>> I want to jsonify the results of a query performed against a Postgres 
>>>>>> table containing a column of type text[], but the problem is that 
>>>>>> clojure.data.json.write-str doesn't seem to know how to handle PG 
>>>>>> arrays:
>>>>>>
>>>>>> *Exception Don't know how to write JSON of class 
>>>>>> org.postgresql.jdbc4.Jdbc4Array  clojure.data.json/write-generic*
>>>>>>
>>>>>>  Do I have to supply a custom handler, or is there a simpler way?
>>>>>>
>>>>>>
>>>>>> http://stackoverflow.com/**quest**ions/19103870/jsonify-a-**jdbc4a**
>>>>>> rray-in-clojure<http://stackoverflow.com/questions/19103870/jsonify-a-jdbc4array-in-clojure>
>>>>>>
>>>>>> I'm asking it here in the hope of getting (maybe) a simpler solution, 
>>>>>> or at least one I can implement readily, because the problem I have with 
>>>>>> one of the suggestions I received (extending org.postgresql.jdbc4.**
>>>>>> Jdbc4Arra**y to implement the missing function) is that I don't know 
>>>>>> how to make it work with my usage pattern, which is simply:
>>>>>>
>>>>>> (json/write-str 
>>>>>>     (jdbc/query *db* 
>>>>>>         ["SELECT * FROM ..."]))
>>>>>>
>>>>>> How do I "get the array before the connection is closed" with such a 
>>>>>> construct? Is there another way?
>>>>>>
>>>>>>   -- 
>>>> -- 
>>>> 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<http://groups.google.com/group/clojure?hl=en>
>>>> --- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "Clojure" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send 
>>>> an email to clojure+u...@**googlegroups.com.
>>>>
>>>> For more options, visit 
>>>> https://groups.google.com/**groups/opt_out<https://groups.google.com/groups/opt_out>
>>>> .
>>>>
>>>
>>>
>>>
>>> -- 
>>> Philippe
>>>  
>>  -- 
>> -- 
>> 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<javascript:>
>> 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 <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> --- 
>> You received this message because you are subscribed to the Google Groups 
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to clojure+u...@googlegroups.com <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> -- 
> http://elhumidor.blogspot.com/ 
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to