Package com.dslplatform.json
Class DslJson.Settings<TContext>
java.lang.Object
com.dslplatform.json.DslJson.Settings<TContext>
- Type Parameters:
TContext- DslJson context
Configuration for DslJson options.
By default key cache is enabled. Everything else is not configured.
To load `META-INF/services` call `includeServiceLoader()`
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate final List<DslJson.ConverterFactory<JsonReader.BindObject>> private final Set<ClassLoader> private final List<Configuration> private TContextprivate final Map<Class<? extends Annotation>, Boolean> private JsonReader.DoublePrecisionprivate JsonReader.ErrorInfoprivate DslJson.Fallback<TContext> private intprivate booleanprivate StringCacheprivate intprivate intprivate booleanprivate final List<DslJson.ConverterFactory<JsonReader.ReadObject>> private JsonReader.UnknownNumberParsingprivate StringCacheprivate final List<DslJson.ConverterFactory<JsonWriter.WriteObject>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionallowArrayFormat(boolean allowArrayFormat) Some encoders/decoders support writing objects in array format.creatorMarker(Class<? extends Annotation> marker, boolean expandVisibility) When there are multiple constructors, pick the one marked with annotation.doublePrecision(JsonReader.DoublePrecision precision) By default doubles are not deserialized into an exact value in some rare edge cases.errorInfo(JsonReader.ErrorInfo errorInfo) By default doubles are not deserialized into an exact value in some rare edge cases.fallbackTo(DslJson.Fallback<TContext> fallback) Deprecated.Load converters using thread local ClassLoader.includeServiceLoader(ClassLoader loader) Load converters using provided `ClassLoader` instance Will scan through `META-INF/services/com.dslplatform.json.Configuration` file and register implementation during startup.limitDigitsBuffer(int size) Specify maximum allowed size for digits buffer.limitStringBuffer(int size) Specify maximum allowed size for string buffer.resolveBinder(DslJson.ConverterFactory<? extends JsonReader.BindObject> binder) DslJson will iterate over converter factories when requested type is unknown.resolveReader(DslJson.ConverterFactory<? extends JsonReader.ReadObject> reader) DslJson will iterate over converter factories when requested type is unknown.resolveWriter(DslJson.ConverterFactory<? extends JsonWriter.WriteObject> writer) DslJson will iterate over converter factories when requested type is unknown.skipDefaultValues(boolean omitDefaults) DslJson can exclude some properties from resulting JSON which it can reconstruct fully from schema information.unknownNumbers(JsonReader.UnknownNumberParsing unknownNumbers) When processing JSON without a schema numbers can be deserialized in various ways: - as longs and decimals - as longs and doubles - as decimals only - as doubles only Default is as long and BigDecimaluseKeyCache(StringCache keyCache) Use specific key cache implementation.useStringValuesCache(StringCache valuesCache) Use specific string values cache implementation.with(Configuration conf) Configure DslJson with custom Configuration during startup.private DslJson.Settings<TContext> with(Iterable<Configuration> confs) withContext(TContext context) Pass in context for DslJson.withJavaConverters(boolean javaSpecifics) Enable converters for Java specific types (Graphics API) not available on Android.
-
Field Details
-
context
-
javaSpecifics
private boolean javaSpecifics -
fallback
-
omitDefaults
private boolean omitDefaults -
allowArrayFormat
private boolean allowArrayFormat -
keyCache
-
valuesCache
-
fromServiceLoader
private int fromServiceLoader -
errorInfo
-
doublePrecision
-
unknownNumbers
-
maxNumberDigits
private int maxNumberDigits -
maxStringBuffer
private int maxStringBuffer -
configurations
-
writerFactories
-
readerFactories
-
binderFactories
-
classLoaders
-
creatorMarkers
-
-
Constructor Details
-
Settings
public Settings()
-
-
Method Details
-
withContext
Pass in context for DslJson. Context will be available in JsonReader for objects which needs it.- Parameters:
context- context propagated to JsonReaders- Returns:
- itself
-
withJavaConverters
Enable converters for Java specific types (Graphics API) not available on Android.- Parameters:
javaSpecifics- should register Java specific converters- Returns:
- itself
-
fallbackTo
@Deprecated public DslJson.Settings<TContext> fallbackTo(@Nullable DslJson.Fallback<TContext> fallback) Deprecated.Will be eventually replaced with writer/reader factories. Used by DslJson to call into when trying to serialize/deserialize object which is not supported.- Parameters:
fallback- how to handle unsupported type- Returns:
- which fallback to use in case of unsupported type
-
skipDefaultValues
DslJson can exclude some properties from resulting JSON which it can reconstruct fully from schema information. Eg. int with value 0 can be omitted since that is default value for the type. Null values can be excluded since they are handled the same way as missing property.- Parameters:
omitDefaults- should exclude default values from resulting JSON- Returns:
- itself
-
allowArrayFormat
Some encoders/decoders support writing objects in array format. For encoder to write objects in such format, Array format must be defined before the Default and minified formats and array format must be allowed via this setting. If objects support multiple formats decoding will work regardless of this setting.- Parameters:
allowArrayFormat- allow serialization via array format- Returns:
- itself
-
useKeyCache
Use specific key cache implementation. Key cache is enabled by default and it's used when deserializing unstructured objects such as Map<String, Object> to avoid allocating new String key instance. Instead StringCache will provide a new or an old instance. This improves memory usage and performance since there is usually small number of keys. It does have some performance overhead, but this is dependant on the implementation.To disable key cache, provide null for it.
- Parameters:
keyCache- which key cache to use- Returns:
- itself
-
useStringValuesCache
Use specific string values cache implementation. By default string values cache is disabled.To support memory restricted scenarios where there is limited number of string values, values cache can be used.
Not every "JSON string" will use this cache... eg UUID, LocalDate don't create an instance of string and therefore don't use this cache.
- Parameters:
valuesCache- which values cache to use- Returns:
- itself
-
resolveWriter
public DslJson.Settings<TContext> resolveWriter(DslJson.ConverterFactory<? extends JsonWriter.WriteObject> writer) DslJson will iterate over converter factories when requested type is unknown. Registering writer converter factory allows for constructing JSON converter lazily.- Parameters:
writer- registered writer factory- Returns:
- itself
-
resolveReader
public DslJson.Settings<TContext> resolveReader(DslJson.ConverterFactory<? extends JsonReader.ReadObject> reader) DslJson will iterate over converter factories when requested type is unknown. Registering reader converter factory allows for constructing JSON converter lazily.- Parameters:
reader- registered reader factory- Returns:
- itself
-
resolveBinder
public DslJson.Settings<TContext> resolveBinder(DslJson.ConverterFactory<? extends JsonReader.BindObject> binder) DslJson will iterate over converter factories when requested type is unknown. Registering binder converter factory allows for constructing JSON converter lazily.- Parameters:
binder- registered binder factory- Returns:
- itself
-
includeServiceLoader
Load converters using thread local ClassLoader. Will scan through `META-INF/services/com.dslplatform.json.Configuration` file and register implementation during startup. This will pick up compile time databindings if they are available in specific folder.Note that gradle on Android has issues with preserving that file, in which case it can be provided manually. DslJson will fall back to "expected" class name if it doesn't find anything during scanning.
- Returns:
- itself
-
includeServiceLoader
Load converters using provided `ClassLoader` instance Will scan through `META-INF/services/com.dslplatform.json.Configuration` file and register implementation during startup. This will pick up compile time databindings if they are available in specific folder.Note that gradle on Android has issues with preserving that file, in which case it can be provided manually. DslJson will fall back to "expected" class name if it doesn't find anything during scanning.
- Parameters:
loader- ClassLoader to use- Returns:
- itself
-
errorInfo
By default doubles are not deserialized into an exact value in some rare edge cases.- Parameters:
errorInfo- information about error in parsing exception- Returns:
- itself
-
doublePrecision
By default doubles are not deserialized into an exact value in some rare edge cases.- Parameters:
precision- type of double deserialization- Returns:
- itself
-
unknownNumbers
When processing JSON without a schema numbers can be deserialized in various ways: - as longs and decimals - as longs and doubles - as decimals only - as doubles only Default is as long and BigDecimal- Parameters:
unknownNumbers- how to deserialize numbers without a schema- Returns:
- itself
-
limitDigitsBuffer
Specify maximum allowed size for digits buffer. Default is 512. Digits buffer is used when processing strange/large input numbers.- Parameters:
size- maximum allowed size for digit buffer- Returns:
- itself
-
limitStringBuffer
Specify maximum allowed size for string buffer. Default is 128MB To protect against malicious inputs, maximum allowed string buffer can be reduced.- Parameters:
size- maximum size of buffer in bytes- Returns:
- itself
-
creatorMarker
public DslJson.Settings<TContext> creatorMarker(Class<? extends Annotation> marker, boolean expandVisibility) When there are multiple constructors, pick the one marked with annotation. When markers is allowed on non public targets, attempt at visibility change will be done in runtime.- Parameters:
marker- annotation used for marking constructor or static method factoryexpandVisibility- should consider annotation declared on non public accessor- Returns:
- itself
-
with
Configure DslJson with custom Configuration during startup. Configurations are extension points for setting up readers/writers during DslJson initialization.- Parameters:
conf- custom extensibility point- Returns:
- itself
-
with
-