Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: Java 1.1
Community Wiki and Resources
Download Client Library
JavaDoc
Couchbase Developer Guide 2.0
Couchbase Server Manual 2.0
Java Client Library
SDK Forum
Wiki: Java Client Library
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
1.3 Working with Documents
Chapter Sections
Chapters

1.3.4. JSON Encoding/Decoding

One benefit of working with JSON documents is that you can map them (nearly) directly from/to objects in your application. Unlike with Java object serialization, you need to do some more work to convert your object to its appropriate JSON representation. Fortunately, libraries like GSON (as mentioned before) are here to help.

In the following example, we define a simple Beer class that implicitly carries the JSON fields as attributes. We can then use GSON to convert from the Object to JSON and back. The example is a little bit contrived, but it shows the roundtrip from Object to JSON back to Object pretty well that you can use throughout your application.

public class App {

  // Model the Beer
  static class Beer {
    String name;
    String type;
    String category;
    float abv;

    // Convenience method to generate a key
    public String getKey() {
      return type + "-" + name;
    }
  }

  public static void main(String args[]) throws Exception {

    // Connect
    CouchbaseClient client = new CouchbaseClient(
      Arrays.asList(URI.create("http://127.0.0.1:8091/pools")),
      "beer-sample",
      ""
    );

    // Instantiate GSON
    Gson gson = new Gson();

    // Create a beer
    Beer myAle = new Beer();
    myAle.name = "cool-ale";
    myAle.type ="beer";
    myAle.category = "Stouts and Ales";
    myAle.abv = 5.2f;

    // Convert the beer to JSON
    String json = gson.toJson(myAle);

    // Store in Couchbase
    client.set(myAle.getKey(), 0, json);

    // Read it back
    String readJson = (String) client.get(myAle.getKey());

    // Create a object out of it
    Beer sameBeer = gson.fromJson(readJson, Beer.class);

    // Now use it like:
    System.out.println(sameBeer.category);

    // Disconnect
    client.shutdown(3, TimeUnit.SECONDS);
    System.exit(0);
  }

}

You can also use the same pattern when you query a View, and include the complete docs through setIncludeDocs(true). Based on the previous Beer class, imagine a View that returns all beers stored in the bucket. We could query the View and immediately create Objects out of them:

View view = client.getView("beer", "only_beers");
Query query = new Query();
query.setIncludeDocs(true);
ViewResponse result = client.query(view, query);

for(ViewRow row : result) {
  Beer beer = gson.fromJson(row.getDocument(), Beer.class);
  System.out.println(beer.name);
}

The previous chapters introduced Couchbase Server and how to use it through the Java SDK. With this knowledge at hand, you are already able to build powerful applications on top of Couchbase Server 2.0 and Java. The next chapter digs a bit deeper and introduces advanced concepts that can help you along the way.