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

serializing collection to json over wire

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: JSON
    • Labels:
      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 Unassigned
            Reporter:
            sweetosaad@gmail.com Sweet S
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: