Class WriteOptionsBuilder

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

public class WriteOptionsBuilder extends Object
Builder class for building the writeOptions.
Author:
John DeRegnaucourt ([email protected]), Kenny Partlow ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.*
  • Field Details

  • Constructor Details

    • WriteOptionsBuilder

      public WriteOptionsBuilder()
      Start with default options
  • Method Details

    • addPermanentAlias

      public static void addPermanentAlias(Class<?> clazz, String alias)
      Call this method to add a permanent (JVM lifetime) alias of a class to an often shorter, name.
      Parameters:
      clazz - Class that will be aliased by a shorter name in the JSON.
      alias - Shorter alias name, for example, "ArrayList" as opposed to "java.util.ArrayList"
    • addPermanentAlias

      public static void addPermanentAlias(Class<?> clazz)
      Call this method to add a permanent (JVM lifetime) alias of a class to an often shorter, name.
      Parameters:
      clazz - Class that will be aliased from fullyQualifiedName -> simpleName
    • addPermanentNonRef

      public static void addPermanentNonRef(Class<?> clazz)
      Call this method to add a permanent (JVM lifetime) class that should not be treated as referencable when being written out to JSON. This means it will never have an @id nor @ref. This feature is useful for small, immutable classes.
      Parameters:
      clazz - Class that will no longer be treated as referenceable when being written to JSON.
    • addPermanentWriter

      public static void addPermanentWriter(Class<?> clazz, JsonWriter.JsonClassWriter writer)
      Call this method to add a custom JSON writer to json-io. It will associate the Class 'c' to the writer you pass in. The writers are found with isAssignableFrom(). If this is too broad, causing too many classes to be associated to the custom writer, you can indicate that json-io should not use a custom write for a particular class, by calling the addNotCustomWrittenClass() method. This method will add the custom writer such that it will be there permanently, for the life of the JVM (static).
      Parameters:
      clazz - Class to assign a custom JSON writer to
      writer - The JsonClassWriter which will write the custom JSON format of class.
    • classLoader

      public WriteOptionsBuilder classLoader(ClassLoader loader)
      Parameters:
      loader - ClassLoader to use when writing JSON to resolve String named classes.
      Returns:
      WriteOptionsBuilder for chained access.
    • shortMetaKeys

      public WriteOptionsBuilder shortMetaKeys(boolean shortMetaKeys)
      Parameters:
      shortMetaKeys - boolean true to turn on short meta-keys, false for long.
      Returns:
      WriteOptionsBuilder for chained access.
    • aliasTypeNames

      public WriteOptionsBuilder aliasTypeNames(Map<String,String> aliases)
      Parameters:
      aliases - Map containing String class names to alias names. The passed in Map will be copied, and be the new baseline settings.
      Returns:
      WriteOptionsBuilder for chained access.
    • aliasTypeName

      public WriteOptionsBuilder aliasTypeName(Class<?> type)
      Aliases the fully qualified class name to its short name
      Parameters:
      type - Class to alias
      Returns:
      ReadOptions for chained access.
    • aliasTypeName

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

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

      public WriteOptionsBuilder withExtendedAliases()
      Returns:
      boolean true if set to always show type (@type)
    • showTypeInfoAlways

      public WriteOptionsBuilder showTypeInfoAlways()
      Set to always show type
      Returns:
      WriteOptionsBuilder for chained access.
    • showTypeInfoNever

      public WriteOptionsBuilder showTypeInfoNever()
      Set to never show type
      Returns:
      WriteOptionsBuilder for chained access.
    • showTypeInfoMinimal

      public WriteOptionsBuilder showTypeInfoMinimal()
      Set to show minimal type. This means that when the type of an object can be inferred, a type field will not be output. A Field that points to an instance of the same time, or a typed [] of objects don't need the type info. However, an Object[], a Collection with no generics, the reader will need to know what type the JSON object is, in order to instantiate the write Java class to which the information will be copied.
      Returns:
      WriteOptionsBuilder for chained access.
    • prettyPrint

      public WriteOptionsBuilder prettyPrint(boolean prettyPrint)
      Parameters:
      prettyPrint - boolean 'prettyPrint' setting, true to turn on, false will turn off.
      Returns:
      WriteOptionsBuilder for chained access.
    • writeLongsAsStrings

      public WriteOptionsBuilder writeLongsAsStrings(boolean writeLongsAsStrings)
      Parameters:
      writeLongsAsStrings - boolean true to turn on writing longs as Strings, false to write them as native JSON longs.
      Returns:
      WriteOptionsBuilder for chained access.
    • skipNullFields

      public WriteOptionsBuilder skipNullFields(boolean skipNullFields)
      Parameters:
      skipNullFields - boolean setting, where true indicates fields with null values will not be written to the JSON, false will allow the field to still be written.
      Returns:
      WriteOptionsBuilder for chained access.
    • forceMapOutputAsTwoArrays

      public WriteOptionsBuilder forceMapOutputAsTwoArrays(boolean forceMapOutputAsTwoArrays)
      Parameters:
      forceMapOutputAsTwoArrays - boolean 'forceMapOutputAsTwoArrays' setting. true will force Java Maps to be written out as two parallel arrays, once for keys, one array for values. false will write out one JSON { } object, if all keys are Strings. If not, then the Map will be output as two parallel arrays (@keys:[...], @values:[...]).
      Returns:
      WriteOptionsBuilder for chained access.
    • allowNanAndInfinity

      public WriteOptionsBuilder allowNanAndInfinity(boolean allowNanAndInfinity)
      Parameters:
      allowNanAndInfinity - boolean 'allowNanAndInfinity' setting. true will allow Double and Floats to be output as NAN and INFINITY, false and these values will come across as null.
      Returns:
      WriteOptionsBuilder for chained access.
    • writeEnumsAsString

      public WriteOptionsBuilder writeEnumsAsString()
      Option to write out enums as a String, it will write out the enum.name() field. This is the default way enums will be written out.
      Returns:
      WriteOptionsBuilder for chained access.per
    • writeEnumAsJsonObject

      public WriteOptionsBuilder writeEnumAsJsonObject(boolean writePublicFieldsOnly)
      Option to write out all the member fields of an enum. You can also filter the field to write out only the public fields on the enum.
      Parameters:
      writePublicFieldsOnly - boolean, only write out the public fields when writing enums as objects
      Returns:
      WriteOptionsBuilder for chained access.
    • closeStream

      public WriteOptionsBuilder closeStream(boolean closeStream)
      Parameters:
      closeStream - boolean set to 'true' to have JsonIo close the OutputStream when it is finished writinging to it. The default is 'true'. If false, the OutputStream will not be closed, allowing you to continue writing further. Example, NDJSON that has new line eliminated JSON objects repeated.
      Returns:
      WriteOptionsBuilder for chained access.
    • setCustomWrittenClasses

      public WriteOptionsBuilder setCustomWrittenClasses(Map<Class<?>,JsonWriter.JsonClassWriter> customWrittenClasses)
      Parameters:
      customWrittenClasses - Map of Class to JsonWriter.JsonClassWriter. Establish the passed in Map as the established Map of custom writers to be used when writing JSON. Using this method more than once, will set the custom writers to only the values from the Set in the last call made.
      Returns:
      WriteOptionsBuilder for chained access.
    • addCustomWrittenClasses

      public WriteOptionsBuilder addCustomWrittenClasses(Map<Class<?>,JsonWriter.JsonClassWriter> customWrittenClasses)
      Parameters:
      customWrittenClasses - Map of Class to JsonWriter.JsonClassWriter. Adds all custom writers into the custom writers map.
      Returns:
      WriteOptionsBuilder for chained access.
    • addCustomWrittenClass

      public WriteOptionsBuilder addCustomWrittenClass(Class<?> clazz, JsonWriter.JsonClassWriter customWriter)
      Parameters:
      clazz - Class to add a custom writer for.
      customWriter - JsonClassWriter to use when the passed in Class is encountered during serialization.
      Returns:
      WriteOptionsBuilder for chained access.
    • addNotCustomWrittenClass

      public WriteOptionsBuilder addNotCustomWrittenClass(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 writer (that could happen through inheritance).
      Parameters:
      notCustomClass - Class to add to the not-customized list.
      Returns:
      WriteOptionsBuilder for chained access.
    • setNotCustomWrittenClasses

      public WriteOptionsBuilder setNotCustomWrittenClasses(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:
      WriteOptionsBuilder for chained access.
    • addIncludedField

      public WriteOptionsBuilder addIncludedField(Class<?> clazz, String includedFieldName)
      Parameters:
      clazz - Class to add a single field to be included in the written JSON.
      includedFieldName - String name of field to include in written JSON.
      Returns:
      WriteOptionsBuilder for chained access.
    • addIncludedFields

      public WriteOptionsBuilder addIncludedFields(Class<?> clazz, Collection<String> includedFieldNames)
      Parameters:
      clazz - Class to add a Collection of fields to be included in written JSON.
      includedFieldNames - Collection of String name of fields to include in written JSON.
      Returns:
      WriteOptionsBuilder for chained access.
    • addIncludedFields

      public WriteOptionsBuilder addIncludedFields(Map<Class<?>,Collection<String>> includedFieldNames)
      Parameters:
      includedFieldNames - Map of Class's mapped to Collection of String field names to include in the written JSON.
      Returns:
      WriteOptionsBuilder for chained access.
    • addExcludedField

      public WriteOptionsBuilder addExcludedField(Class<?> clazz, String excludedFieldName)
      Parameters:
      clazz - Class to add a single field to be excluded.
      excludedFieldName - String name of field to exclude from written JSON.
      Returns:
      WriteOptionsBuilder for chained access.
    • addExcludedFields

      public WriteOptionsBuilder addExcludedFields(Class<?> clazz, Collection<String> excludedFields)
      Parameters:
      clazz - Class to add a Collection of fields to be excluded in written JSON.
      excludedFields - Collection of String name of fields to exclude in written JSON.
      Returns:
      WriteOptionsBuilder for chained access.
    • addExcludedFields

      public WriteOptionsBuilder addExcludedFields(Map<Class<?>,Collection<String>> excludedFieldNames)
      Parameters:
      excludedFieldNames - Map of Class's mapped to Collection of String field names to exclude from written JSON.
      Returns:
      WriteOptionsBuilder for chained access.
    • isoDateFormat

      public WriteOptionsBuilder isoDateFormat()
      Change the date-time format to the ISO date format: "yyyy-MM-dd". This is for java.util.Data and java.sql.Date.
      Returns:
      WriteOptionsBuilder for chained access.
    • isoDateTimeFormat

      public WriteOptionsBuilder isoDateTimeFormat()
      Change the date-time format to the ISO date-time format: "yyyy-MM-dd'T'HH:mm:ss" (default). This is for java.util.Date and java.sql.Date.
      Returns:
      WriteOptionsBuilder for chained access.
    • longDateFormat

      public WriteOptionsBuilder longDateFormat()
      Change the java.uti.Date and java.sql.Date format output to a "long," the number of seconds since Jan 1, 1970 at midnight. Useful if you do not need to see the JSON, and want to keep the format smaller. This is for java.util.Date and java.sql.Date.
      Returns:
      WriteOptionsBuilder for chained access.
    • dateTimeFormat

      public WriteOptionsBuilder dateTimeFormat(String format)
      Change the date-time format to the passed in format. The format pattens can be found in the Java Doc for the java.time.format.DateTimeFormatter class. There are many constants you can use, as well as the definition of how to construct your own patterns. This is for java.util.Date and java.sql.Date.
      Returns:
      WriteOptionsBuilder for chained access.
    • setFilteredMethodNames

      public WriteOptionsBuilder setFilteredMethodNames(Collection<String> methodNames)
      Parameters:
      methodNames - Replaces the collection of methodNames that are not to be considered as accessors.
      Returns:
      WriteOptionsBuilder for chained access.
    • addFilteredMethodNames

      public WriteOptionsBuilder addFilteredMethodNames(Collection<String> methodNames)
      Parameters:
      methodNames - Adds to the collection of methodNames that are not to be considered as accessors.
      Returns:
      WriteOptionsBuilder for chained access.
    • addFilteredMethodName

      public WriteOptionsBuilder addFilteredMethodName(String methodName)
      Parameters:
      methodName - Adds to the collection of methodNames that are not to be considered as accessors.
      Returns:
      WriteOptionsBuilder for chained access.
    • addNonStandardMappings

      public WriteOptionsBuilder addNonStandardMappings(Map<Class<?>,Map<String,String>> nonStandardMappings)
      Parameters:
      nonStandardMappings - Adds to the collection of methodNames that are not to be considered as accessors.
      Returns:
      WriteOptionsBuilder for chained access.
    • addNonStandardMapping

      public WriteOptionsBuilder addNonStandardMapping(Class<?> c, String fieldName, String methodName)
      Parameters:
      methodName - Adds to the collection of methodNames that are not to be considered as accessors.
      Returns:
      WriteOptionsBuilder for chained access.
    • setNonReferenceableClasses

      public WriteOptionsBuilder setNonReferenceableClasses(Collection<Class<?>> classes)
      Parameters:
      classes - Replaces the collection of classes to treat as non-referenceable with the given class.
      Returns:
      WriteOptionsBuilder for chained access.
    • addNonReferenceableClasses

      public WriteOptionsBuilder addNonReferenceableClasses(Collection<Class<?>> classes)
      Parameters:
      classes - classes 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:
      WriteOptionsBuilder for chained access.
    • addNonReferenceableClass

      public WriteOptionsBuilder 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:
      WriteOptionsBuilder for chained access.
    • build

      public WriteOptions build()
      Seal the instance of this class so that no more changes can be made to it.
      Returns:
      WriteOptionsBuilder for chained access.
    • addFilter

      public boolean addFilter(FieldFilter filter)
    • removeFilter

      public boolean removeFilter(FieldFilter filter)