Package com.cedarsoftware.util.io
Class WriteOptionsBuilder
java.lang.Object
com.cedarsoftware.util.io.WriteOptionsBuilder
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.
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddCustomWrittenClass
(Class<?> clazz, JsonWriter.JsonClassWriter customWriter) addCustomWrittenClasses
(Map<Class<?>, JsonWriter.JsonClassWriter> customWrittenClasses) addExcludedField
(Class<?> clazz, String excludedFieldName) addExcludedFields
(Class<?> clazz, Collection<String> excludedFields) addExcludedFields
(Map<Class<?>, Collection<String>> excludedFieldNames) boolean
addFilter
(FieldFilter filter) addFilteredMethodName
(String methodName) addFilteredMethodNames
(Collection<String> methodNames) addIncludedField
(Class<?> clazz, String includedFieldName) addIncludedFields
(Class<?> clazz, Collection<String> includedFieldNames) addIncludedFields
(Map<Class<?>, Collection<String>> includedFieldNames) addNonReferenceableClass
(Class<?> clazz) addNonReferenceableClasses
(Collection<Class<?>> classes) addNonStandardMapping
(Class<?> c, String fieldName, String methodName) 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).static void
addPermanentAlias
(Class<?> clazz) Call this method to add a permanent (JVM lifetime) alias of a class to an often shorter, name.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.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.static void
addPermanentWriter
(Class<?> clazz, JsonWriter.JsonClassWriter writer) Call this method to add a custom JSON writer to json-io.aliasTypeName
(Class<?> type) Aliases the fully qualified class name to its short namealiasTypeName
(Class<?> type, String alias) aliasTypeName
(String typeName, String alias) aliasTypeNames
(Map<String, String> aliases) allowNanAndInfinity
(boolean allowNanAndInfinity) build()
Seal the instance of this class so that no more changes can be made to it.classLoader
(ClassLoader loader) closeStream
(boolean closeStream) dateTimeFormat
(String format) Change the date-time format to the passed in format.forceMapOutputAsTwoArrays
(boolean forceMapOutputAsTwoArrays) Change the date-time format to the ISO date format: "yyyy-MM-dd".Change the date-time format to the ISO date-time format: "yyyy-MM-dd'T'HH:mm:ss" (default).Change the java.uti.Date and java.sql.Date format output to a "long," the number of seconds since Jan 1, 1970 at midnight.prettyPrint
(boolean prettyPrint) boolean
removeFilter
(FieldFilter filter) setCustomWrittenClasses
(Map<Class<?>, JsonWriter.JsonClassWriter> customWrittenClasses) setFilteredMethodNames
(Collection<String> methodNames) setNonReferenceableClasses
(Collection<Class<?>> classes) setNotCustomWrittenClasses
(Collection<Class<?>> notCustomClasses) shortMetaKeys
(boolean shortMetaKeys) Set to always show typeSet to show minimal type.Set to never show typeskipNullFields
(boolean skipNullFields) writeEnumAsJsonObject
(boolean writePublicFieldsOnly) Option to write out all the member fields of an enum.Option to write out enums as a String, it will write out the enum.name() field.writeLongsAsStrings
(boolean writeLongsAsStrings)
-
Field Details
-
ISO_DATE_FORMAT
- See Also:
-
ISO_DATE_TIME_FORMAT
- See Also:
-
-
Constructor Details
-
WriteOptionsBuilder
public WriteOptionsBuilder()Start with default options
-
-
Method Details
-
addPermanentAlias
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
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
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
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 towriter
- The JsonClassWriter which will write the custom JSON format of class.
-
classLoader
- Parameters:
loader
- ClassLoader to use when writing JSON to resolve String named classes.- Returns:
- WriteOptionsBuilder for chained access.
-
shortMetaKeys
- Parameters:
shortMetaKeys
- boolean true to turn on short meta-keys, false for long.- Returns:
- WriteOptionsBuilder for chained access.
-
aliasTypeNames
- 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
Aliases the fully qualified class name to its short name- Parameters:
type
- Class to alias- Returns:
- ReadOptions for chained access.
-
aliasTypeName
- Parameters:
type
- Class to aliasalias
- String shorter name to use, typically.- Returns:
- ReadOptions for chained access.
-
aliasTypeName
- Parameters:
typeName
- String class namealias
- String shorter name to use, typically.- Returns:
- WriteOptionsBuilder for chained access.
-
withExtendedAliases
- Returns:
- boolean true if set to always show type (@type)
-
showTypeInfoAlways
Set to always show type- Returns:
- WriteOptionsBuilder for chained access.
-
showTypeInfoNever
Set to never show type- Returns:
- WriteOptionsBuilder for chained access.
-
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
- Parameters:
prettyPrint
- boolean 'prettyPrint' setting, true to turn on, false will turn off.- Returns:
- WriteOptionsBuilder for chained access.
-
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
- 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
- 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
- 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
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
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
- 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
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
- 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
- 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
- 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
- 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
- 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
- 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
- Parameters:
excludedFieldNames
- Map of Class's mapped to Collection of String field names to exclude from written JSON.- Returns:
- WriteOptionsBuilder for chained access.
-
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
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
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
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
- Parameters:
methodNames
- Replaces the collection of methodNames that are not to be considered as accessors.- Returns:
- WriteOptionsBuilder for chained access.
-
addFilteredMethodNames
- Parameters:
methodNames
- Adds to the collection of methodNames that are not to be considered as accessors.- Returns:
- WriteOptionsBuilder for chained access.
-
addFilteredMethodName
- 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
- Parameters:
methodName
- Adds to the collection of methodNames that are not to be considered as accessors.- Returns:
- WriteOptionsBuilder for chained access.
-
setNonReferenceableClasses
- Parameters:
classes
- Replaces the collection of classes to treat as non-referenceable with the given class.- Returns:
- WriteOptionsBuilder for chained access.
-
addNonReferenceableClasses
- 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
- 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
Seal the instance of this class so that no more changes can be made to it.- Returns:
- WriteOptionsBuilder for chained access.
-
addFilter
-
removeFilter
-