Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

122 Commits

Repository files navigation

ArangoDB-Logo

ArangoDB VelocyPack Java

Maven Central

Java implementation for VelocyPack.

Maven

To add the dependency to your project with maven, add the following code to your pom.xml:

<dependencies>
<dependency>
<groupId>com.arangodb</groupId>
<artifactId>velocypack</artifactId>
<version>1.0.13</version>
</dependency>
</dependencies>

If you want to test with a snapshot version (e.g. 1.0.0-SNAPSHOT), add the staging repository of oss.sonatype.org to your pom.xml:

<repositories>
<repository>
<id>arangodb-snapshots</id>
<url>https://oss.sonatype.org/content/groups/staging</url>
</repository>
</repositories>

Compile

mvn clean install -DskipTests=true -Dgpg.skip=true -Dmaven.javadoc.skip=true -B

Usage

build VelocyPack - Object

VPackBuilderbuilder = newVPackBuilder();
builder.add(ValueType.OBJECT); // object startbuilder.add("foo", "bar"); // add field "foo" with value "bar"builder.close(); // object endVPackSliceslice = builder.slice(); // create slice

working with VPackSlice - Object

VPackSliceslice = ...
intsize = slice.size(); // number of fieldsVPackSlicefoo = slice.get("foo"); // get field "foo"Stringvalue = foo.getAsString(); // get value from "foo"// iterate over the fieldsfor (finalIterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext();) {
Entry<String, VPackSlice> field = iterator.next();
...
}

build VelocyPack - Array

VPackBuilderbuilder = newVPackBuilder();
builder.add(ValueType.ARRAY); // array startbuilder.add(1); // add value 1builder.add(2); // add value 2builder.add(3); // add value 3builder.close(); // array endVPackSliceslice = builder.slice(); // create slice

working with VPackSlice - Array

VPackSliceslice = ...
intsize = slice.size(); // number of values// iterate over valuesfor (inti = 0; i < slice.size(); i++) {
VPackSlicevalue = slice.get(i);
...
}
// iterate over values with Iteratorfor (finalIterator<VPackSlice> iterator = slice.arrayIterator(); iterator.hasNext();) {
VPackSlicevalue = iterator.next();
...
}

build VelocyPack - nested Objects

VPackBuilderbuilder = newVPackBuilder();
builder.add(ValueType.OBJECT); // object startbuilder.add("foo", ValueType.OBJECT); // add object in field "foo"builder.add("bar", 1); // add field "bar" with value 1 to object "foo"builder.close(); // object "foo" endbuilder.close(); // object endVPackSliceslice = builder.slice(); // create slice

serialize POJO

MyBeanentity = newMyBean();
VPackvpack = newVPack.Builder().build();
VPackSliceslice = vpack.serialize(entity);

deserialize VelocyPack

VPackSliceslice = ...
VPackvpack = newVPack.Builder().build();
MyBeanentity = vpack.deserialize(slice, MyBean.class);

parse Json to VelocPack

Stringjson = ...
VPackParserparser = newVPackParser.Builder().build();
VPackSliceslice = parser.fromJson(json);

parse VelocyPack to Json

VPackSliceslice = ...
VPackParserparser = newVPackParser.Builder().build();
Stringjson = parser.toJson(slice);

Registering modules

Both VPack and VPackParser allow registering of modules which can offer custom serializers/deserializers for additional types.

VPackModule

VPackModulemodule = ...
VPackvpack = newVPack.Builder().registerModule(module).build();

VPackParserModule

VPackParserModulemodule = ...
VPackParserparser = VPackParser.Builder().registerModule(module).build();

Configure serialization / deserialization

POJOs

The class VPack can serialize/deserialize POJOs. They need at least a constructor without parameter. Also Builder deserialization, All-Arguments-Constructor deserialization and Static Factory Method deserialization are supported.

publicclassMyObject {
privateStringname;
privateGendergender;
privateintage;
publicMyObject() {
super();
}
}

serialized fieldnames

To use a different serialized name for a field, use the annotation SerializedName.

publicclassMyObject {
@SerializedName("title")
privateStringname;
privateGendergender;
privateintage;
publicMyObject() {
super();
}
}

ignore fields

To ignore fields at serialization/deserialization, use the annotation Expose

publicclassMyObject {
@ExposeprivateStringname;
@Expose(serialize = true, deserialize = false)
privateGendergender;
privateintage;
publicMyObject() {
super();
}
}

custom de-/serializer

VPackvpack = newVPack.Builder()
.registerDeserializer(MyObject.class, newVPackDeserializer<MyObject>() {
@OverridepublicMyObjectdeserialize(
finalVPackSliceparent,
finalVPackSlicevpack,
finalVPackDeserializationContextcontext) throwsVPackException {
finalMyObjectobj = newMyObject();
obj.setName(vpack.get("name").getAsString());
returnobj;
}
}).registerSerializer(MyObject.class, newVPackSerializer<MyObject>() {
@Overridepublicvoidserialize(
finalVPackBuilderbuilder,
finalStringattribute,
finalMyObjectvalue,
finalVPackSerializationContextcontext) throwsVPackException {
builder.add(attribute, ValueType.OBJECT);
builder.add("name", value.getName());
builder.close();
}
}).build();

Builder deserialization

Deserialization using builders is supported using the following annotations:

@VPackPOJOBuilder

It allows specifying the builder setters and build method. It has the following fields:

  • buildMethodName: String: the build method to call on the builder object after having set all the attributes
  • withSetterPrefix: String: the prefix of the builder setters

This annotation can target:

  • the builder class having a public no-arg constructor, eg:
@VPackPOJOBuilder(buildMethodName = "build", withSetterPrefix = "set")
publicclassBuilder {
publicBuilder() {
// ...
}
publicMyClassbuild() {
// ...
}
// ...
}
  • a public static factory method which returns the builder, eg:
publicclassMyClass {
@VPackPOJOBuilder(buildMethodName = "build", withSetterPrefix = "with")
publicstaticBuilder<MyClass> builder() {
//...
}
// ...
}

@VPackDeserialize

It allows to specify the builder class that will be used during the deserialization. It has the following fields:

  • builder: Class<?>: builder class to use
  • builderConfig: VPackPOJOBuilder: it allows specifying the builder setters and build method, useful in case the builder code is auto generated and you cannot add @VPackPOJOBuilder to it.

This annotation can target:

  • setter: allows specifying the builder for the setter argument
  • field: allows specifying the builder for the field
  • class: allows specifying the builder for the class
  • parameter: allows specifying the builder for a constructor (or factory method) parameter

Example:

@VPackDeserialize(builder = MyClassBuilder.class,
builderConfig = @VPackPOJOBuilder(buildMethodName = "build",
withSetterPrefix = "with"))
publicclassMyClass {
// ...
} 

All-Arguments-Constructor deserialization

Deserialization using All-Arguments-Constructor is supported annotating the constructor with @VPackCreator and annotating each of its parameters with @SerializedName("name"), eg:

publicclassPerson {
privatefinalStringname;
privatefinalintage;
@VPackCreatorpublicPerson(
@SerializedName("name") Stringname,
@SerializedName("age") intage
) {
this.name = name;
this.age = age;
}
// ...
}

Static Factory Method deserialization

Deserialization using Static Factory Method is supported annotating the method with @VPackCreator and annotating each of its parameters with @SerializedName("name"), eg:

publicclassPerson {
privatefinalStringname;
privatefinalintage;
privatePerson(Stringname, intage) {
this.name = name;
this.age = age;
}
@VPackCreatorpublicstaticPersonof(
@SerializedName("name") Stringname,
@SerializedName("age") intage
) {
returnnewPerson(name, age);
}
// ...
}

Kotlin data classes

Deserialization of Kotlin data classes is supported annotating the constructor with @VPackCreator and annotating each of its parameters with @SerializedName("name"), eg:

data classPerson @VPackCreator constructor(
@SerializedName("name") valname:String,
@SerializedName("age") valage:Int
)

Scala case classes

Deserialization of Scala case classes is supported annotating the constructor with @VPackCreator and annotating each of its parameters with @SerializedName("name"), eg:

caseclassCasePerson@VPackCreator()(
@SerializedName("name") valname:String,
@SerializedName("age") valage:Int
)

Learn more

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages