Class IgnoredFieldConverter

java.lang.Object
name.martingeisse.grumpyjson.builtin.helper_types.IgnoredFieldConverter
All Implemented Interfaces:
JsonDeserializer, JsonSerializer<IgnoredField>

public final class IgnoredFieldConverter extends Object implements JsonSerializer<IgnoredField>, JsonDeserializer
The converter for IgnoredField.

This converter is registered by default, and only needs to be manually registered if it gets removed, such as by calling JsonRegistries.clear().

  • Constructor Details

    • IgnoredFieldConverter

      public IgnoredFieldConverter()
      Constructor.
  • Method Details

    • supportsTypeForDeserialization

      public boolean supportsTypeForDeserialization(Type type)
      Description copied from interface: JsonDeserializer
      Checks if this deserializer supports the specified type.
      Specified by:
      supportsTypeForDeserialization in interface JsonDeserializer
      Parameters:
      type - the type to check
      Returns:
      true if supported, false if not
    • deserialize

      public IgnoredField deserialize(com.google.gson.JsonElement json, Type type) throws JsonDeserializationException
      Description copied from interface: JsonDeserializer
      Converts a value from JSON.

      This method must not be called with a type for which JsonDeserializer.supportsTypeForDeserialization(Type) returns false. Calling it with such types anyway results in undefined behavior.

      Specified by:
      deserialize in interface JsonDeserializer
      Parameters:
      json - the JSON
      type - the type to deserialize
      Returns:
      the deserialized value
      Throws:
      JsonDeserializationException - if the JSON does not match the expected structure
    • deserializeAbsent

      public IgnoredField deserializeAbsent(Type type)
      Description copied from interface: JsonDeserializer
      Converts a value from an absent JSON fragment. This can be used to return a default for optional object properties.

      The standard implementation of this method is that missing values are not tolerated, and throws an exception.

      This method must not be called with a type for which JsonDeserializer.supportsTypeForDeserialization(Type) returns false. Calling it with such types anyway results in undefined behavior.

      Specified by:
      deserializeAbsent in interface JsonDeserializer
      Parameters:
      type - the type to deserialize
      Returns:
      the default value for that type
    • supportsClassForSerialization

      public boolean supportsClassForSerialization(Class<?> clazz)
      Description copied from interface: JsonSerializer
      Checks if this serializer supports the specified class.
      Specified by:
      supportsClassForSerialization in interface JsonSerializer<IgnoredField>
      Parameters:
      clazz - the class to check
      Returns:
      true if supported, false if not
    • serialize

      public com.google.gson.JsonElement serialize(IgnoredField value) throws JsonSerializationException
      Description copied from interface: JsonSerializer
      Converts a value to JSON.

      This method must not be called with values for whose class JsonSerializer.supportsClassForSerialization(Class) returns false. Calling it with such values anyway results in undefined behavior.

      This method is not supported for values that can vanish during serialization, such as OptionalField -- JsonSerializer.serializeOptional(Object) should be called instead. If this method is called with such values anyway, it should always fail, even when the value does not vanish, to capture bugs early. Examples where this happens: - in a list of OptionalFields. While we could simply remove vanishing elements, doing so is just a weird way of filtering the list before serialization, which can be done the usual way. Moreover, there is no useful interpretation of a list-of-OptionalField during deserialization since all elements found in the JSON are known to be present, and vanishing elements cannot be found. - when turning a top-level OptionalField to JSON - when nesting two OptionalFields

      Specified by:
      serialize in interface JsonSerializer<IgnoredField>
      Parameters:
      value - the value to convert to JSON
      Returns:
      the generated JSON
      Throws:
      JsonSerializationException - if the value is in an inconsistent state, or in a state that cannot be converted to JSON
    • serializeOptional

      public Optional<com.google.gson.JsonElement> serializeOptional(IgnoredField value) throws JsonSerializationException
      Description copied from interface: JsonSerializer
      Converts a value to JSON in a context in which a non-existing JSON value can be handled, and therefore supports values that turn out to vanish during serialization, such as OptionalField. The primary use case is for optional object properties.

      Most types cannot vanish, and therefore have no special behavior in a context that supports vanishable values, so the standard implementation just delegates to JsonSerializer.serialize(Object).

      This method must not be called with values for whose class JsonSerializer.supportsClassForSerialization(Class) returns false. Calling it with such values anyway results in undefined behavior.

      Specified by:
      serializeOptional in interface JsonSerializer<IgnoredField>
      Parameters:
      value - the value to convert to JSON. This value is not allowed to be null; "optional" refers to the fact that serialization happens in a context in which the field can vanish in JSON based on the value to serialize, not that the input value is optional.
      Returns:
      the generated JSON, or nothing in case the value vanishes in JSON
      Throws:
      JsonSerializationException - if the value is in an inconsistent state, or in a state that cannot be converted to JSON