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
VPackBuilderbuilder = newVPackBuilder();
builder.add(ValueType.OBJECT); // object startbuilder.add("foo", "bar"); // add field "foo" with value "bar"builder.close(); // object endVPackSliceslice = builder.slice(); // create sliceVPackSliceslice = ...
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();
...
}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 sliceVPackSliceslice = ...
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();
...
}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 sliceMyBeanentity = newMyBean();
VPackvpack = newVPack.Builder().build();
VPackSliceslice = vpack.serialize(entity);VPackSliceslice = ...
VPackvpack = newVPack.Builder().build();
MyBeanentity = vpack.deserialize(slice, MyBean.class);Stringjson = ...
VPackParserparser = newVPackParser.Builder().build();
VPackSliceslice = parser.fromJson(json);VPackSliceslice = ...
VPackParserparser = newVPackParser.Builder().build();
Stringjson = parser.toJson(slice);Both VPack and VPackParser allow registering of modules which can offer custom serializers/deserializers for additional types.
VPackModulemodule = ...
VPackvpack = newVPack.Builder().registerModule(module).build();VPackParserModulemodule = ...
VPackParserparser = 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.
publicclassMyObject {
privateStringname;
privateGendergender;
privateintage;
publicMyObject() {
super();
}
}To use a different serialized name for a field, use the annotation SerializedName.
publicclassMyObject {
@SerializedName("title")
privateStringname;
privateGendergender;
privateintage;
publicMyObject() {
super();
}
}To ignore fields at serialization/deserialization, use the annotation Expose
publicclassMyObject {
@ExposeprivateStringname;
@Expose(serialize = true, deserialize = false)
privateGendergender;
privateintage;
publicMyObject() {
super();
}
}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();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")
publicclassBuilder {
publicBuilder() {
// ...
}
publicMyClassbuild() {
// ...
}
// ...
}- a public static factory method which returns the builder, eg:
publicclassMyClass {
@VPackPOJOBuilder(buildMethodName = "build", withSetterPrefix = "with")
publicstaticBuilder<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@VPackPOJOBuilderto 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"))
publicclassMyClass {
// ...
} 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;
}
// ...
}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);
}
// ...
}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
)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
)