Giter Club home page Giter Club logo

java-velocypack's Introduction

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>x.y.z</version>
  </dependency>
</dependencies>

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

Learn more

java-velocypack's People

Contributors

dependabot[bot] avatar hkernbach avatar htmldoug avatar jebbench avatar mpoeter avatar mpv1989 avatar rashtao avatar rocketraman avatar siilike avatar simran-b avatar wajda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

java-velocypack's Issues

VPackBuilder misses add(Byte)

It has add(final String attribute, final Byte value), so I would have expected a add(final Byte value) to be present, like there are for all other data types. addTagged(final long tag, final Byte value) is also missing.

serialize and deserialize not support generic types

Version: 1.0.11

Test source code: https://gist.github.com/zqq90/2aa2e7aa6fdd7453e50d26cb0bc22b30

Exceptions like:

Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl cannot be cast to java.lang.Class
	at com.arangodb.velocypack.VPack.addValue(VPack.java:854)
	at com.arangodb.velocypack.VPack.serializeField(VPack.java:824)
	...
Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
	at com.arangodb.velocypack.VPack.addValue(VPack.java:854)
	at com.arangodb.velocypack.VPack.serializeField(VPack.java:824)
	...
Caused by: java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
	at com.arangodb.velocypack.VPack.getValue(VPack.java:560)
	at com.arangodb.velocypack.VPack.deserializeCollection(VPack.java:597)
	...

VPackParser fails on valid json

When trying to parse json containing array with multiple objects with null values parser fails with exception.

String json = "{\"values\": [ {\"a\": null}, {\"b\": null}]}";
VPackParser parser = new VPackParser.Builder().build();
parser.fromJson(json);

Results in:

Unexpected exception at position -1: null
	at com.arangodb.velocypack.VPackParser$VPackContentHandler.add(VPackParser.java:386)
	at com.arangodb.velocypack.VPackParser$VPackContentHandler.startObject(VPackParser.java:444)
	at org.json.simple.parser.JSONParser.parse(JSONParser.java:488)
	at org.json.simple.parser.JSONParser.parse(JSONParser.java:301)
	at org.json.simple.parser.JSONParser.parse(JSONParser.java:295)
	at com.arangodb.velocypack.VPackParser.fromJson(VPackParser.java:339)
	at com.arangodb.velocypack.VPackParser.fromJson(VPackParser.java:331)

I'm using java-velocypack version 1.0.9

provide documentation on how to create single element slice

Excuse the code (working with Clojure)
As a slice can contain a single element.

(-> (new VPackBuilder)
    (.add ValueType/OBJECT)
    (.add "foo" "bar")
    .close
    .slice
    (.get "foo"))

#object[com.arangodb.velocypack.VPackSlice 0x2b9072bd "\"bar\""]

Is there a canonical way of creating a single element slice?

(-> (new VPackBuilder)
    (.add ValueType/ARRAY)
    (.add "bar")
    .close
    .slice
    (.get 0))

#object[com.arangodb.velocypack.VPackSlice 0x1eb0779a "\"bar\""]

Allow use of type tokens in `VPackDeserializationContext` for deserializing generic Lists

The VPackDeserializationContext currently takes a Class<T> as a parameter. The underlying code does not require a Class<T>, it only requires the more general Type.

By accepting a Type in the VPackDeserializationContext.deserialize interface method rather than Class<T>, the user is free to pass in other implementations of Type, such as ParameterizedType which can be used to deserialize Lists with a parameterized type, as well as Maps). These parameters can be generated and "reified" by using a "type token" implementation, such as is provided in Guava, and indeed in quite a few other libraries as well.

Along with some minor additional code in VPack.java to deal with deserializing wildcard types e.g. if we try to deserialize into a list like List<? extends Foo> this works quite well.

This does also require a minor change in a couple of places in the java driver to cast the result coming back from this call, because java's type inference sucks. I suppose for the same reason this is also a backward incompatible change, and so this probably requires a major version bump. However, I've left the versions alone for now.

VPackBuilder builds invalid unindexed single (small) value array

Consider the following (kotlin) code, using java-velocypack 2.2.1:

val slice = with(VPackBuilder(object : VPackBuilder.BuilderOptions {
	override fun isBuildUnindexedArrays() = true
	override fun isBuildUnindexedObjects() = true

	override fun setBuildUnindexedArrays(buildUnindexedArrays : Boolean) = throw NotImplementedError()
	override fun setBuildUnindexedObjects(buildUnindexedObjects : Boolean) = throw NotImplementedError()
}))
{
	add(ValueType.ARRAY)
	add(1)
	close()
	slice()
}

println(slice)
println(slice.toByteArray().toList())

Expected output would be (note that the byte values are base 10):

[1]
[19, 4, 49, 1]

Actual output is:

["(non-representable type)"]
[19, 4, 0, 1]

The output is as expected (albeit with a larger byte array) when isBuildUnindexedArrays() = false. The output is also as expected when not using small integer values.

The VPackBuilder seems to write the 1 correctly as 49 into its buffer, but fails to copy it to the correct position in VPackBuilder::closeCompactArrayOrObject.

DateUtil does incorrect conversion of UTC time

Hi,

the com.arangodb.velocypack.internal.util.DateUtil does incorrect conversion of java.util.Date objects. The error is that the format is set to ISO 8601 in UTC but the time zone is not taken into account.
A date, e.g. Mon Apr 16 17:17:21 CEST 2018 would be converted to 2018-04-16T17:17:21Z, but correct would be 2018-04-16T15:17:21Z.

Fix:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); 
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));

Best regards

Serialization of negative int value

When serializing an object having negative int value for one of its instance field, the returned serialized object have a weird value instead of the negative int value.

For example, if I call the following piece of code with an object having -50 as value for one of its int field, the serialized value for this field is 4294967246;

VPack vpack = vpack();
System.out.println(vpack.serialize(object).toString());

Support for heterogeneous arrays

When serializing an array of an abstract class or interface, a type hint should be added to the items, just like it's done for generic lists.

Switch to a Jackson for Json

Hi, can the json implementation be switched to Jackson? Jackson is standardized and used across the board in most projects. json-simple creates an extra dependency for us. Also as it's more heavily tested any issues, such as security are better looked after.

As an added bonus Jackon is faster. If size is an issue please look at jackson-jr.

Thanks!

How to migrate from @SerializedName an @Expose

With the latest update, the annotations @SerializezdName and @expose have been removed from this library and as far as I can tell, the functionality those provided no longer exists. Is there a migration process to move code that used those annotations to the latest version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.