001package io.ebean.text.json;
002
003import io.ebean.bean.PersistenceContext;
004
005import java.util.LinkedHashMap;
006import java.util.Map;
007
008/**
009 * Provides the ability to customise the reading of JSON content.
010 * <p>
011 * You can register JsonReadBeanVisitors to customise the processing of the
012 * beans as they are processed and handle any custom JSON elements that
013 * could not be mapped to bean properties.
014 * </p>
015 */
016public class JsonReadOptions {
017
018  protected final Map<String, JsonReadBeanVisitor<?>> visitorMap;
019
020  protected Object objectMapper;
021
022  protected boolean enableLazyLoading;
023
024  protected PersistenceContext persistenceContext;
025
026  protected Object loadContext;
027
028  /**
029   * Default constructor.
030   */
031  public JsonReadOptions() {
032    this.visitorMap = new LinkedHashMap<>();
033  }
034
035  /**
036   * Return the map of JsonReadBeanVisitor's.
037   */
038  public Map<String, JsonReadBeanVisitor<?>> getVisitorMap() {
039    return visitorMap;
040  }
041
042  /**
043   * Register a JsonReadBeanVisitor for the root level.
044   */
045  public JsonReadOptions addRootVisitor(JsonReadBeanVisitor<?> visitor) {
046    return addVisitor(null, visitor);
047  }
048
049  /**
050   * Register a JsonReadBeanVisitor for a given path.
051   */
052  public JsonReadOptions addVisitor(String path, JsonReadBeanVisitor<?> visitor) {
053    visitorMap.put(path, visitor);
054    return this;
055  }
056
057  /**
058   * Return true if lazy loading is enabled after the objects are loaded.
059   */
060  public boolean isEnableLazyLoading() {
061    return enableLazyLoading;
062  }
063
064  /**
065   * Set to true to enable lazy loading on partially populated beans.
066   * <p>
067   * If this is set to true a persistence context will be created if one has
068   * not already been supplied.
069   */
070  public JsonReadOptions setEnableLazyLoading(boolean enableLazyLoading) {
071    this.enableLazyLoading = enableLazyLoading;
072    return this;
073  }
074
075  /**
076   * Return the Jackson ObjectMapper to use (if not wanted to use the objectMapper set on the DatabaseConfig).
077   */
078  public Object getObjectMapper() {
079    return objectMapper;
080  }
081
082  /**
083   * Set the Jackson ObjectMapper to use (if not wanted to use the objectMapper set on the DatabaseConfig).
084   */
085  public JsonReadOptions setObjectMapper(Object objectMapper) {
086    this.objectMapper = objectMapper;
087    return this;
088  }
089
090  /**
091   * Set the persistence context to use when building the object graph from the JSON.
092   */
093  public JsonReadOptions setPersistenceContext(PersistenceContext persistenceContext) {
094    this.persistenceContext = persistenceContext;
095    return this;
096  }
097
098  /**
099   * Return the persistence context to use when marshalling JSON.
100   */
101  public PersistenceContext getPersistenceContext() {
102    return persistenceContext;
103  }
104
105  /**
106   * Return the load context to use.
107   */
108  public Object getLoadContext() {
109    return loadContext;
110  }
111
112  /**
113   * Set the load context to use.
114   */
115  public void setLoadContext(Object loadContext) {
116    this.loadContext = loadContext;
117  }
118
119}