pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Craig Ringer <ringerc@ringerc.id.au>
To: Mikko Tiihonen <mikko.tiihonen@nitorcreations.com>
Cc: pgsql-jdbc@postgresql.org
Subject: Re: json type
Date: Tue, 22 May 2012 12:53:32 +0800
Message-ID: <4FBB1BCC.8040709@ringerc.id.au> (raw)
In-Reply-To: <4FBA73E1.5090106@nitorcreations.com>
References: <4FAB8796.40904@nitorcreations.com>
	<4FB9FA49.4070409@ringerc.id.au>
	<4FBA73E1.5090106@nitorcreations.com>

On 05/22/2012 12:57 AM, Mikko Tiihonen wrote:

> Unfortunately there is no standard json type on java side so adding 
> support for json to postgres jdbc is not straight forward.
While true, you can accept it as a java.lang.String and feed it into 
your JSON library of choice. Most people will be doing that directly or 
via a facade that takes care of it for them, like a custom JPA mapping type.

Personally I wouldn't want PgJDBC trying to convert the serialised JSON 
representations for me, I'd just land up fighting it the first time I 
wanted to do something it hadn't anticipated.

>
> I've been thinking that we could have a code that allows the postgres 
> jdbc user to set the json class to use (gson, org.json, json-simple, 
> jackson) and add some reflection code into the jdbc driver to return 
> path so that it works nicely. And of course the reverse reflection 
> code for sending json to database.

IMO, reflection + database access = ick.

Also, remember that people often won't want PgJDBC to convert the result 
into a smart Java JSON object representation like a Jackson JSONNode. 
They'll want to use data binding to convert the JSON to/from an object 
tree. For that purpose the best thing PgJDBC can do is get out of the 
way and give the user access to the raw JSON data to feed into 
Jackson/whatever.

Reflection isn't as slow as it was, but it's still unusable under 
SecurityManager control and can't be inlined/JIT'd properly. I think 
reflection is the wrong answer here. We'd be better off bundling 
implementations of an adapter interface and allowing which one to be 
specified at runtime on a system-wide or per-connection basis. The 
desired adapter would be instantiated (as a static member for the global 
setting controlled by system properties, or as a Connection member for 
the per-connection version) and would be used for all conversions 
between JSON text received from Pg and the user's desired JSON type. The 
default implementation if no other one was selected would be to use 
java.lang.String as the JSON type, or maybe to return an InputStream or 
a Reader to allow streaming access.

The only worry I have with an adapter interface approach also exists 
with a reflection-based approach, and that's the inability to stream 
JSON in and out for large values. The adapter interface should probably 
also accept stream-oriented access, but that'll require a bit more 
investigation into the streaming interfaces of the various JSON 
libraries currently available.

Think for a simple non-streaming version:

public interface PgJSONAdapter<JSONType> {
   public JSONType toJSON(String jsonText);
   public String fromJSON(JSONType jsonObject);
}

public class PgJSONAdapterString implements PgJSONAdapter<String> {
   public String toJSON(String jsonText) { return jsonText; }
   public String fromJSON(String jsonObject) { return jsonObject; }
}

public class PgJSONAdapterJacksonTree implements PgJSONAdapter<JsonNode> {
   public JsonNode toJSON(String jsonText) {
     return new ObjectMapper().readValue(jsonText, JsonNode.class);
   }
   public String fromJSON(JsonNode jsonObject) {
     return jsonObject.toString();
   }
}

You wouldn't get specific param and return types on the get/set json 
methods, but you don't get that with a reflection approach either, and 
the JDBC API doesn't really allow us to extend Connection, Statement, 
ResultSet, etc nicely anyway (not least as these are often wrapped by 
connection pools). There's nothing wrong with using 
getObject()/setObject() for JSON IMO.

Look at http://wiki.fasterxml.com/JacksonInFiveMinutes and you'll 
quickly see how hard it'd be to come up with any scheme that'll satisfy 
all the ways people are going to want to work with JSON data. Much 
easier to just let Pg provide and accept strings and streams, plus 
*maybe* a simple translator adapter. I'm honestly not even convinced 
that's worth doing until/unless a future JSR promotes a JSON library to 
a standard API and part of the JDK.

--
Craig Ringer

view thread (2+ messages)

Message-ID: <4FBB1BCC.8040709@ringerc.id.au>
Permalink:  ../4FBB1BCC.8040709@ringerc.id.au/
Also on:    postgresql.org/message-id/4FBB1BCC.8040709@ringerc.id.au

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: ringerc@ringerc.id.au, mikko.tiihonen@nitorcreations.com
  Subject: Re: json type
  In-Reply-To: <4FBB1BCC.8040709@ringerc.id.au>
  References: <4FAB8796.40904@nitorcreations.com> <4FB9FA49.4070409@ringerc.id.au> <4FBA73E1.5090106@nitorcreations.com> <4FBB1BCC.8040709@ringerc.id.au>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox