001package io.avaje.jsonb; 002 003import io.avaje.jsonb.core.DefaultBootstrap; 004import io.avaje.jsonb.spi.Bootstrap; 005import io.avaje.jsonb.spi.PropertyNames; 006 007import java.io.*; 008import java.lang.reflect.Type; 009import java.util.Iterator; 010import java.util.ServiceLoader; 011 012/** 013 * Provides access to json adapters by type. 014 * 015 * <h4>Initialise with defaults</h3> 016 * 017 * <pre>{@code 018 * Jsonb jsonb = Jsonb.newBuilder().build(); 019 * }</pre> 020 * 021 * <h4>Initialise with some configuration</h3> 022 * 023 * <pre>{@code 024 * Jsonb jsonb = Jsonb.newBuilder() 025 * .serializeNulls(true) 026 * .serializeEmpty(true) 027 * .failOnUnknown(true) 028 * .build(); 029 * }</pre> 030 * 031 * <h4>fromJson</h4> 032 * <p> 033 * Read json content from: String, byte[], Reader, InputStream, JsonReader 034 * </p> 035 * <pre>{@code 036 * 037 * JsonType<Customer> customerType = jsonb.type(Customer.class); 038 * 039 * Customer customer = customerType.fromJson(content); 040 * 041 * }</pre> 042 * 043 * <h4>toJson</h4> 044 * <p> 045 * Write json content to: String, byte[], Writer, OutputStream, JsonWriter 046 * </p> 047 * <pre>{@code 048 * 049 * JsonType<Customer> customerType = jsonb.type(Customer.class); 050 * 051 * String asJson = customerType.toJson(customer); 052 * 053 * }</pre> 054 */ 055public interface Jsonb { 056 057 /** 058 * Create and return a new Jsonb.Builder to configure before building the Jsonb instance. 059 * <p> 060 * We can register JsonAdapter's to use for specific types before building and returning 061 * the Jsonb instance to use. 062 * <p> 063 * Note that JsonAdapter's that are generated are automatically registered via service 064 * loading so there is no need to explicitly register those generated JsonAdapters. 065 * 066 * <pre>{@code 067 * 068 * Jsonb jsonb = Jsonb.newBuilder() 069 * .serializeNulls(true) 070 * .serializeEmpty(true) 071 * .failOnUnknown(true) 072 * .build(); 073 * 074 * }</pre> 075 */ 076 static Builder newBuilder() { 077 Iterator<Bootstrap> bootstrapService = ServiceLoader.load(Bootstrap.class).iterator(); 078 if (bootstrapService.hasNext()) { 079 return bootstrapService.next().newBuilder(); 080 } 081 return DefaultBootstrap.newBuilder(); 082 } 083 084 /** 085 * Return the JsonType used to read and write json for the given class. 086 */ 087 <T> JsonType<T> type(Class<T> cls); 088 089 /** 090 * Return the JsonType used to read and write json for the given type. 091 */ 092 <T> JsonType<T> type(Type type); 093 094 /** 095 * Return the JsonAdapter used to read and write json for the given class. 096 */ 097 <T> JsonAdapter<T> adapter(Class<T> cls); 098 099 /** 100 * Return the JsonAdapter used to read and write json for the given type. 101 */ 102 <T> JsonAdapter<T> adapter(Type type); 103 104 /** 105 * Return the JsonReader used to read the given json content. 106 */ 107 JsonReader reader(String json); 108 109 /** 110 * Return the JsonReader used to read the given json content in bytes. 111 */ 112 JsonReader reader(byte[] jsonBytes); 113 114 /** 115 * Return the JsonReader used to read the json content from the given reader. 116 */ 117 JsonReader reader(Reader reader); 118 119 /** 120 * Return the JsonReader used to read the json content from the given inputStream. 121 */ 122 JsonReader reader(InputStream inputStream); 123 124 /** 125 * Return the JsonWriter used to write json to the given writer. 126 */ 127 JsonWriter writer(Writer writer); 128 129 /** 130 * Return the JsonWriter used to write json to the given outputStream. 131 */ 132 JsonWriter writer(OutputStream outputStream); 133 134 /** 135 * Return the property names as PropertyNames. 136 * <p> 137 * Provides the option of optimising the writing of json for property names 138 * by having them already escaped and encoded rather than as plain strings. 139 */ 140 PropertyNames properties(String... names); 141 142 /** 143 * Build the Jsonb instance adding JsonAdapter, Factory or AdapterBuilder. 144 */ 145 interface Builder { 146 147 /** 148 * Set to serialise null values or not. 149 * <p> 150 * Default is to not serialise nulls. 151 */ 152 Builder serializeNulls(boolean serializeNulls); 153 154 /** 155 * Set to serialise empty collections or not. 156 * <p> 157 * Default is to not serialise empty collections. 158 */ 159 Builder serializeEmpty(boolean serializeEmpty); 160 161 /** 162 * Set failOnUnknown to true such that an exception is thrown when unknown 163 * properties are read in the json content. 164 */ 165 Builder failOnUnknown(boolean failOnUnknown); 166 167 /** 168 * Set to true for BigDecimal and BigInteger to serialise as String values rather than number values. 169 */ 170 Builder mathTypesAsString(boolean mathTypesAsString); 171 172 /** 173 * Add a JsonAdapter to use for the given type. 174 */ 175 <T> Builder add(Type type, JsonAdapter<T> jsonAdapter); 176 177 /** 178 * Add a AdapterBuilder which provides a JsonAdapter to use for the given type. 179 */ 180 Builder add(Type type, AdapterBuilder builder); 181 182 /** 183 * Add a Component which can provide multiple JsonAdapters and or configuration. 184 */ 185 Builder add(Jsonb.Component component); 186 187 /** 188 * Add a JsonAdapter.Factory which provides JsonAdapters to use. 189 */ 190 Builder add(JsonAdapter.Factory factory); 191 192 /** 193 * Build and return the Jsonb instance with all the given adapters and factories registered. 194 */ 195 Jsonb build(); 196 } 197 198 /** 199 * Function to build a JsonAdapter that needs Jsonb. 200 */ 201 @FunctionalInterface 202 interface AdapterBuilder { 203 204 /** 205 * Create a JsonAdapter given the Jsonb instance. 206 */ 207 JsonAdapter<?> build(Jsonb jsonb); 208 } 209 210 /** 211 * Components register JsonAdapters Jsonb.Builder 212 */ 213 @FunctionalInterface 214 interface Component { 215 216 /** 217 * Register JsonAdapters with the Builder. 218 */ 219 void register(Builder builder); 220 } 221}