Java implementation for VelocyPack.
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>
mvn clean install -DskipTests=true -Dgpg.skip=true -Dmaven.javadoc.skip=true -B
VPackBuilder builder = new VPackBuilder();
builder.add(ValueType.OBJECT); // object start
builder.add("foo", "bar"); // add field "foo" with value "bar"
builder.close(); // object end
VPackSlice slice = builder.slice(); // create slice
VPackSlice slice = ...
int size = slice.size(); // number of fields
VPackSlice foo = slice.get("foo"); // get field "foo"
String value = foo.getAsString(); // get value from "foo"
// iterate over the fields
for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext();) {
Entry<String, VPackSlice> field = iterator.next();
...
}
VPackBuilder builder = new VPackBuilder();
builder.add(ValueType.ARRAY); // array start
builder.add(1); // add value 1
builder.add(2); // add value 2
builder.add(3); // add value 3
builder.close(); // array end
VPackSlice slice = builder.slice(); // create slice
VPackSlice slice = ...
int size = slice.size(); // number of values
// iterate over values
for (int i = 0; i < slice.size(); i++) {
VPackSlice value = slice.get(i);
...
}
// iterate over values with Iterator
for (final Iterator<VPackSlice> iterator = slice.arrayIterator(); iterator.hasNext();) {
VPackSlice value = iterator.next();
...
}
VPackBuilder builder = new VPackBuilder();
builder.add(ValueType.OBJECT); // object start
builder.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" end
builder.close(); // object end
VPackSlice slice = builder.slice(); // create slice
MyBean entity = new MyBean();
VPack vpack = new VPack.Builder().build();
VPackSlice slice = vpack.serialize(entity);
VPackSlice slice = ...
VPack vpack = new VPack.Builder().build();
MyBean entity = vpack.deserialize(slice, MyBean.class);
String json = ...
VPackParser parser = new VPackParser.Builder().build();
VPackSlice slice = parser.fromJson(json);
VPackSlice slice = ...
VPackParser parser = new VPackParser.Builder().build();
String json = parser.toJson(slice);
Both VPack
and VPackParser
allow registering of modules which can offer custom serializers/deserializers for additional types.
VPackModule module = ...
VPack vpack = new VPack.Builder().registerModule(module).build();
VPackParserModule module = ...
VPackParser parser = VPackParser.Builder().registerModule(module).build();
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.
public class MyObject {
private String name;
private Gender gender;
private int age;
public MyObject() {
super();
}
}
To use a different serialized name for a field, use the annotation SerializedName
.
public class MyObject {
@SerializedName("title")
private String name;
private Gender gender;
private int age;
public MyObject() {
super();
}
}
To ignore fields at serialization/deserialization, use the annotation Expose
public class MyObject {
@Expose
private String name;
@Expose(serialize = true, deserialize = false)
private Gender gender;
private int age;
public MyObject() {
super();
}
}
VPack vpack = new VPack.Builder()
.registerDeserializer(MyObject.class, new VPackDeserializer<MyObject>() {
@Override
public MyObject deserialize(
final VPackSlice parent,
final VPackSlice vpack,
final VPackDeserializationContext context) throws VPackException {
final MyObject obj = new MyObject();
obj.setName(vpack.get("name").getAsString());
return obj;
}
}).registerSerializer(MyObject.class, new VPackSerializer<MyObject>() {
@Override
public void serialize(
final VPackBuilder builder,
final String attribute,
final MyObject value,
final VPackSerializationContext context) throws VPackException {
builder.add(attribute, ValueType.OBJECT);
builder.add("name", value.getName());
builder.close();
}
}).build();
Deserialization using builders is supported using the following annotations:
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 attributeswithSetterPrefix: 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")
public class Builder {
public Builder() {
// ...
}
public MyClass build() {
// ...
}
// ...
}
- a public static factory method which returns the builder, eg:
public class MyClass {
@VPackPOJOBuilder(buildMethodName = "build", withSetterPrefix = "with")
public static Builder<MyClass> builder() {
//...
}
// ...
}
It allows to specify the builder class that will be used during the deserialization. It has the following fields:
builder: Class<?>
: builder class to usebuilderConfig: 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 argumentfield
: allows specifying the builder for the fieldclass
: allows specifying the builder for the classparameter
: allows specifying the builder for a constructor (or factory method) parameter
Example:
@VPackDeserialize(builder = MyClassBuilder.class,
builderConfig = @VPackPOJOBuilder(buildMethodName = "build",
withSetterPrefix = "with"))
public class MyClass {
// ...
}
Deserialization using All-Arguments-Constructor is supported annotating the constructor with @VPackCreator
and
annotating each of its parameters with @SerializedName("name")
, eg:
public class Person {
private final String name;
private final int age;
@VPackCreator
public Person(
@SerializedName("name") String name,
@SerializedName("age") int age
) {
this.name = name;
this.age = age;
}
// ...
}
Deserialization using Static Factory Method is supported annotating the method with @VPackCreator
and
annotating each of its parameters with @SerializedName("name")
, eg:
public class Person {
private final String name;
private final int age;
private Person(String name, int age) {
this.name = name;
this.age = age;
}
@VPackCreator
public static Person of(
@SerializedName("name") String name,
@SerializedName("age") int age
) {
return new Person(name, age);
}
// ...
}
Deserialization of Kotlin data classes is supported annotating the constructor with @VPackCreator
and annotating each of
its parameters with @SerializedName("name")
, eg:
data class Person @VPackCreator constructor(
@SerializedName("name") val name: String,
@SerializedName("age") val age: Int
)
Deserialization of Scala case classes is supported annotating the constructor with @VPackCreator
and annotating each of
its parameters with @SerializedName("name")
, eg:
case class CasePerson @VPackCreator()(
@SerializedName("name") val name: String,
@SerializedName("age") val age: Int
)