Package com.cedarsoftware.util.io
Class ReadOptionsBuilder
java.lang.Object
com.cedarsoftware.util.io.ReadOptionsBuilder
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddClassFactory
(Class<?> clazz, JsonReader.ClassFactory factory) Associate a ClassFactory to a Class that needs help being constructed and read in.addConverterOverride
(Class<?> source, Class<?> target, com.cedarsoftware.util.convert.Convert<?> conversionFunction) addCustomReaderClass
(Class<?> clazz, JsonReader.JsonClassReader customReader) addNonReferenceableClass
(Class<?> clazz) addNotCustomReaderClass
(Class<?> notCustomClass) Add a class to the not-customized list - the list of classes that you do not want to be picked up by a custom reader (that could happen through inheritance).static void
addPermanentAlias
(Class<?> sourceClass, String alias) Call this method to add a permanent (JVM lifetime) alias of a class to an often shorter, name.static void
addPermanentClassFactory
(Class<?> sourceClass, JsonReader.ClassFactory factory) Call this method to add a permanent (JVM lifetime) coercion of one class to another during instance creation.static void
addPermanentCoercedType
(Class<?> sourceClass, Class<?> destinationClass) Call this method to add a permanent (JVM lifetime) coercion of one class to another during instance creation.static void
addPermanentReader
(Class<?> c, JsonReader.JsonClassReader reader) Call this method to add a custom JSON reader to json-io.aliasTypeName
(Class<?> type, String alias) aliasTypeName
(String typeName, String alias) aliasTypeNames
(Map<String, String> aliasTypeNames) allowNanAndInfinity
(boolean allowNanAndInfinity) static void
assignInstantiator
(Class<?> classToCreate, JsonReader.ClassFactory factory) build()
classLoader
(ClassLoader classLoader) closeStream
(boolean closeStream) coerceClass
(String sourceClassName, Class<?> destClass) Coerce a class from one type (named in the JSON) to another type.failOnUnknownType
(boolean fail) maxDepth
(int maxDepth) missingFieldHandler
(JsonReader.MissingFieldHandler missingFieldHandler) replaceClassFactories
(Map<Class<?>, ? extends JsonReader.ClassFactory> factories) Associate multiple ClassFactory instances to Classes that needs help being constructed and read in.replaceCustomReaderClasses
(Map<? extends Class<?>, ? extends JsonReader.JsonClassReader> customReaderClasses) replaceNotCustomReaderClasses
(Collection<Class<?>> notCustomClasses) Return as JAVA_OBJECT's the returned value will be of the class type passed into JsonReader.toJava(json, rootClass).Set to return as JSON_OBJECTS's (faster, useful for large, more simple object data sets).setCharset
(Charset charset) unknownTypeClass
(Class<?> c) Set a class to use when the JSON reader cannot instantiate a class.
-
Constructor Details
-
ReadOptionsBuilder
public ReadOptionsBuilder()Start with default options
-
-
Method Details
-
addPermanentClassFactory
Call this method to add a permanent (JVM lifetime) coercion of one class to another during instance creation. Examples of classes that might need this are proxied classes such as HibernateBags, etc. That you want to create as regular jvm collections.- Parameters:
sourceClass
- String class name (fully qualified name) that will be coerced to another type.factory
- JsonReader.ClassFactory instance which can create the sourceClass and load it's contents, using passed in JsonValues (JsonObject or JsonArray).
-
addPermanentCoercedType
Call this method to add a permanent (JVM lifetime) coercion of one class to another during instance creation. Examples of classes that might need this are proxied classes such as HibernateBags, etc. That you want to create as regular jvm collections.- Parameters:
sourceClass
- String class name (fully qualified name) that will be coerced to another type.destinationClass
- Class to coerce to. For example, java.util.Collections$EmptyList to java.util.ArrayList.
-
addPermanentAlias
Call this method to add a permanent (JVM lifetime) alias of a class to an often shorter, name.- Parameters:
sourceClass
- String class name (fully qualified name) that will be aliased by a shorter name in the JSON.alias
- Shorter alias name, for example, "ArrayList" as opposed to "java.util.ArrayList"
-
addPermanentReader
Call this method to add a custom JSON reader to json-io. It will associate the Class 'c' to the reader you pass in. The readers are found with isAssignableFrom(). If this is too broad, causing too many classes to be associated to the custom reader, you can indicate that json-io should not use a custom reader for a particular class, by calling the addNotCustomReader() method. This method will add the custom reader such that it will be there permanently, for the life of the JVM (static).- Parameters:
c
- Class to assign a custom JSON reader toreader
- The JsonClassReader which will read the custom JSON format of 'c'
-
assignInstantiator
- Parameters:
classToCreate
- Class that will need to use a JsonReader.ClassFactory for instantiation and loading.factory
- JsonReader.ClassFactory instance that has been created to instantiate a difficult to create and load class (class c).
-
build
- Returns:
- ReadOptions - built options
-
addNotCustomReaderClass
Add a class to the not-customized list - the list of classes that you do not want to be picked up by a custom reader (that could happen through inheritance).- Parameters:
notCustomClass
- Class to add to the not-customized list.- Returns:
- ReadOptionsBuilder for chained access.
-
replaceNotCustomReaderClasses
- Parameters:
notCustomClasses
- initialize the list of classes on the non-customized list. All prior associations will be dropped and this Collection will establish the new list.- Returns:
- ReadOptionsBuilder for chained access.
-
addConverterOverride
public ReadOptionsBuilder addConverterOverride(Class<?> source, Class<?> target, com.cedarsoftware.util.convert.Convert<?> conversionFunction) - Parameters:
source
- source class.target
- target class.conversionFunction
- functional interface to run Conversion.- Returns:
- ReadOptionsBuilder for chained access.
-
replaceCustomReaderClasses
public ReadOptionsBuilder replaceCustomReaderClasses(Map<? extends Class<?>, ? extends JsonReader.JsonClassReader> customReaderClasses) - Parameters:
customReaderClasses
- Map of Class to JsonReader.JsonClassReader. Establish the passed in Map as the established Map of custom readers to be used when reading JSON. Using this method more than once, will set the custom readers to only the values from the Set in the last call made.- Returns:
- ReadOptionsBuilder for chained access.
-
addCustomReaderClass
public ReadOptionsBuilder addCustomReaderClass(Class<?> clazz, JsonReader.JsonClassReader customReader) - Parameters:
clazz
- Class to add a custom reader for.customReader
- JsonClassReader to use when the passed in Class is encountered during serialization. Custom readers are passed an empty instance. Use a ClassFactory if you want to instantiate and load the class in one step.- Returns:
- ReadOptionsBuilder for chained access.
-
replaceClassFactories
public ReadOptionsBuilder replaceClassFactories(Map<Class<?>, ? extends JsonReader.ClassFactory> factories) Associate multiple ClassFactory instances to Classes that needs help being constructed and read in.- Parameters:
factories
- Map of entries that are Class to Factory. The Factory class knows how to instantiate and load the class associated to it.- Returns:
- ReadOptionsBuilder for chained access.
-
addClassFactory
Associate a ClassFactory to a Class that needs help being constructed and read in.- Parameters:
clazz
- Class that is difficult to instantiate.factory
- Class written to instantiate the 'clazz' and load it's values.- Returns:
- ReadOptionsBuilder for chained access.
-
returnAsNativeJsonObjects
Set to return as JSON_OBJECTS's (faster, useful for large, more simple object data sets). This returns as native json types (Map, Object[], long, double, boolean) -
returnAsJavaObjects
Return as JAVA_OBJECT's the returned value will be of the class type passed into JsonReader.toJava(json, rootClass). This mode is good for cloning exact objects. -
classLoader
- Parameters:
classLoader
- ClassLoader to use when reading JSON to resolve String named classes.- Returns:
- ReadOptionsBuilder for chained access.
-
failOnUnknownType
- Parameters:
fail
- boolean indicating whether we should fail on unknown type encountered.- Returns:
- ReadOptionsBuilder for chained access.
-
unknownTypeClass
Set a class to use when the JSON reader cannot instantiate a class. When that happens, it will throw a JsonIoException() if no 'unknownTypeClass' is set, otherwise it will create the instance of the class specified as the 'unknownTypeClass.' A common one to use, for example, if java.util.LinkedHashMap.- Parameters:
c
- Class to instantiate when a String specified class in the JSON cannot be instantiated. Set to null if you want a JsonIoException to be thrown when this happens.- Returns:
- ReadOptionsBuilder for chained access.
-
closeStream
- Parameters:
closeStream
- boolean set to 'true' to have JsonIo close the InputStream when it is finished reading from it. The default is 'true'. If false, the InputStream will not be closed, allowing you to continue reading further. Example, NDJSON that has new line eliminated JSON objects repeated.- Returns:
- ReadOptionsBuilder for chained access.
-
maxDepth
- Parameters:
maxDepth
- int maximum of the depth that JSON Objects {...} can be nested. Set this to prevent security risk from StackOverflow attack vectors.- Returns:
- ReadOptionsBuilder for chained access.
-
allowNanAndInfinity
- Parameters:
allowNanAndInfinity
- boolean 'allowNanAndInfinity' setting. true will allow Double and Floats to be read in as NaN and +Inf, -Inf [infinity], false and a JsonIoException will be thrown if these values are encountered- Returns:
- ReadOptionsBuilder for chained access.
-
aliasTypeNames
- Parameters:
aliasTypeNames
- Map containing String class names to alias names. The passed in Map will be copied, and be the new baseline settings.- Returns:
- ReadOptionsBuilder for chained access.
-
aliasTypeName
- Parameters:
type
- String class namealias
- String shorter name to use, typically.- Returns:
- ReadOptionsBuilder for chained access.
-
aliasTypeName
- Parameters:
typeName
- String class namealias
- String shorter name to use, typically.- Returns:
- ReadOptionsBuilder for chained access.
-
setLocale
- Parameters:
locale
- target locale for conversions- Returns:
- ReadOptionsBuilder for chained access.
-
setCharset
- Parameters:
charset
- source charset for conversions- Returns:
- ReadOptionsBuilder for chained access.
-
setZoneId
- Parameters:
zoneId
- source charset for conversions- Returns:
- ReadOptionsBuilder for chained access.
-
setTrueCharacter
- Parameters:
ch
- Character used when converting true -> char- Returns:
- ReadOptionsBuilder for chained access.
-
setFalseCharacter
- Parameters:
ch
- Character used when converting false -> char- Returns:
- ReadOptionsBuilder for chained access.
-
coerceClass
Coerce a class from one type (named in the JSON) to another type. For example, converting "java.util.Collections$SingletonSet" to a LinkedHashSet.class.- Parameters:
sourceClassName
- String class name to coerce to another type.destClass
- Class to coerce into.- Returns:
- ReadOptionsBuilder for chained access.
-
missingFieldHandler
- Parameters:
missingFieldHandler
- JsonReader.MissingFieldHandler implementation to call. This method will be called when a field in the JSON is read in, yet there is no corresponding field on the destination object to receive the value.- Returns:
- ReadOptionsBuilder for chained access.
-
addNonReferenceableClass
- Parameters:
clazz
- class to add to be considered a non-referenceable object. Just like an "int" for example, any class added here will never use an @id/@ref pair. The downside, is that when read, each instance, even if the same as another, will use memory.- Returns:
- ReadOptionsBuilder for chained access.
-