Skip to content

@BindType

xcesco edited this page Apr 6, 2018 · 13 revisions

This is one the key annotation for Kripton. It is used to generate for annotated Java classes a BinderMapper. It's used on class definitions.

Attributes

Defined attributes are:

  • value: name of the element. For XML it's the tag name. For JSON it has no use. For Property format, it's the name of the property. If no value is specified, the name of the element (tag or attribute for XML, the name for properties just for example) will be the field name.
  • allFields: All fields are bound, for each kind of binding. The default value is true. If this attribute is false it is necessary to specify which field is needed to be persisted by @Bind annotation.

How it is used

When a Java class is annotated by @Bind annotation, a class mapper will be created by Kripton Annotation Processor. For the class Friend:

@BindType
public class Image {
  public String id;
  public String format;
  public String url;
  public String description;
}

The annotation processor will generate FriendBindMap:

@BindMap(Image.class)
public class ImageBindMap extends AbstractMapper<Image> {
  @Override
  public int serializeOnJackson(Image object, JsonGenerator jacksonSerializer) throws Exception {
    ...
  }

  @Override
  public int serializeOnJacksonAsString(Image object, JsonGenerator jacksonSerializer) throws Exception {
    ...
  }

  @Override
  public void serializeOnXml(Image object, XMLSerializer xmlSerializer, int currentEventType) throws Exception {
    ...
  }

  @Override
  public Image parseOnJackson(JsonParser jacksonParser) throws Exception {
    ...
  }

  @Override
  public Image parseOnJacksonAsString(JsonParser jacksonParser) throws Exception {
    ...
  }

  @Override
  public Image parseOnXml(XMLParser xmlParser, int currentEventType) throws Exception {
    ...
  }
}

This kind of class will be used by binder contexts to do serialize o deserialize Java bean. This class is showed just to understand how Kripton Annotation Processor works.

To persist Friend class in JSON:

Friend input=new Friend();
...
String result=KriptonBinder.jsonBind().serialize(input);

To deserialize a Friend class from its JSON representation:

String buffer=...
...
Friend result=KriptonBinder.jsonBind().parse(buffer, Friend.class);

Persistence on XML format is similar to persist on JSON or another format, registry XML binder context before persisting bean.

Friend input=new Friend();
...
String result=KriptonBinder.xmlBind().serialize(input);
...

To deserialize a Friend class from its JSON rapresentation:

String buffer="{\"id\":23,\"name\":\"dummy name\"}";
...
Friend result=KriptonBinder.xmlBind().parse(buffer, Friend.class);

For other format like YAML or CBOR or (Java) Properties, before use parsing or serialize an object, you have to registry specific binding context. To enable the optional persistence context:

// registry CBOR context
KriptonBinder.registryBinder(new KriptonCborContext());

// registry YAML context
KriptonBinder.registryBinder(new KriptonYamlContext());

// registry (Java) Properties context
KriptonBinder.registryBinder(new KriptonPropertiesContext());

Every context can be used only if you include relative dependencies:

  • kripton-dataformat-yaml for YAML format
  • kripton-dataformat-properties for (Java) properties
  • kripton-dataformat-cbor for CBOR format
  • kripton-dataformat-smile for SMILE format

Attributes

  • value: Name of the element. For XML it's the tag name. For JSON it has no use. For Property format, it's the name of the property
  • allFields: All fields are bound, for each kind of binding.

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally