Package groovy.json

# Class DefaultJsonGenerator.ClosureConverter

java.lang.Object
groovy.json.DefaultJsonGenerator.ClosureConverter

All Implemented Interfaces:
  `JsonGenerator.Converter`

Enclosing class:
  DefaultJsonGenerator

protected static class DefaultJsonGenerator.ClosureConverter extends Object implements JsonGenerator.Converter
A converter that handles converting a given type using a closure.

Since:
  2.5.0

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected final Closure<?>`
  `closure`
  Closure used to convert matching values.
  `protected final int`
  `paramCount`
  Number of parameters expected by the conversion closure.
  `protected final Class<?>`
  `type`
  Type accepted by this converter.

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `ClosureConverter(Class<?> type, Closure<?> closure)`
  Creates a converter backed by the supplied closure.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `Object`
  `convert(Object value, String key)`
  Converts a given value.
  `boolean`
  `equals(Object o)`
  Any two Converter instances registered for the same type are considered to be equal.
  `boolean`
  `handles(Class<?> type)`
  Returns `true` if this converter can handle conversions of the given type.
  `int`
  `hashCode()`
  Returns a hash code based on the handled type.
  `String`
  `toString()`
  Returns a string form that includes the handled type.

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

  `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### type

    protected final Class<?> type
    Type accepted by this converter.

  - ### closure

    protected final Closure<?> closure
    Closure used to convert matching values.

  - ### paramCount

    protected final int paramCount
    Number of parameters expected by the conversion closure.

- ## Constructor Details

  - ### ClosureConverter

    protected ClosureConverter(Class<?> type, Closure<?> closure)
    Creates a converter backed by the supplied closure.
    Parameters:
      `type` - the type handled by the converter
      `closure` - the closure performing the conversion

- ## Method Details

  - ### handles

    public boolean handles(Class<?> type)
    Returns `true` if this converter can handle conversions of the given type.
    Specified by:
      `handles` in interface `JsonGenerator.Converter`
    Parameters:
      `type` - the type of the object to convert
    Returns:
      true if this converter can successfully convert values of the given type

  - ### convert

    public Object convert(Object value, String key)
    Converts a given value.
    Specified by:
      `convert` in interface `JsonGenerator.Converter`
    Parameters:
      `value` - the object to convert
      `key` - the key name for the value, may be `null`
    Returns:
      the converted object

  - ### equals

    public boolean equals(Object o)
    Any two Converter instances registered for the same type are considered to be equal. This comparison makes managing instances in a Set easier; since there is no chaining of Converters it makes sense to only allow one per type.
    Overrides:
      `equals` in class `Object`
    Parameters:
      `o` - the object with which to compare.
    Returns:
      `true` if this object contains the same class; `false` otherwise.

  - ### hashCode

    public int hashCode()
    Returns a hash code based on the handled type.
    Overrides:
      `hashCode` in class `Object`
    Returns:
      the hash code for this converter

  - ### toString

    public String toString()
    Returns a string form that includes the handled type.
    Overrides:
      `toString` in class `Object`
    Returns:
      a string representation of this converter