Re: ⚠ Re: Bug in JDBC.driver CopyManager method when copying json output

From: "Stadlmann, Gerson" <Gerson(dot)Stadlmann(at)ifco(dot)com>
To: Dave Cramer <pg(at)fastcrypt(dot)com>
Cc: List <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: ⚠ Re: Bug in JDBC.driver CopyManager method when copying json output
Date: 2018-10-20 14:25:08
Message-ID: E378CD9D-10EF-4E53-9825-E2C4D4650058@ifco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hello,
Even your sample clearly shows duplication of escape characters.
My backend was a test program to show the issue clearly.

I solved it for my use-case using csv format instead of text.

Kind regards

Gerson
Am 20.10.2018 um 15:44 schrieb Dave Cramer <pg(at)fastcrypt(dot)com<mailto:pg(at)fastcrypt(dot)com>>:

⚠ This email originated from outside of Brambles.
I suggest this is a backend problem...

This is done with psql...

select to_json('my name is \"rabbit\"'::text);
to_json
-----------------------------
"my name is \\\"rabbit\\\""
(1 row)

test=# COPY (select to_json('my name is \"rabbit\"'::text)) TO STDOUT WITH NULL AS ''";
test"#
test=# COPY (select to_json('my name is \"rabbit\"'::text)) TO STDOUT WITH NULL AS '';
"my name is \\\\\\"rabbit\\\\\\""

Dave Cramer

davec(at)postgresintl(dot)com<mailto:davec(at)postgresintl(dot)com>
www.postgresintl.com<https://urldefense.proofpoint.com/v2/url?u=http-3A__www.postgresintl.com&d=DwMFaQ&c=hQhrIJ5kaiukSwSR0qBfqg&r=MhvlkO9AM5yMgRjBEW4OMgdAsd_218P_93GvtxG3XJk&m=v2w0NSVw39_DUk3WF1jhPIwCFIOfPQJs3nhAK7esjFg&s=U0i342OK1ub8D5jTjmtq-N90jzFHPbuvBYJctc5db5A&e=>

On Fri, 19 Oct 2018 at 10:12, Stadlmann, Gerson <Gerson(dot)Stadlmann(at)ifco(dot)com<mailto:Gerson(dot)Stadlmann(at)ifco(dot)com>> wrote:
Dear JDBC-Devs,
i think i found an escaping bug that causes duplicate escaping with backslash when using jdbc drivers CopyManager with JSON results.

Sample Query:
select to_json( 'my name is "rabbit"'::text);

Executing this query as Statement
<java>
Statement statement = connection.createStatement()
ResultSet resultSet = statement.executeQuery("select to_json('my name is \"rabbit\"'::text)");
String result=resultSet.next().getString(1);// => "my name is \"rabbit\""
</java>

Execution of this query using CopyManager (see result file attachment)
This Execution does the duplicate escaping for JSON output causing invalid json text.
<java>
BaseConnection bc=(BaseConnection) connection;
final CopyManager cm=new CopyManager(bc);
File someFile=new File("test.json");
try(FileWriter fw=new FileWriter(someFile)){
cm.copyOut("COPY (select to_json('my name is \"rabbit\"'::text)) TO STDOUT WITH NULL AS ''", fw);
// => my name is \\"rabbit\\" instead
}
</java>

My Driver:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>

My pg Server (Docker):
10.4 (Debian 10.4-2.pgdg90+1)

If that is required behavior I suggest to introduce another format “json” in addition to “text” and “csv”.

Kind regards

Gerson Stadlmann
Manager Software Development International

[IFCOGlobal_logo_RGB - email v2]

IFCO Systems Austria GmbH
Unterthalham Straße 2 | 4694 Ohlsdorf | Austria
T: +43 (0)7612-787 782 | M: +43 664 3855 154
gerson(dot)stadlmann(at)ifco(dot)com<mailto:gerson(dot)stadlmann(at)ifco(dot)com> | www.ifco.com<http://www.ifco.com/>

A better supply chain serves us all. Let’s eat.

Board of Management: Wolfgang Orgeldinger / Chairman, Margit Kendler, Christoph Trixl, Heimo Thaler FN213156s, LG Wels, UID-Nr. ATU 52552107 SAVE PAPER - THINK BEFORE YOU PRINT
Board of Management: Wolfgang Orgeldinger / Chairman, Margit Kendler, Christoph Trixl, Heimo Thaler FN213156s, LG Wels, UID-Nr. ATU 52552107 SAVE PAPER - THINK BEFORE YOU PRINT

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message David G. Johnston 2018-10-20 14:44:05 Bug in JDBC.driver CopyManager method when copying json output
Previous Message Dave Cramer 2018-10-20 13:43:58 Re: Bug in JDBC.driver CopyManager method when copying json output