sinamdar2015 / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Integrate with java.sql.ResultSet #464

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I would like to do the following:

    Statement s = "SELECT id, label FROM arbitrary_view";
    PreparedStatement ps = getConnection().prepareStatement( s );
    ResultSet rs = ps.executeQuery();

    Gson gson = new Gson();
    String json = gson.toJson( rs );

The ResultSetMetaData contains all the information necessary to create a 
sensible default JSON structure for the basic types (strings, numbers) based on 
the JDBC driver being used. The above code should generate:

    {
      id: id_value_1,
      label: label_value_1
    },
    {
      id: id_value_2,
      label: label_value_2
    }

Instead, the code generates the following:

java.lang.IllegalArgumentException: class java.text.DecimalFormat declares 
multiple JSON fields named maximumIntegerDigits
    at com.ahs.ismtool.QueryService.handle(QueryService.java:85)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111)
    at org.eclipse.jetty.server.Server.handle(Server.java:350)
    at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:454)
    at org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:890)
    at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:944)
    at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:630)
    at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230)
    at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:77)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:620)
    at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:46)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:603)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:538)
    at java.lang.Thread.run(Unknown Source)

Original issue reported on code.google.com by dave.jar...@gmail.com on 31 Jul 2012 at 11:57

GoogleCodeExporter commented 9 years ago
See also:

http://stackoverflow.com/questions/6514876/converting-resultset-to-json-faster-o
r-better-way

Original comment by dave.jar...@gmail.com on 1 Aug 2012 at 12:04

GoogleCodeExporter commented 9 years ago
You need to write a custom type adapter.

Original comment by limpbizkit on 2 Sep 2012 at 9:28

GoogleCodeExporter commented 9 years ago
Is this not a common enough feature to warrant implementation?

Original comment by dave.jar...@gmail.com on 3 Sep 2012 at 12:44

GoogleCodeExporter commented 9 years ago
Yes. I too was expecting a default implementation for such kinds of objects. 
But there apparently isn't.

Original comment by ashleyli...@gmail.com on 9 Nov 2012 at 7:49

GoogleCodeExporter commented 9 years ago
I was forced by EclipseLink to redefine id field in some of my objects (kind of 
bug in EclipseLink...). And now all the json serialization is broken. And there 
is no easy way to fix it. 

shouldSkipField(FieldAttributes f) cant help because it has no access to the 
java type which is actually serializing (you can get a declaring type but it is 
an abstract class in my case and the actual class is unknown).

The only way I see now is to exclude this field from a serialization, serialize 
object to json tree and add the field manually. Or to write a dozen of adapters.

It would be good to have some mechanism to resolve this issue. Instead of
        if (previous != null) {
          throw new IllegalArgumentException(declaredType
              + " declares multiple JSON fields named " + previous.name);
        }
we can use some policy enum to define behavior. For example "prefer 
overridden", "ignore overridden", "throw exception".

Or to add some annotation which can be used on overridden field.

Original comment by kostya31...@gmail.com on 26 Mar 2014 at 2:40