serializing collection to json over wire

XMLWordPrintableJSON

    • Type: Task
    • Resolution: Done
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: JSON
    • None
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Sort of naive approach to get the point across.

       

      MongoCursor<Document> cursor = ((MongoCollection)data).find().iterator();
      
      try {
       response.getWriter().write("[");
       while (cursor.hasNext()) {
       response.getWriter().write(cursor.next().toJson() + (cursor.hasNext() ? "," : ""));
       }
       response.getWriter().write("]");
      } finally {
       cursor.close();
      }

       

      // applying some json techniques
      
      MongoCursor<Document> cursor = ((MongoCollection)data).find().iterator();
      JsonWriter jsonWriter = new JsonWriter(response.getWriter());
      jsonWriter.writeStartDocument();
      jsonWriter.writeStartArray();
      
      while(cursor.hasNext()) {
          jsonWriter.writeStartDocument();
          jsonWriter.writeString(cursor.next().toJson());
          jsonWriter.writeEndDocument();
      }
      
      jsonWriter.writeEndArray();
      jsonWriter.writeEndDocument();
      
      cursor.close();
      

      Please guide for the write way to serialize collections to JSON.

              Assignee:
              Unassigned
              Reporter:
              Sweet S
              None
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: