[JAVA-709] com.mongodb.util.JSON should provide JSON#serialize( Object o, Writer writer) and JSON#serialize( Object o, OutputStream out) to have a better support for streaming Created: 08/Dec/12  Updated: 31/Jan/18  Resolved: 31/Jan/18

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Angelo Assignee: Unassigned
Resolution: Won't Fix Votes: 2
Labels: driver
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File AbstractObjectSerializer.java     Text File ClassMapBasedObjectSerializer.java     Text File JSON.java     Text File JSONCallback.java     Text File JSONSerializers.java     Text File ObjectSerializer.java    

 Description   

To serialize a BasicDBObject to a JSON content, it uses the https://github.com/mongodb/mongo-java-driver/blob/master/src/main/com/mongodb/util/JSON.java which provides the JSON#serialize( Object o, StringBuilder buf).

IMHO, I think it should be better to provides 2 methods with Writer and OutputStream:

  • JSON#serialize( Object o, Writer writer)
  • JSON#serialize( Object o, OutputStream out)

Why? Because if you wish returns the JSON content from a Servlet (HttpServletResponse#getWriter(), HttpServletResponse#getOutputStream()), we cannot use streaming mode. We need to create a StringBuilder and put it to the http response writer or outpustream :

-------------------------------------------------------
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BasicDBObject o = ...;
StringBuilder buf = new StringBuilder ();
JSON.serialize(o, buf);
response.getWriter().write(buf.toString());
// same code :
//response.getWriter().write(o.toString());
}
-------------------------------------------------------

I think it should be better to do with streaming mode like this:

-------------------------------------------------------
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BasicDBObject o = ...;
JSON.serialize(o, response.getWriter());
}
-------------------------------------------------------



 Comments   
Comment by Jeffrey Yemin [ 31/Jan/18 ]

The com.mongodb.util.JSON class is deprecated, so we won't be expanding its API. Please note that the JsonWriter class which has replaced it does take a Writer.

Comment by Angelo [ 17/Dec/12 ]

By waiting for your decision, I have created mongo-jee project and I have putted my code on Git at https://github.com/angelozerr/mongo-jee/tree/master/src/main/java/com/mongodb/jee/util

Comment by Angelo [ 17/Dec/12 ]

I have modified source of 2.10.1 to manage streaming with JSON.serialize which provides 2 methods :

  • JSON#serialize( Object o, Writer writer)
  • JSON#serialize( Object o, OutputStream out)

Note that I have changed the package to com.mongodb.jee.util to have none conflict with my JEE support for MongoDB and your driver.

I'm using this JSON serialize methods with Servlet (Writer) and JAX-RS MessageBodyWriter.

Comment by Angelo [ 15/Dec/12 ]

It seems this issue is similat than https://jira.mongodb.org/browse/JAVA-104

Generated at Thu Feb 08 08:52:53 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.