Jul 24 2012
Add new JSON document using plain text string with LightCouch
Hey,
Small post outlining a small modification to the 0.4 lightcouch source to allow direct plain text json insertion. A colleague and I had a JSON string which we wanted to insert into lightcouch without having to create a JSON object – only to have .toString() be called during the final POST request.
We ended up adding the following public method based on the existing well written Response put(URI uri, Object object, boolean newEntity) method within the CouchDbClientBase class.
This breaks with the conventions in the code, though in our own case we wanted to avoid intermediate conversions and add the capability to directly add a new document.
Method Code
/**
* Performs a HTTP PUT save request to add a new document based on a JSON String. Modified by Menelaos Bakopoulos based on original lightcouch code.
* @return {@link Response}
*/
public Response put_new_document(String json_text) {
HttpResponse response = null;
try {
String id;
id = generateUUID();
HttpPut put = new HttpPut(builder(getDBUri()).
setEntity(put, json_text);
response = executeRequest(put);
return getResponse(response);
} finally {
close(response);

}