[ 
https://issues.apache.org/jira/browse/AVRO-2655?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16995500#comment-16995500
 ] 

Wang Geng edited comment on AVRO-2655 at 12/13/19 9:27 AM:
-----------------------------------------------------------

To implement this feature. My suggestion is to add Optional type in Schema.java 
along with OptionalSchema.

In SpecificData.java, need to handle whether the object is instance of Optional.

Here is some draft code diff.

in Schema.java
{code:java}
// code placeholder
  public enum Type {
    RECORD, ENUM, ARRAY, MAP, UNION, FIXED, STRING, BYTES, INT, LONG, FLOAT, 
DOUBLE, BOOLEAN, NULL, OPTIONAL;
...
  private static class OptionalSchema extends Schema{
    private final Schema valueType;    public OptionalSchema(Schema valueType) {
      super(Type.OPTIONAL);
      this.valueType = valueType;
    }    @Override
    public Schema getValueType() {
      return valueType;
    }{code}
 
{code:java}
// code placeholder

      else if (Optional.class.isAssignableFrom(raw)){ // Optional
        if (params.length != 1)
          throw new AvroTypeException("No Optional type specified.");
          return Schema.createOptional(createSchema(params[0], names));
      }
{code}


was (Author: biggeng):
To implement this feature. My suggestion is to add Optional type in Schema.java 
along with OptionalSchema.

In SpecificData.java, need to handle whether the object is instance of Optional.

Here is some draft code diff.
{code:java}
// code placeholder

      else if (Optional.class.isAssignableFrom(raw)){ // Optional
        if (params.length != 1)
          throw new AvroTypeException("No Optional type specified.");
          return Schema.createOptional(createSchema(params[0], names));
      }
{code}

> Support Optional Class in Java
> ------------------------------
>
>                 Key: AVRO-2655
>                 URL: https://issues.apache.org/jira/browse/AVRO-2655
>             Project: Apache Avro
>          Issue Type: New Feature
>          Components: java
>            Reporter: Wang Geng
>            Priority: Major
>              Labels: features
>
> Optional class is a useful way in Java to avoid NPE since Java 8. Also we 
> have Optional description in Avro, using Union[Null, ]. 
> Avro needs to serialize/deserialize Optional object in Java.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to