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()).

path(id).build());
setEntity(put, json_text);
response = executeRequest(put);
return getResponse(response);
} finally {
close(response);

}
}



Chris Georgoulis

Chris Georgoulis

Summary: - Primary programming language: Java SE (SWING, JDBC), Java EE, (JSF - PrimeFaces, JPA - EclipseLink/Hibernate, EJB, CDI ), - Secondary languages: C/C++, Web Technologies(HTML, XHTML, XML, CSS, JavaScript, jQuery, AngularJS). - Frameworks: Android SDK, Global Sensors Network (GSN), Open Computer Vision (OpenCV). - IDE's: Eclipse, Visual Studio - Other Apps: MS Office, Photoshop, Dreamweaver - Database Design - SQL(MySQL), NoSQL(MongoDB) - Modeling Tools: UML

More Posts