Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. 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

    • Type: Icon: Improvement Improvement
    • Resolution: Won't Fix
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: API
    • Labels:

      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());
      }
      -------------------------------------------------------

        1. AbstractObjectSerializer.java
          1 kB
        2. ClassMapBasedObjectSerializer.java
          3 kB
        3. JSON.java
          16 kB
        4. JSONCallback.java
          5 kB
        5. JSONSerializers.java
          16 kB
        6. ObjectSerializer.java
          1 kB

            Assignee:
            Unassigned Unassigned
            Reporter:
            angelozerr Angelo
            Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: