Package groovy.json
Class DefaultJsonGenerator
java.lang.Object
groovy.json.DefaultJsonGenerator
- All Implemented Interfaces:
JsonGenerator
A JsonGenerator that can be configured with various
JsonGenerator.Options.
If the default options are sufficient consider using the static JsonOutput.toJson
methods.- Since:
- 2.5.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classA converter that handles converting a given type using a closure.Nested classes/interfaces inherited from interface groovy.json.JsonGenerator
JsonGenerator.Converter, JsonGenerator.Options -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Set<JsonGenerator.Converter>Converters consulted before applying the default JSON serialization rules.protected final StringDate format pattern used when serializingDatevalues.protected final LocaleLocale used when formattingDatevalues.protected final booleanIndicates whether Unicode characters are emitted without escaping.Field names excluded from generated JSON objects.Field types excluded from generated JSON objects.protected final booleanIndicates whethernullvalues are omitted from the output.protected final TimeZoneTime zone used when formattingDatevalues. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedCreates a generator from the supplied serialization options. -
Method Summary
Modifier and TypeMethodDescriptionprotected JsonGenerator.ConverterfindConverter(Class<?> type) Finds a converter that can handle the given type.protected Map<?,?> getObjectProperties(Object object) Collects the public readable properties of the given object for JSON object serialization.booleanisExcludingFieldsNamed(String name) Indicates whether this JsonGenerator is configured to exclude fields by the given name.booleanisExcludingValues(Object value) Indicates whether this JsonGenerator is configured to exclude values of the given object (may benull).protected booleanshouldExcludeType(Class<?> type) Indicates whether the given type should be excluded from the generated output.Converts an object to its JSON representation.protected voidwriteArray(Class<?> arrayClass, Object array, CharBuf buffer) Serializes array and writes it into specified buffer.protected voidwriteCharSequence(CharSequence seq, CharBuf buffer) Serializes any char sequence and writes it into specified buffer.protected voidSerializes date and writes it into specified buffer.protected voidwriteIterator(Iterator<?> iterator, CharBuf buffer) Serializes iterator and writes it into specified buffer.protected voidSerializes map and writes it into specified buffer.protected voidwriteMapEntry(String key, Object value, CharBuf buffer) Serializes a map entry and writes it into specified buffer.protected voidwriteNumber(Class<?> numberClass, Number value, CharBuf buffer) Serializes Number value and writes it into specified buffer.protected voidwriteObject(Object object, CharBuf buffer) Serializes an unnamed value and writes it into the specified buffer.protected voidwriteObject(String key, Object object, CharBuf buffer) Serializes object and writes it into specified buffer.protected voidwriteRaw(CharSequence seq, CharBuf buffer) Serializes any char sequence and writes it into specified buffer without performing any manipulation of the given text.
-
Field Details
-
excludeNulls
protected final boolean excludeNullsIndicates whethernullvalues are omitted from the output. -
disableUnicodeEscaping
protected final boolean disableUnicodeEscapingIndicates whether Unicode characters are emitted without escaping. -
dateFormat
Date format pattern used when serializingDatevalues. -
dateLocale
Locale used when formattingDatevalues. -
timezone
Time zone used when formattingDatevalues. -
converters
Converters consulted before applying the default JSON serialization rules. -
excludedFieldNames
Field names excluded from generated JSON objects. -
excludedFieldTypes
Field types excluded from generated JSON objects.
-
-
Constructor Details
-
DefaultJsonGenerator
Creates a generator from the supplied serialization options.- Parameters:
options- configured generator options
-
-
Method Details
-
toJson
Converts an object to its JSON representation.- Specified by:
toJsonin interfaceJsonGenerator- Parameters:
object- to convert to JSON- Returns:
- JSON
-
isExcludingFieldsNamed
Indicates whether this JsonGenerator is configured to exclude fields by the given name.- Specified by:
isExcludingFieldsNamedin interfaceJsonGenerator- Parameters:
name- of the field- Returns:
- true if that field is being excluded, else false
-
isExcludingValues
Indicates whether this JsonGenerator is configured to exclude values of the given object (may benull).- Specified by:
isExcludingValuesin interfaceJsonGenerator- Parameters:
value- an instance of an object- Returns:
- true if values like this are being excluded, else false
-
writeNumber
Serializes Number value and writes it into specified buffer. -
writeObject
Serializes an unnamed value and writes it into the specified buffer.- Parameters:
object- the value to serializebuffer- the output buffer
-
writeObject
Serializes object and writes it into specified buffer. -
getObjectProperties
Collects the public readable properties of the given object for JSON object serialization.- Parameters:
object- the object whose properties should be collected- Returns:
- a map of property names to values
-
writeCharSequence
Serializes any char sequence and writes it into specified buffer. -
writeRaw
Serializes any char sequence and writes it into specified buffer without performing any manipulation of the given text. -
writeDate
Serializes date and writes it into specified buffer. -
writeArray
Serializes array and writes it into specified buffer. -
writeMap
Serializes map and writes it into specified buffer. -
writeMapEntry
Serializes a map entry and writes it into specified buffer. -
writeIterator
Serializes iterator and writes it into specified buffer. -
findConverter
Finds a converter that can handle the given type. The first converter that reports it can handle the type is returned, based on the order in which the converters were specified. Anullvalue will be returned if no suitable converter can be found for the given type.- Parameters:
type- that this converter can handle- Returns:
- first converter that can handle the given type; else
nullif no compatible converters are found for the given type.
-
shouldExcludeType
Indicates whether the given type should be excluded from the generated output.- Parameters:
type- the type to check- Returns:
trueif the given type should not be output, elsefalse
-