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