Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-3365

serializing collection to json over wire

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • None
    • JSON
    • None

    Description

      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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            sweetosaad@gmail.com Sweet S
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: