com.arangodb.velocypack.ValueType Java Examples

The following examples show how to use com.arangodb.velocypack.ValueType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
private void writeArray(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	if (byte[].class.equals(source.getClass())) {
		sink.add(attribute, Base64Utils.encodeToString((byte[]) source));
	}

	else {
		sink.add(attribute, ValueType.ARRAY);
		for (int i = 0; i < Array.getLength(source); ++i) {
			final Object element = Array.get(source, i);
			writeInternal(null, element, sink, getNonNullComponentType(definedType));
		}
		sink.close();
	}
}
 
Example #2
Source File: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObjectInObject() throws VPackException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT);// object start
    builder.add("foo", ValueType.OBJECT); // add object in field "foo"
    builder.add("bar", 2); // add field "bar" with value 2 to object "foo"
    builder.close();// object "foo" end
    builder.close();// object end

    final VPackSlice slice = builder.slice(); // create slice
    assertThat(slice.isObject(), is(true));

    final VPackSlice foo = slice.get("foo");
    assertThat(foo.isObject(), is(true));

    final VPackSlice bar = foo.get("bar"); // get field "bar" from "foo"
    assertThat(bar.isInteger(), is(true));
}
 
Example #3
Source File: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObject() throws VPackException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT);// object start
    builder.add("foo", 1); // add field "foo" with value 1
    builder.add("bar", 2); // add field "bar" with value 2
    builder.close();// object end

    final VPackSlice slice = builder.slice(); // create slice
    assertThat(slice.isObject(), is(true));
    assertThat(slice.size(), is(2)); // number of fields

    final VPackSlice foo = slice.get("foo"); // get field "foo"
    assertThat(foo.isInteger(), is(true));
    assertThat(foo.getAsInt(), is(1));

    final VPackSlice bar = slice.get("bar"); // get field "bar"
    assertThat(bar.isInteger(), is(true));
    assertThat(bar.getAsInt(), is(2));

    // iterate over the fields
    for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext(); ) {
        final Entry<String, VPackSlice> field = iterator.next();
        assertThat(field.getValue().isInteger(), is(true));
    }
}
 
Example #4
Source File: VPackExample.java    From arangodb-java-driver-async with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", 1); // add field "foo" with value 1
	builder.add("bar", 2); // add field "bar" with value 2
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));
	assertThat(slice.size(), is(2)); // number of fields

	final VPackSlice foo = slice.get("foo"); // get field "foo"
	assertThat(foo.isInteger(), is(true));
	assertThat(foo.getAsInt(), is(1));

	final VPackSlice bar = slice.get("bar"); // get field "bar"
	assertThat(bar.isInteger(), is(true));
	assertThat(bar.getAsInt(), is(2));

	// iterate over the fields
	for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext();) {
		final Entry<String, VPackSlice> field = iterator.next();
		assertThat(field.getValue().isInteger(), is(true));
	}
}
 
Example #5
Source File: VPackExample.java    From arangodb-java-driver-async with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObjectInObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", ValueType.OBJECT); // add object in field "foo"
	builder.add("bar", 2); // add field "bar" with value 2 to object "foo"
	builder.close();// object "foo" end
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));

	final VPackSlice foo = slice.get("foo");
	assertThat(foo.isObject(), is(true));

	final VPackSlice bar = foo.get("bar"); // get field "bar" from "foo"
	assertThat(bar.isInteger(), is(true));
}
 
Example #6
Source File: BaseDocumentTest.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void deserialize() throws VPackException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT);
    builder.add("_id", "test/test");
    builder.add("_key", "test");
    builder.add("_rev", "test");
    builder.add("a", "a");
    builder.close();

    final VPack.Builder vbuilder = new VPack.Builder();
    vbuilder.registerModule(new VPackDriverModule());
    final VPack vpacker = vbuilder.build();

    final BaseDocument entity = vpacker.deserialize(builder.slice(), BaseDocument.class);
    assertThat(entity.getId(), is(notNullValue()));
    assertThat(entity.getId(), is("test/test"));
    assertThat(entity.getKey(), is(notNullValue()));
    assertThat(entity.getKey(), is("test"));
    assertThat(entity.getRevision(), is(notNullValue()));
    assertThat(entity.getRevision(), is("test"));
    assertThat(entity.getProperties().size(), is(1));
    assertThat(String.valueOf(entity.getAttribute("a")), is("a"));
}
 
Example #7
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 6 votes vote down vote up
private void writeMap(
	final String attribute,
	final Map<? extends Object, ? extends Object> source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	sink.add(attribute, ValueType.OBJECT);

	for (final Entry<? extends Object, ? extends Object> entry : source.entrySet()) {
		final Object key = entry.getKey();
		final Object value = entry.getValue();

		writeInternal(convertId(key), value, sink, getNonNullMapValueType(definedType));
	}

	sink.close();
}
 
Example #8
Source File: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", 1); // add field "foo" with value 1
	builder.add("bar", 2); // add field "bar" with value 2
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));
	assertThat(slice.size(), is(2)); // number of fields

	final VPackSlice foo = slice.get("foo"); // get field "foo"
	assertThat(foo.isInteger(), is(true));
	assertThat(foo.getAsInt(), is(1));

	final VPackSlice bar = slice.get("bar"); // get field "bar"
	assertThat(bar.isInteger(), is(true));
	assertThat(bar.getAsInt(), is(2));

	// iterate over the fields
	for (final Iterator<Entry<String, VPackSlice>> iterator = slice.objectIterator(); iterator.hasNext(); ) {
		final Entry<String, VPackSlice> field = iterator.next();
		assertThat(field.getValue().isInteger(), is(true));
	}
}
 
Example #9
Source File: VPackExample.java    From arangodb-java-driver with Apache License 2.0 6 votes vote down vote up
@Test
public void buildObjectInObject() throws VPackException {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT);// object start
	builder.add("foo", ValueType.OBJECT); // add object in field "foo"
	builder.add("bar", 2); // add field "bar" with value 2 to object "foo"
	builder.close();// object "foo" end
	builder.close();// object end

	final VPackSlice slice = builder.slice(); // create slice
	assertThat(slice.isObject(), is(true));

	final VPackSlice foo = slice.get("foo");
	assertThat(foo.isObject(), is(true));

	final VPackSlice bar = foo.get("bar"); // get field "bar" from "foo"
	assertThat(bar.isInteger(), is(true));
}
 
Example #10
Source File: VPackSerializers.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
private static void serializeFieldLinks(final VPackBuilder builder, final Collection<FieldLink> links) {
    if (!links.isEmpty()) {
        builder.add("fields", ValueType.OBJECT);
        for (final FieldLink fieldLink : links) {
            builder.add(fieldLink.getName(), ValueType.OBJECT);
            final Collection<String> analyzers = fieldLink.getAnalyzers();
            if (!analyzers.isEmpty()) {
                builder.add("analyzers", ValueType.ARRAY);
                for (final String analyzer : analyzers) {
                    builder.add(analyzer);
                }
                builder.close();
            }
            final Boolean includeAllFields = fieldLink.getIncludeAllFields();
            if (includeAllFields != null) {
                builder.add("includeAllFields", includeAllFields);
            }
            final Boolean trackListPositions = fieldLink.getTrackListPositions();
            if (trackListPositions != null) {
                builder.add("trackListPositions", trackListPositions);
            }
            final StoreValuesType storeValues = fieldLink.getStoreValues();
            if (storeValues != null) {
                builder.add("storeValues", storeValues.name().toLowerCase());
            }
            serializeFieldLinks(builder, fieldLink.getFields());
            builder.close();
        }
        builder.close();
    }
}
 
Example #11
Source File: InsertDocumentExample.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void insertVPack() throws ExecutionException, InterruptedException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT).add("foo", "bar").close();
    collection.insertDocument(builder.slice())
            .whenComplete((doc, ex) -> assertThat(doc.getKey(), is(notNullValue())))
            .get();
}
 
Example #12
Source File: ArangoDatabaseTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionVPackArray() throws VPackException {
    final VPackSlice params = new VPackBuilder().add(ValueType.ARRAY).add("hello").add("world").close().slice();
    final TransactionOptions options = new TransactionOptions().params(params);
    final String result = db
            .transaction("function (params) { return params[0] + ' ' + params[1];}", String.class, options);
    assertThat(result, is("hello world"));
}
 
Example #13
Source File: ArangoDatabaseTest.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void transactionVPackObject() throws VPackException {
    final VPackSlice params = new VPackBuilder().add(ValueType.OBJECT).add("foo", "hello").add("bar", "world")
            .close().slice();
    final TransactionOptions options = new TransactionOptions().params(params);
    final String result = db
            .transaction("function (params) { return params['foo'] + ' ' + params['bar'];}", String.class, options);
    assertThat(result, is("hello world"));
}
 
Example #14
Source File: InsertDocumentExample.java    From arangodb-java-driver with Apache License 2.0 5 votes vote down vote up
@Test
public void insertVPack() {
	final VPackBuilder builder = new VPackBuilder();
	builder.add(ValueType.OBJECT).add("foo", "bar").close();
	final DocumentCreateEntity<VPackSlice> doc = collection.insertDocument(builder.slice());
	assertThat(doc.getKey(), is(notNullValue()));
}
 
Example #15
Source File: InsertDocumentExample.java    From arangodb-java-driver-async with Apache License 2.0 5 votes vote down vote up
@Test
public void insertVPack() throws ExecutionException, InterruptedException {
    final VPackBuilder builder = new VPackBuilder();
    builder.add(ValueType.OBJECT).add("foo", "bar").close();
    collection.insertDocument(builder.slice())
            .whenComplete((doc, ex) -> assertThat(doc.getKey(), is(notNullValue())))
            .get();
}
 
Example #16
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 5 votes vote down vote up
private void writeCollection(
	final String attribute,
	final Object source,
	final VPackBuilder sink,
	final TypeInformation<?> definedType) {

	sink.add(attribute, ValueType.ARRAY);

	for (final Object entry : asCollection(source)) {
		writeInternal(null, entry, sink, getNonNullComponentType(definedType));
	}

	sink.close();
}
 
Example #17
Source File: ArangoSerializationTest.java    From arangodb-java-driver with Apache License 2.0 4 votes vote down vote up
@Test
public void deseriarlize() {
    final VPackBuilder builder = new VPackBuilder().add(ValueType.OBJECT).add("foo", "bar").close();
    final BaseDocument doc = util.deserialize(builder.slice(), BaseDocument.class);
    assertThat(doc.getAttribute("foo").toString(), is("bar"));
}
 
Example #18
Source File: CustomMappingTest.java    From spring-data with Apache License 2.0 4 votes vote down vote up
@Override
public VPackSlice convert(final CustomVPackTestEntity source) {
	return new VPackBuilder().add(ValueType.OBJECT).add(FIELD, source.getValue()).close().slice();
}
 
Example #19
Source File: DefaultArangoConverter.java    From spring-data with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeSimple(final String attribute, final Object source, final VPackBuilder sink) {
	if (source == null) {
		sink.add(ValueType.NULL);
	}
	// com.arangodb.*
	else if (source instanceof VPackSlice) {
		sink.add(attribute, (VPackSlice) source);
	} //
	else if (source instanceof DBDocumentEntity) {
		writeMap(attribute, (Map<String, Object>) source, sink, ClassTypeInformation.MAP);
	}
	// java.lang.*
	else if (source instanceof Boolean) {
		sink.add(attribute, (Boolean) source);
	} //
	else if (source instanceof Byte) {
		sink.add(attribute, (Byte) source);
	} //
	else if (source instanceof Character) {
		sink.add(attribute, (Character) source);
	} //
	else if (source instanceof Short) {
		sink.add(attribute, (Short) source);
	} //
	else if (source instanceof Integer) {
		sink.add(attribute, (Integer) source);
	} //
	else if (source instanceof Long) {
		sink.add(attribute, (Long) source);
	} //
	else if (source instanceof Float) {
		sink.add(attribute, (Float) source);
	} //
	else if (source instanceof Double) {
		sink.add(attribute, (Double) source);
	} //
	else if (source instanceof String) {
		sink.add(attribute, (String) source);
	} //
	else if (source instanceof Class) {
		sink.add(attribute, ((Class<?>) source).getName());
	} //
	else if (source instanceof Enum) {
		sink.add(attribute, ((Enum<?>) source).name());
	}
	// primitive arrays
	else if (ClassUtils.isPrimitiveArray(source.getClass())) {
		writeArray(attribute, source, sink, ClassTypeInformation.OBJECT);
	}
	// java.util.Date / java.sql.Date / java.sql.Timestamp
	else if (source instanceof Date) {
		sink.add(attribute, DateUtil.format((Date) source));
	}
	// java.math.*
	else if (source instanceof BigInteger) {
		sink.add(attribute, (BigInteger) source);
	} //
	else if (source instanceof BigDecimal) {
		sink.add(attribute, (BigDecimal) source);
	}
	// java.time.*
	else if (source instanceof Instant) {
		sink.add(attribute, JavaTimeUtil.format((Instant) source));
	} //
	else if (source instanceof LocalDate) {
		sink.add(attribute, JavaTimeUtil.format((LocalDate) source));
	} //
	else if (source instanceof LocalDateTime) {
		sink.add(attribute, JavaTimeUtil.format((LocalDateTime) source));
	} //
	else if (source instanceof OffsetDateTime) {
		sink.add(attribute, JavaTimeUtil.format((OffsetDateTime) source));
	} //
	else if (source instanceof ZonedDateTime) {
		sink.add(attribute, JavaTimeUtil.format((ZonedDateTime) source));
	} //
	else {
		throw new MappingException(String.format("Type %s is not a simple type!", source.getClass()));
	}
}
 
Example #20
Source File: ArangoDBAsync.java    From arangodb-java-driver-async with Apache License 2.0 3 votes vote down vote up
/**
 * Register a custom {@link VPackJsonDeserializer} for a specific type and attribute name to be used within the
 * internal serialization process.
 * 
 * <p>
 * <strong>Attention:</strong>can not be used together with {@link #serializer(ArangoSerialization)}
 * </p>
 * 
 * @param attribute
 * @param type
 *            the type the serializer should be registered for
 * @param deserializer
 * @return {@link ArangoDBAsync.Builder}
 */
public Builder registerJsonDeserializer(
	final String attribute,
	final ValueType type,
	final VPackJsonDeserializer deserializer) {
	vpackParserBuilder.registerDeserializer(attribute, type, deserializer);
	return this;
}
 
Example #21
Source File: ArangoDBAsync.java    From arangodb-java-driver-async with Apache License 2.0 2 votes vote down vote up
/**
 * Register a custom {@link VPackJsonDeserializer} for a specific type to be used within the internal
 * serialization process.
 * 
 * <p>
 * <strong>Attention:</strong>can not be used together with {@link #serializer(ArangoSerialization)}
 * </p>
 * 
 * @param type
 *            the type the serializer should be registered for
 * @param deserializer
 * @return {@link ArangoDBAsync.Builder}
 */
public Builder registerJsonDeserializer(final ValueType type, final VPackJsonDeserializer deserializer) {
	vpackParserBuilder.registerDeserializer(type, deserializer);
	return this;
}