Class DslJson.Settings<TContext>

  • Type Parameters:
    TContext - DslJson context
    Enclosing class:
    DslJson<TContext>

    public static class DslJson.Settings<TContext>
    extends java.lang.Object
    Configuration for DslJson options. By default key cache is enabled. Everything else is not configured. To load `META-INF/services` call `includeServiceLoader()`
    • Constructor Detail

      • Settings

        public Settings()
    • Method Detail

      • withContext

        public DslJson.Settings<TContext> withContext​(@Nullable
                                                      TContext context)
        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

        public DslJson.Settings<TContext> withJavaConverters​(boolean javaSpecifics)
        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

        public DslJson.Settings<TContext> skipDefaultValues​(boolean omitDefaults)
        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

        public DslJson.Settings<TContext> allowArrayFormat​(boolean 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

        public DslJson.Settings<TContext> useKeyCache​(@Nullable
                                                      StringCache keyCache)
        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

        public DslJson.Settings<TContext> useStringValuesCache​(@Nullable
                                                               StringCache valuesCache)
        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

        public DslJson.Settings<TContext> 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

        public DslJson.Settings<TContext> includeServiceLoader​(java.lang.ClassLoader loader)
        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

        public DslJson.Settings<TContext> errorInfo​(JsonReader.ErrorInfo 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

        public DslJson.Settings<TContext> doublePrecision​(JsonReader.DoublePrecision precision)
        By default doubles are not deserialized into an exact value in some rare edge cases.
        Parameters:
        precision - type of double deserialization
        Returns:
        itself
      • unknownNumbers

        public DslJson.Settings<TContext> 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 BigDecimal
        Parameters:
        unknownNumbers - how to deserialize numbers without a schema
        Returns:
        itself
      • limitDigitsBuffer

        public DslJson.Settings<TContext> limitDigitsBuffer​(int size)
        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

        public DslJson.Settings<TContext> limitStringBuffer​(int size)
        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​(java.lang.Class<? extends java.lang.annotation.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 factory
        expandVisibility - should consider annotation declared on non public accessor
        Returns:
        itself
      • with

        public DslJson.Settings<TContext> with​(Configuration conf)
        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