Package: groovy.json

## [Java] Class DefaultJsonGenerator.ClosureConverter

- groovy.json.DefaultJsonGenerator.ClosureConverter

- All Implemented Interfaces and Traits:
    Converter

      protected static class DefaultJsonGenerator.ClosureConverter
      extends Object
      implements Converter

  A converter that handles converting a given type using a closure.

  Since:
    2.5.0

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `protected Closure<?>` | `closure` | Closure used to convert matching values.
    `protected int` | `paramCount` | Number of parameters expected by the conversion closure.
    `protected Class<?>` | `type` | Type accepted by this converter.

  - ### Constructor Summary

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

  - ### Methods Summary

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

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### 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 Detail

    - #### 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 Detail

    - #### public Object convert(Object value, String key)

      Converts a given value.

      Parameters:
        `value` - the object to convert
        `key` - the key name for the value, may be `null`
      Returns:
        the converted object

    - #### 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.

      Parameters:
        `o` - the object with which to compare.
      Returns:
        `true` if this object contains the same class; `false` otherwise.

    - #### public boolean handles(Class<?> type)

      Returns `true` if this converter can handle conversions of the given type.

      Parameters:
        `type` - the type of the object to convert
      Returns:
        true if this converter can successfully convert values of the given type

    - #### public int hashCode()

      Returns a hash code based on the handled type.

      Returns:
        the hash code for this converter

    - #### public String toString()

      Returns a string form that includes the handled type.

      Returns:
        a string representation of this converter