[JAVA-3365] serializing collection to json over wire Created: 26/Jul/19  Updated: 11/Sep/19  Resolved: 29/Jul/19

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

Type: Task Priority: Major - P3
Reporter: Sweet S Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 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.



 Comments   
Comment by Esha Bhargava [ 29/Jul/19 ]

sweetosaad@gmail.com  Thanks for your report. Please note that this project is for reporting bugs or feature suggestions for the driver. For MongoDB-related support discussion please post on the [mongodb-user group|http://groups.google.com/group/mongodb-user] or [Stack Overflow with the mongodb tag|http://stackoverflow.com/questions/tagged/mongodb]. A question like this involving more discussion would be best posted on the mongodb-users group.

 

 

 

 

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