Skip to content

Automatically Apply jsonbadapaters #35

@jsonbrobot

Description

@jsonbrobot

In the spirits of what JPA has done with AttributeConverter(autoApply=true)

  • I would suggest the following changes and implementation of the JsonbtypeAdapter:
@JsonbAnnotation
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD,ElementType.TYPE})
public @interface JsonbTypeAdapter {

  //Defines if the runtime should automatically use the following adapter
  //for all types as defined by the adapter implementation generic-type
  //default is false
  boolean autoApply() default false;
 }
  • The adapter interface adaptation
    //Where generic-type V determines the actual type for which this adapter is associated. 
    public interface JsonbAdapter<V, B> {

       //Adapts the user property specified to a marshallable json capable type
       B adaptTo(V value);

       V adaptFrom(B value)
    }
  • Then an example usages:
    //In this case, the jsonb runtime should apply this adapter to all properties and types, for which the type is Normaltype. 
    //These adapters could technically be registered on application startup (for javaee) 
    @JsonTypeAdapter(autoApply=true)
    public class CustomTypeAdapater implements JsonbAdapter<NormalType, Customtype> {
    }

    // example
    public class CustomObject {
      //Automatically, the json runtime will apply the CustomTypeAdapater without user explicitly specifying the values.
      private NormalType2 normaltype;
    }
  • and, from which the user has to specifically annotate the field with
    @JsonTypeAdapter(autoApply=false)
    public class CustomTypeAdapater2 implements JsonbAdapter<NormalType2, Customtype2> {
    }

    // and interface 
    public interface JsnobAdapted {
      Class<? extends JsonbAdapter> adapter();
    }

    //Example usage 
    public class CustomObject2 {

      @JsnobAdapted(CustomTypeAdapater2.class)
      private NormalType2 normaltype;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions