Class JsonGenerator.Options
- java.lang.Object
-
- groovy.json.JsonGenerator.Options
-
- Enclosing interface:
- JsonGenerator
public static class JsonGenerator.Options extends java.lang.ObjectA builder used to construct aJsonGeneratorinstance that allows control over the serialized JSON output. If you do not need to customize the output it is recommended to use the staticJsonOutput.toJsonmethods.Example:
def generator = new groovy.json.JsonGenerator.Options() .excludeNulls() .dateFormat('yyyy') .excludeFieldsByName('bar', 'baz') .excludeFieldsByType(java.sql.Date) .build() def input = [foo: null, lastUpdated: Date.parse('yyyy-MM-dd', '2014-10-24'), bar: 'foo', baz: 'foo', systemDate: new java.sql.Date(new Date().getTime())] assert generator.toJson(input) == '{"lastUpdated":"2014"}'- Since:
- 2.5.0
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Set<JsonGenerator.Converter>convertersprotected java.lang.StringdateFormatprotected java.util.LocaledateLocaleprotected static java.lang.StringDEFAULT_TIMEZONEprotected booleandisableUnicodeEscapingprotected java.util.Set<java.lang.String>excludedFieldNamesprotected java.util.Set<java.lang.Class<?>>excludedFieldTypesprotected booleanexcludeNullsprotected static java.lang.StringJSON_DATE_FORMATprotected static java.util.LocaleJSON_DATE_FORMAT_LOCALEprotected java.util.TimeZonetimezone
-
Constructor Summary
Constructors Constructor Description Options()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonGenerator.OptionsaddConverter(JsonGenerator.Converter converter)Registers a converter that will be called when a type it handles is encountered.<T> JsonGenerator.OptionsaddConverter(java.lang.Class<T> type, Closure<?> closure)Registers a closure that will be called when the specified type or subtype is serialized.JsonGeneratorbuild()Creates aJsonGeneratorthat is based on the current options.JsonGenerator.OptionsdateFormat(java.lang.String format)Sets the date format that will be used to serializeDateobjects.JsonGenerator.OptionsdateFormat(java.lang.String format, java.util.Locale locale)Sets the date format that will be used to serializeDateobjects.JsonGenerator.OptionsdisableUnicodeEscaping()Disables the escaping of Unicode characters in JSON String values.JsonGenerator.OptionsexcludeFieldsByName(java.lang.CharSequence... fieldNames)Excludes from the output any fields that match the specified names.JsonGenerator.OptionsexcludeFieldsByName(java.lang.Iterable<? extends java.lang.CharSequence> fieldNames)Excludes from the output any fields that match the specified names.JsonGenerator.OptionsexcludeFieldsByType(java.lang.Class<?>... types)Excludes from the output any fields whose type is the same or is assignable to any of the given types.JsonGenerator.OptionsexcludeFieldsByType(java.lang.Iterable<java.lang.Class<?>> types)Excludes from the output any fields whose type is the same or is assignable to any of the given types.JsonGenerator.OptionsexcludeNulls()Do not serializenullvalues.JsonGenerator.Optionstimezone(java.lang.String timezone)Sets the time zone that will be used to serialize dates.
-
-
-
Field Detail
-
JSON_DATE_FORMAT
protected static final java.lang.String JSON_DATE_FORMAT
- See Also:
- Constant Field Values
-
JSON_DATE_FORMAT_LOCALE
protected static final java.util.Locale JSON_DATE_FORMAT_LOCALE
-
DEFAULT_TIMEZONE
protected static final java.lang.String DEFAULT_TIMEZONE
- See Also:
- Constant Field Values
-
excludeNulls
protected boolean excludeNulls
-
disableUnicodeEscaping
protected boolean disableUnicodeEscaping
-
dateFormat
protected java.lang.String dateFormat
-
dateLocale
protected java.util.Locale dateLocale
-
timezone
protected java.util.TimeZone timezone
-
converters
protected final java.util.Set<JsonGenerator.Converter> converters
-
excludedFieldNames
protected final java.util.Set<java.lang.String> excludedFieldNames
-
excludedFieldTypes
protected final java.util.Set<java.lang.Class<?>> excludedFieldTypes
-
-
Method Detail
-
excludeNulls
public JsonGenerator.Options excludeNulls()
Do not serializenullvalues.- Returns:
- a reference to this
Optionsinstance
-
disableUnicodeEscaping
public JsonGenerator.Options disableUnicodeEscaping()
Disables the escaping of Unicode characters in JSON String values.- Returns:
- a reference to this
Optionsinstance
-
dateFormat
public JsonGenerator.Options dateFormat(java.lang.String format)
Sets the date format that will be used to serializeDateobjects. This must be a valid pattern forSimpleDateFormatand the date formatter will be constructed with the default locale ofLocale.US.- Parameters:
format- date format pattern used to serialize dates- Returns:
- a reference to this
Optionsinstance - Throws:
java.lang.NullPointerException- if the given pattern is nulljava.lang.IllegalArgumentException- if the given pattern is invalid
-
dateFormat
public JsonGenerator.Options dateFormat(java.lang.String format, java.util.Locale locale)
Sets the date format that will be used to serializeDateobjects. This must be a valid pattern forSimpleDateFormat.- Parameters:
format- date format pattern used to serialize dateslocale- the locale whose date format symbols will be used- Returns:
- a reference to this
Optionsinstance - Throws:
java.lang.IllegalArgumentException- if the given pattern is invalid
-
timezone
public JsonGenerator.Options timezone(java.lang.String timezone)
Sets the time zone that will be used to serialize dates.- Parameters:
timezone- used to serialize dates- Returns:
- a reference to this
Optionsinstance - Throws:
java.lang.NullPointerException- if the given timezone is null
-
addConverter
public JsonGenerator.Options addConverter(JsonGenerator.Converter converter)
Registers a converter that will be called when a type it handles is encountered.- Parameters:
converter- to register- Returns:
- a reference to this
Optionsinstance
-
addConverter
public <T> JsonGenerator.Options addConverter(java.lang.Class<T> type, Closure<?> closure)
Registers a closure that will be called when the specified type or subtype is serialized.The closure must accept either 1 or 2 parameters. The first parameter is required and will be instance of the
typefor which the closure is registered. The second optional parameter should be of typeStringand, if available, will be passed the name of the key associated with this value if serializing a JSON Object. This parameter will benullwhen serializing a JSON Array or when there is no way to determine the name of the key.Example:
def generator = new groovy.json.JsonGenerator.Options() .addConverter(URL) { URL u->u.getHost() } .build() def input = [domain: new URL('http://groovy-lang.org/json.html#_parser_variants')] assert generator.toJson(input) == '{"domain":"groovy-lang.org"}'If two or more closures are registered for the exact same type the last closure based on the order they were specified will be used. When serializing an object its type is compared to the list of registered types in the order the were given and the closure for the first suitable type will be called. Therefore, it is important to register more specific types first.
- Type Parameters:
T- the type this converter is registered to handle- Parameters:
type- the type to convertclosure- called when the registered type or any type assignable to the given type is encountered- Returns:
- a reference to this
Optionsinstance - Throws:
java.lang.NullPointerException- if the given type or closure is nulljava.lang.IllegalArgumentException- if the given closure does not accept a parameter of the given type
-
excludeFieldsByName
public JsonGenerator.Options excludeFieldsByName(java.lang.CharSequence... fieldNames)
Excludes from the output any fields that match the specified names.- Parameters:
fieldNames- name of the field to exclude from the output- Returns:
- a reference to this
Optionsinstance
-
excludeFieldsByName
public JsonGenerator.Options excludeFieldsByName(java.lang.Iterable<? extends java.lang.CharSequence> fieldNames)
Excludes from the output any fields that match the specified names.- Parameters:
fieldNames- collection of names to exclude from the output- Returns:
- a reference to this
Optionsinstance
-
excludeFieldsByType
public JsonGenerator.Options excludeFieldsByType(java.lang.Class<?>... types)
Excludes from the output any fields whose type is the same or is assignable to any of the given types.- Parameters:
types- excluded from the output- Returns:
- a reference to this
Optionsinstance
-
excludeFieldsByType
public JsonGenerator.Options excludeFieldsByType(java.lang.Iterable<java.lang.Class<?>> types)
Excludes from the output any fields whose type is the same or is assignable to any of the given types.- Parameters:
types- collection of types to exclude from the output- Returns:
- a reference to this
Optionsinstance
-
build
public JsonGenerator build()
Creates aJsonGeneratorthat is based on the current options.- Returns:
- a fully configured
JsonGenerator
-
-