Skip to content

rocketraman/java-velocypack

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 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

  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

working with VPackSlice - Object

  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();
    ...
  }

build VelocyPack - Array

  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

working with VPackSlice - Array

  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();
    ...
  }

build VelocyPack - nested Objects

  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

serialize POJO

  MyBean entity = new MyBean();
  VPack vpack = new VPack.Builder().build();
  VPackSlice slice = vpack.serialize(entity);

deserialize VelocyPack

  VPackSlice slice = ...
  VPack vpack = new VPack.Builder().build();
  MyBean entity = vpack.deserialize(slice, MyBean.class);

parse Json to VelocPack

  String json = ...
  VPackParser parser = new VPackParser.Builder().build();
  VPackSlice slice = parser.fromJson(json);

parse VelocyPack to Json

  VPackSlice slice = ...
  VPackParser parser = new VPackParser.Builder().build();
  String json = parser.toJson(slice);

Registering modules

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

VPackModule

  VPackModule module = ...
  VPack vpack = new VPack.Builder().registerModule(module).build();

VPackParserModule

  VPackParserModule module = ...
  VPackParser parser = VPackParser.Builder().registerModule(module).build();

Configure serialization / deserialization

POJOs

The class VPack can serialize/deserialize POJOs. They need at least a constructor without parameter.

  public class MyObject {

    private String name;
    private Gender gender;
    private int age;

    public MyObject() {
      super();
    }

  }

serialized fieldnames

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();
    }

  }

ignore fields

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();
    }

  }

custom de-/serializer

  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();

Learn more

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%