Class ReadOptionsBuilder

java.lang.Object
com.cedarsoftware.util.io.ReadOptionsBuilder

public class ReadOptionsBuilder extends Object
  • Constructor Details

    • ReadOptionsBuilder

      public ReadOptionsBuilder()
      Start with default options
  • Method Details

    • addPermanentClassFactory

      public 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. 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

      public 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. 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

      public 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.
      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

      public static void addPermanentReader(Class<?> c, JsonReader.JsonClassReader reader)
      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 to
      reader - The JsonClassReader which will read the custom JSON format of 'c'
    • assignInstantiator

      public static void assignInstantiator(Class<?> classToCreate, JsonReader.ClassFactory factory)
      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

      public ReadOptions build()
      Returns:
      ReadOptions - built options
    • addNotCustomReaderClass

      public ReadOptionsBuilder 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).
      Parameters:
      notCustomClass - Class to add to the not-customized list.
      Returns:
      ReadOptionsBuilder for chained access.
    • replaceNotCustomReaderClasses

      public ReadOptionsBuilder replaceNotCustomReaderClasses(Collection<Class<?>> notCustomClasses)
      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

      public ReadOptionsBuilder addClassFactory(Class<?> clazz, JsonReader.ClassFactory factory)
      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

      public ReadOptionsBuilder 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

      public ReadOptionsBuilder 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

      public ReadOptionsBuilder classLoader(ClassLoader classLoader)
      Parameters:
      classLoader - ClassLoader to use when reading JSON to resolve String named classes.
      Returns:
      ReadOptionsBuilder for chained access.
    • failOnUnknownType

      public ReadOptionsBuilder failOnUnknownType(boolean fail)
      Parameters:
      fail - boolean indicating whether we should fail on unknown type encountered.
      Returns:
      ReadOptionsBuilder for chained access.
    • unknownTypeClass

      public ReadOptionsBuilder unknownTypeClass(Class<?> c)
      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

      public ReadOptionsBuilder closeStream(boolean 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

      public ReadOptionsBuilder maxDepth(int 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

      public ReadOptionsBuilder allowNanAndInfinity(boolean 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

      public ReadOptionsBuilder aliasTypeNames(Map<String,String> 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

      public ReadOptionsBuilder aliasTypeName(Class<?> type, String alias)
      Parameters:
      type - String class name
      alias - String shorter name to use, typically.
      Returns:
      ReadOptionsBuilder for chained access.
    • aliasTypeName

      public ReadOptionsBuilder aliasTypeName(String typeName, String alias)
      Parameters:
      typeName - String class name
      alias - String shorter name to use, typically.
      Returns:
      ReadOptionsBuilder for chained access.
    • setLocale

      public ReadOptionsBuilder setLocale(Locale locale)
      Parameters:
      locale - target locale for conversions
      Returns:
      ReadOptionsBuilder for chained access.
    • setCharset

      public ReadOptionsBuilder setCharset(Charset charset)
      Parameters:
      charset - source charset for conversions
      Returns:
      ReadOptionsBuilder for chained access.
    • setZoneId

      public ReadOptionsBuilder setZoneId(ZoneId zoneId)
      Parameters:
      zoneId - source charset for conversions
      Returns:
      ReadOptionsBuilder for chained access.
    • setTrueCharacter

      public ReadOptionsBuilder setTrueCharacter(Character ch)
      Parameters:
      ch - Character used when converting true -> char
      Returns:
      ReadOptionsBuilder for chained access.
    • setFalseCharacter

      public ReadOptionsBuilder setFalseCharacter(Character ch)
      Parameters:
      ch - Character used when converting false -> char
      Returns:
      ReadOptionsBuilder for chained access.
    • coerceClass

      public ReadOptionsBuilder coerceClass(String sourceClassName, Class<?> destClass)
      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

      public ReadOptionsBuilder missingFieldHandler(JsonReader.MissingFieldHandler 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

      public ReadOptionsBuilder addNonReferenceableClass(Class<?> clazz)
      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.