001package io.ebean.text.json;
002
003import io.ebean.FetchPath;
004import io.ebean.config.JsonConfig;
005import io.ebean.text.PathProperties;
006
007import java.util.HashMap;
008import java.util.Map;
009
010/**
011 * Provides options for customising the JSON write process.
012 * <p>
013 * You can explicitly state which properties to include in the JSON output for
014 * the root level and each path.
015 * </p>
016 */
017public class JsonWriteOptions {
018
019  protected FetchPath pathProperties;
020
021  protected Object objectMapper;
022
023  protected JsonConfig.Include include;
024
025  protected Map<String, JsonWriteBeanVisitor<?>> visitorMap;
026
027  /**
028   * Parse and return a PathProperties from nested string format like
029   * (a,b,c(d,e),f(g)) where "c" is a path containing "d" and "e" and "f" is a
030   * path containing "g" and the root path contains "a","b","c" and "f".
031   *
032   * @see io.ebean.text.PathProperties#parse(String)
033   */
034  public static JsonWriteOptions parsePath(String pathProperties) {
035
036    return pathProperties(PathProperties.parse(pathProperties));
037  }
038
039  /**
040   * Construct JsonWriteOptions with the given pathProperties.
041   */
042  public static JsonWriteOptions pathProperties(FetchPath pathProperties) {
043    JsonWriteOptions o = new JsonWriteOptions();
044    o.setPathProperties(pathProperties);
045    return o;
046  }
047
048  /**
049   * Set the Map of properties to include by path.
050   */
051  public void setPathProperties(FetchPath pathProperties) {
052    this.pathProperties = pathProperties;
053  }
054
055  /**
056   * Return the properties to include by path.
057   */
058  public FetchPath getPathProperties() {
059    return pathProperties;
060  }
061
062  /**
063   * Return the include mode for this request.
064   */
065  public JsonConfig.Include getInclude() {
066    return include;
067  }
068
069  /**
070   * Set the include mode for this request.
071   */
072  public void setInclude(JsonConfig.Include include) {
073    this.include = include;
074  }
075
076  /**
077   * Register a JsonWriteBeanVisitor for the root level.
078   */
079  public JsonWriteOptions setRootPathVisitor(JsonWriteBeanVisitor<?> visitor) {
080    return setPathVisitor(null, visitor);
081  }
082
083  /**
084   * Register a JsonWriteBeanVisitor for the given path.
085   */
086  public JsonWriteOptions setPathVisitor(String path, JsonWriteBeanVisitor<?> visitor) {
087    if (visitorMap == null) {
088      visitorMap = new HashMap<>();
089    }
090    visitorMap.put(path, visitor);
091    return this;
092  }
093
094  /**
095   * Return the Map of registered JsonWriteBeanVisitor's by path.
096   */
097  public Map<String, JsonWriteBeanVisitor<?>> getVisitorMap() {
098    return visitorMap;
099  }
100
101  /**
102   * Return the jackson object mapper to use.
103   * <p/>
104   * If null the ObjectMapper from DatabaseConfig will be used.
105   */
106  public Object getObjectMapper() {
107    return objectMapper;
108  }
109
110  /**
111   * Set the jackson object mapper to use.
112   * <p/>
113   * If null the ObjectMapper from DatabaseConfig will be used.
114   */
115  public void setObjectMapper(Object objectMapper) {
116    this.objectMapper = objectMapper;
117  }
118}