public static class JsonGenerator.Options
extends java.lang.Object
JsonGenerator
instance that allows
control over the serialized JSON output. If you do not need to customize the
output it is recommended to use the static JsonOutput.toJson
methods.
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"}'
Modifier and Type | Field and Description |
---|---|
protected java.util.Set<JsonGenerator.Converter> |
converters |
protected java.lang.String |
dateFormat |
protected java.util.Locale |
dateLocale |
protected static java.lang.String |
DEFAULT_TIMEZONE |
protected boolean |
disableUnicodeEscaping |
protected java.util.Set<java.lang.String> |
excludedFieldNames |
protected java.util.Set<java.lang.Class<?>> |
excludedFieldTypes |
protected boolean |
excludeNulls |
protected static java.lang.String |
JSON_DATE_FORMAT |
protected static java.util.Locale |
JSON_DATE_FORMAT_LOCALE |
protected java.util.TimeZone |
timezone |
Constructor and Description |
---|
Options() |
Modifier and Type | Method and Description |
---|---|
<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.
|
JsonGenerator.Options |
addConverter(JsonGenerator.Converter converter)
Registers a converter that will be called when a type it handles is encountered.
|
JsonGenerator |
build()
Creates a
JsonGenerator that is based on the current options. |
JsonGenerator.Options |
dateFormat(java.lang.String format)
Sets the date format that will be used to serialize
Date objects. |
JsonGenerator.Options |
dateFormat(java.lang.String format,
java.util.Locale locale)
Sets the date format that will be used to serialize
Date objects. |
JsonGenerator.Options |
disableUnicodeEscaping()
Disables the escaping of Unicode characters in JSON String values.
|
JsonGenerator.Options |
excludeFieldsByName(java.lang.CharSequence... fieldNames)
Excludes from the output any fields that match the specified names.
|
JsonGenerator.Options |
excludeFieldsByName(java.lang.Iterable<? extends java.lang.CharSequence> fieldNames)
Excludes from the output any fields that match the specified names.
|
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.
|
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.
|
JsonGenerator.Options |
excludeNulls()
Do not serialize
null values. |
JsonGenerator.Options |
timezone(java.lang.String timezone)
Sets the time zone that will be used to serialize dates.
|
protected static final java.lang.String JSON_DATE_FORMAT
protected static final java.util.Locale JSON_DATE_FORMAT_LOCALE
protected static final java.lang.String DEFAULT_TIMEZONE
protected boolean excludeNulls
protected boolean disableUnicodeEscaping
protected java.lang.String dateFormat
protected java.util.Locale dateLocale
protected java.util.TimeZone timezone
protected final java.util.Set<JsonGenerator.Converter> converters
protected final java.util.Set<java.lang.String> excludedFieldNames
protected final java.util.Set<java.lang.Class<?>> excludedFieldTypes
public JsonGenerator.Options excludeNulls()
null
values.Options
instancepublic JsonGenerator.Options disableUnicodeEscaping()
Options
instancepublic JsonGenerator.Options dateFormat(java.lang.String format)
Date
objects.
This must be a valid pattern for SimpleDateFormat
and the
date formatter will be constructed with the default locale of Locale.US
.format
- date format pattern used to serialize datesOptions
instancejava.lang.NullPointerException
- if the given pattern is nulljava.lang.IllegalArgumentException
- if the given pattern is invalidpublic JsonGenerator.Options dateFormat(java.lang.String format, java.util.Locale locale)
Date
objects.
This must be a valid pattern for SimpleDateFormat
.format
- date format pattern used to serialize dateslocale
- the locale whose date format symbols will be usedOptions
instancejava.lang.IllegalArgumentException
- if the given pattern is invalidpublic JsonGenerator.Options timezone(java.lang.String timezone)
timezone
- used to serialize datesOptions
instancejava.lang.NullPointerException
- if the given timezone is nullpublic JsonGenerator.Options addConverter(JsonGenerator.Converter converter)
converter
- to registerOptions
instancepublic <T> JsonGenerator.Options addConverter(java.lang.Class<T> type, Closure<?> closure)
The closure must accept either 1 or 2 parameters. The first parameter
is required and will be instance of the type
for which the closure
is registered. The second optional parameter should be of type String
and, if available, will be passed the name of the key associated with this
value if serializing a JSON Object. This parameter will be null
when
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.
T
- the type this converter is registered to handletype
- the type to convertclosure
- called when the registered type or any type assignable to the given
type is encounteredOptions
instancejava.lang.NullPointerException
- if the given type or closure is nulljava.lang.IllegalArgumentException
- if the given closure does not accept
a parameter of the given typepublic JsonGenerator.Options excludeFieldsByName(java.lang.CharSequence... fieldNames)
fieldNames
- name of the field to exclude from the outputOptions
instancepublic JsonGenerator.Options excludeFieldsByName(java.lang.Iterable<? extends java.lang.CharSequence> fieldNames)
fieldNames
- collection of names to exclude from the outputOptions
instancepublic JsonGenerator.Options excludeFieldsByType(java.lang.Class<?>... types)
types
- excluded from the outputOptions
instancepublic JsonGenerator.Options excludeFieldsByType(java.lang.Iterable<java.lang.Class<?>> types)
types
- collection of types to exclude from the outputOptions
instancepublic JsonGenerator build()
JsonGenerator
that is based on the current options.JsonGenerator