com.avaje.ebean.text.json
Class JsonWriteOptions

java.lang.Object
  extended by com.avaje.ebean.text.json.JsonWriteOptions

public class JsonWriteOptions
extends Object

    

Provides options for customising the JSON write process.

You can optionally provide a custom JsonValueAdapter to handle specific formatting for Date and DateTime types.

You can optionally register JsonWriteBeanVisitors to customise the processing of the beans as they are processed and add raw JSON elements.

You can explicitly state which properties to include in the JSON output for the root level and each path.

 // 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);
 
 

Author:
rbygrave
See Also:
JsonContext.toList(Class, String, JsonReadOptions)

Constructor Summary
JsonWriteOptions()
           
 
Method Summary
 JsonWriteOptions copy()
          This creates and returns a copy of these options.
 String getCallback()
          Return a JSONP callback function.
 PathProperties getPathProperties()
          Return the properties to include by path.
 JsonValueAdapter getValueAdapter()
          Return the JsonValueAdapter.
 Map<String,JsonWriteBeanVisitor<?>> getVisitorMap()
          Return the Map of registered JsonWriteBeanVisitor's by path.
 JsonWriteOptions setCallback(String callback)
          Set a JSONP callback function.
 void setPathProperties(PathProperties pathProperties)
          Set the Map of properties to include by path.
 JsonWriteOptions setPathProperties(String path, Set<String> propertiesToInclude)
          Set the properties to include in the JSON output for the given path.
 JsonWriteOptions setPathProperties(String path, String propertiesToInclude)
          Set the properties to include in the JSON output for the given path.
 JsonWriteOptions setPathVisitor(String path, JsonWriteBeanVisitor<?> visitor)
          Register a JsonWriteBeanVisitor for the given path.
 JsonWriteOptions setRootPathProperties(Set<String> propertiesToInclude)
          Set the properties to include in the JSON output for the root level.
 JsonWriteOptions setRootPathProperties(String propertiesToInclude)
          Set the properties to include in the JSON output for the root level.
 JsonWriteOptions setRootPathVisitor(JsonWriteBeanVisitor<?> visitor)
          Register a JsonWriteBeanVisitor for the root level.
 JsonWriteOptions setValueAdapter(JsonValueAdapter valueAdapter)
          Set a JsonValueAdapter for custom DateTime and Date formatting.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JsonWriteOptions

public JsonWriteOptions()
Method Detail

copy

public JsonWriteOptions copy()
This creates and returns a copy of these options.

Note that it assumes that the JsonWriteBeanVisitor (if defined) are immutable and any JsonWriteBeanVisitor instances are shared between the original and the copy.


getCallback

public String getCallback()
Return a JSONP callback function.


setCallback

public JsonWriteOptions setCallback(String callback)
Set a JSONP callback function.


getValueAdapter

public JsonValueAdapter getValueAdapter()
Return the JsonValueAdapter.


setValueAdapter

public JsonWriteOptions setValueAdapter(JsonValueAdapter valueAdapter)
Set a JsonValueAdapter for custom DateTime and Date formatting.


setRootPathVisitor

public JsonWriteOptions setRootPathVisitor(JsonWriteBeanVisitor<?> visitor)
Register a JsonWriteBeanVisitor for the root level.


setPathVisitor

public JsonWriteOptions setPathVisitor(String path,
                                       JsonWriteBeanVisitor<?> visitor)
Register a JsonWriteBeanVisitor for the given path.


setPathProperties

public JsonWriteOptions setPathProperties(String path,
                                          Set<String> propertiesToInclude)
Set the properties to include in the JSON output for the given path.

Parameters:
propertiesToInclude - The set of properties to output

setPathProperties

public JsonWriteOptions setPathProperties(String path,
                                          String propertiesToInclude)
Set the properties to include in the JSON output for the given path.

Parameters:
propertiesToInclude - Comma delimited list of properties to output

setRootPathProperties

public JsonWriteOptions setRootPathProperties(String propertiesToInclude)
Set the properties to include in the JSON output for the root level.

Parameters:
propertiesToInclude - Comma delimited list of properties to output

setRootPathProperties

public JsonWriteOptions setRootPathProperties(Set<String> propertiesToInclude)
Set the properties to include in the JSON output for the root level.

Parameters:
propertiesToInclude - The set of properties to output

getVisitorMap

public Map<String,JsonWriteBeanVisitor<?>> getVisitorMap()
Return the Map of registered JsonWriteBeanVisitor's by path.


setPathProperties

public void setPathProperties(PathProperties pathProperties)
Set the Map of properties to include by path.


getPathProperties

public PathProperties getPathProperties()
Return the properties to include by path.



Copyright © 2010. All Rights Reserved.