Skip to content

Persist in CBOR format

xcesco edited this page Sep 5, 2017 · 4 revisions

CBOR format

The annotation @BindType allows to persist Java class instance. For User class

@BindType
public class User {
  public String email;
  public String name;
  public String surname;
}

When it is converted by Kripton, it will be converted in a CBOR representation similar to:

BF65656D61696C6E64756D6D7940746573742E6F7267646E616D6564546F6E6A677375726E616D65664D616E65726F68757365726E616D656431323334FF

CBOR is a binary format. The above string representation is done converting every byte in a its two digit hesadecimal value.

As default behaviour, @BindType annotation persists all public fields and attribute with getter/setter.

It's supposed that you already include kripton-android-library or kripton-library in your dependencies.

Before serialize/deserialize java object in CBOR format, you need to include kripton-dataformat-cbor in project's dependency via maven:

<dependency>
  <groupId>com.abubusoft</groupId>
  <artifactId>kripton-dataformat-cbor</artifactId>
  <version>2.0.1</version>
</dependency>

Or via gradle

compile 'com.abubusoft:kripton-dataformat-cbor:2.0.1'

Moreover, you need to registry the CBOR context:

KriptonBinder.registryBinder(new KriptonCborContext());

Serialize from Java bean instance

Code to generate an CBOR version of User instance class:

BinderContext binderContext=KriptonBinder.bind(BinderType.CBOR);
// We use this buffer as output for serialization
KriptonByteArrayOutputStream buffer = new KriptonByteArrayOutputStream();
binderContext.serialize(user, buffer);

We can not use serialize method that produce directly a string value for CBOR format. We need to use an output stream as target.

Parse to Java bean instance

Code to parse CBOR and create a User instance:

BinderContext binderContext=KriptonBinder.bind(BinderType.CBOR);
User user=binderContext.parse(buffer.getByteBufferCopy(), User.class);

For CBOR format you can not use parse method with String input parameter, because it's a binary format. We need to use an stream as input.

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