Package groovy.json

# Class DefaultJsonGenerator

java.lang.Object
groovy.json.DefaultJsonGenerator

All Implemented Interfaces:
  `JsonGenerator`

public class DefaultJsonGenerator extends Object implements 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:
  - `JsonGenerator.Options.build()`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `protected static class`
  `DefaultJsonGenerator.ClosureConverter`
  A 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

  Fields
  Modifier and Type
  Field
  Description
  `protected final Set<JsonGenerator.Converter>`
  `converters`
  Converters consulted before applying the default JSON serialization rules.
  `protected final String`
  `dateFormat`
  Date format pattern used when serializing `Date` values.
  `protected final Locale`
  `dateLocale`
  Locale used when formatting `Date` values.
  `protected final boolean`
  `disableUnicodeEscaping`
  Indicates whether Unicode characters are emitted without escaping.
  `protected final Set<String>`
  `excludedFieldNames`
  Field names excluded from generated JSON objects.
  `protected final Set<Class<?>>`
  `excludedFieldTypes`
  Field types excluded from generated JSON objects.
  `protected final boolean`
  `excludeNulls`
  Indicates whether `null` values are omitted from the output.
  `protected final TimeZone`
  `timezone`
  Time zone used when formatting `Date` values.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `DefaultJsonGenerator(JsonGenerator.Options options)`
  Creates a generator from the supplied serialization options.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected JsonGenerator.Converter`
  `findConverter(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.
  `boolean`
  `isExcludingFieldsNamed(String name)`
  Indicates whether this JsonGenerator is configured to exclude fields by the given name.
  `boolean`
  `isExcludingValues(Object value)`
  Indicates whether this JsonGenerator is configured to exclude values of the given object (may be `null`).
  `protected boolean`
  `shouldExcludeType(Class<?> type)`
  Indicates whether the given type should be excluded from the generated output.
  `String`
  `toJson(Object object)`
  Converts an object to its JSON representation.
  `protected void`
  `writeArray(Class<?> arrayClass, Object array, CharBuf buffer)`
  Serializes array and writes it into specified buffer.
  `protected void`
  `writeCharSequence(CharSequence seq, CharBuf buffer)`
  Serializes any char sequence and writes it into specified buffer.
  `protected void`
  `writeDate(Date date, CharBuf buffer)`
  Serializes date and writes it into specified buffer.
  `protected void`
  `writeIterator(Iterator<?> iterator, CharBuf buffer)`
  Serializes iterator and writes it into specified buffer.
  `protected void`
  `writeMap(Map<?,?> map, CharBuf buffer)`
  Serializes map and writes it into specified buffer.
  `protected void`
  `writeMapEntry(String key, Object value, CharBuf buffer)`
  Serializes a map entry and writes it into specified buffer.
  `protected void`
  `writeNumber(Class<?> numberClass, Number value, CharBuf buffer)`
  Serializes Number value and writes it into specified buffer.
  `protected void`
  `writeObject(Object object, CharBuf buffer)`
  Serializes an unnamed value and writes it into the specified buffer.
  `protected void`
  `writeObject(String key, Object object, CharBuf buffer)`
  Serializes object and writes it into specified buffer.
  `protected void`
  `writeRaw(CharSequence seq, CharBuf buffer)`
  Serializes any char sequence and writes it into specified buffer without performing any manipulation of the given text.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### excludeNulls

    protected final boolean excludeNulls
    Indicates whether `null` values are omitted from the output.

  - ### disableUnicodeEscaping

    protected final boolean disableUnicodeEscaping
    Indicates whether Unicode characters are emitted without escaping.

  - ### dateFormat

    protected final String dateFormat
    Date format pattern used when serializing `Date` values.

  - ### dateLocale

    protected final Locale dateLocale
    Locale used when formatting `Date` values.

  - ### timezone

    protected final TimeZone timezone
    Time zone used when formatting `Date` values.

  - ### converters

    protected final Set<JsonGenerator.Converter> converters
    Converters consulted before applying the default JSON serialization rules.

  - ### excludedFieldNames

    protected final Set<String> excludedFieldNames
    Field names excluded from generated JSON objects.

  - ### excludedFieldTypes

    protected final Set<Class<?>> excludedFieldTypes
    Field types excluded from generated JSON objects.

- ## Constructor Details

  - ### DefaultJsonGenerator

    protected DefaultJsonGenerator(JsonGenerator.Options options)
    Creates a generator from the supplied serialization options.
    Parameters:
      `options` - configured generator options

- ## Method Details

  - ### toJson

    public String toJson(Object object)
    Converts an object to its JSON representation.
    Specified by:
      `toJson` in interface `JsonGenerator`
    Parameters:
      `object` - to convert to JSON
    Returns:
      JSON

  - ### isExcludingFieldsNamed

    public boolean isExcludingFieldsNamed(String name)
    Indicates whether this JsonGenerator is configured to exclude fields by the given name.
    Specified by:
      `isExcludingFieldsNamed` in interface `JsonGenerator`
    Parameters:
      `name` - of the field
    Returns:
      true if that field is being excluded, else false

  - ### isExcludingValues

    public boolean isExcludingValues(Object value)
    Indicates whether this JsonGenerator is configured to exclude values of the given object (may be `null`).
    Specified by:
      `isExcludingValues` in interface `JsonGenerator`
    Parameters:
      `value` - an instance of an object
    Returns:
      true if values like this are being excluded, else false

  - ### writeNumber

    protected void writeNumber(Class<?> numberClass, Number value, CharBuf buffer)
    Serializes Number value and writes it into specified buffer.

  - ### writeObject

    protected void writeObject(Object object, CharBuf buffer)
    Serializes an unnamed value and writes it into the specified buffer.
    Parameters:
      `object` - the value to serialize
      `buffer` - the output buffer

  - ### writeObject

    protected void writeObject(String key, Object object, CharBuf buffer)
    Serializes object and writes it into specified buffer.

  - ### getObjectProperties

    protected Map<?,?> getObjectProperties(Object object)
    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

    protected void writeCharSequence(CharSequence seq, CharBuf buffer)
    Serializes any char sequence and writes it into specified buffer.

  - ### writeRaw

    protected void writeRaw(CharSequence seq, CharBuf buffer)
    Serializes any char sequence and writes it into specified buffer without performing any manipulation of the given text.

  - ### writeDate

    protected void writeDate(Date date, CharBuf buffer)
    Serializes date and writes it into specified buffer.

  - ### writeArray

    protected void writeArray(Class<?> arrayClass, Object array, CharBuf buffer)
    Serializes array and writes it into specified buffer.

  - ### writeMap

    protected void writeMap(Map<?,?> map, CharBuf buffer)
    Serializes map and writes it into specified buffer.

  - ### writeMapEntry

    protected void writeMapEntry(String key, Object value, CharBuf buffer)
    Serializes a map entry and writes it into specified buffer.

  - ### writeIterator

    protected void writeIterator(Iterator<?> iterator, CharBuf buffer)
    Serializes iterator and writes it into specified buffer.

  - ### findConverter

    protected JsonGenerator.Converter findConverter(Class<?> type)
    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. A `null` value 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 `null` if no compatible converters are found for the given type.

  - ### shouldExcludeType

    protected boolean shouldExcludeType(Class<?> type)
    Indicates whether the given type should be excluded from the generated output.
    Parameters:
      `type` - the type to check
    Returns:
      `true` if the given type should not be output, else `false`