001package com.avaje.ebean.text.json; 002 003import com.avaje.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<String, JsonReadBeanVisitor<?>>(); 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 * </p> 070 */ 071 public JsonReadOptions setEnableLazyLoading(boolean enableLazyLoading) { 072 this.enableLazyLoading = enableLazyLoading; 073 return this; 074 } 075 076 /** 077 * Return the Jackson ObjectMapper to use (if not wanted to use the objectMapper set on the ServerConfig). 078 */ 079 public Object getObjectMapper() { 080 return objectMapper; 081 } 082 083 /** 084 * Set the Jackson ObjectMapper to use (if not wanted to use the objectMapper set on the ServerConfig). 085 */ 086 public JsonReadOptions setObjectMapper(Object objectMapper) { 087 this.objectMapper = objectMapper; 088 return this; 089 } 090 091 /** 092 * Set the persistence context to use when building the object graph from the JSON. 093 */ 094 public JsonReadOptions setPersistenceContext(PersistenceContext persistenceContext) { 095 this.persistenceContext = persistenceContext; 096 return this; 097 } 098 099 /** 100 * Return the persistence context to use when marshalling JSON. 101 */ 102 public PersistenceContext getPersistenceContext() { 103 return persistenceContext; 104 } 105 106 /** 107 * Return the load context to use. 108 */ 109 public Object getLoadContext() { 110 return loadContext; 111 } 112 113 /** 114 * Set the load context to use. 115 */ 116 public void setLoadContext(Object loadContext) { 117 this.loadContext = loadContext; 118 } 119 120}