Package com.avaje.ebean.text.json
JSON formatting and parsing objects (See JsonContext).
See:
Description
Interface Summary |
JsonContext |
Converts objects to and from JSON format. |
JsonElement |
Marker interface for all the Raw JSON types. |
JsonReadBeanVisitor<T> |
Provides for some custom handling of json content as it is read. |
JsonValueAdapter |
Allows you to customise the Date and Timestamp formats. |
JsonWriteBeanVisitor<T> |
Allows for customising the JSON write processing. |
JsonWriter |
The JSON Writer made available to JsonWriteBeanVisitor's so that you can append your own JSON content into the output. |
Package com.avaje.ebean.text.json Description
JSON formatting and parsing objects (See JsonContext).
The goal is to provide JSON support taking into account various ORM issues such as partial objects (for fetching and updating), reference beans and bi-directional relationships.
Example:
// find some customers ...
List<Customer> list = Ebean.find(Customer.class)
.select("id, name, status, shippingAddress")
.fetch("billingAddress","line1, city")
.fetch("billingAddress.country", "*")
.fetch("contacts", "firstName,email")
.order().desc("id")
.findList();
JsonContext json = Ebean.createJsonContext();
JsonWriteOptions writeOptions = new JsonWriteOptions();
writeOptions.setRootPathVisitor(new JsonWriteBeanVisitor<Customer>() {
public void visit(Customer bean, JsonWriter ctx) {
System.out.println("write visit customer: " + bean);
ctx.appendKeyValue("dummyCust", "34");
ctx.appendKeyValue("smallCustObject", "{\"a\":34,\"b\":\"asdasdasd\"}");
}
});
writeOptions.setPathProperties("contacts", "firstName,id");
writeOptions.setPathVisitor("contacts", new JsonWriteBeanVisitor<Contact>() {
public void visit(Contact bean, JsonWriter ctx) {
System.out.println("write additional custom json on customer: " + bean);
ctx.appendKeyValue("dummy", " 3400" + bean.getId() + "");
ctx.appendKeyValue("smallObject", "{\"contactA\":34,\"contactB\":\"banana\"}");
}
});
// output as a JSON string with pretty formatting
String s = json.toJsonString(list, true, writeOptions);
Copyright © 2010. All Rights Reserved.