Skip to content

Commit d86dc9e

Browse files
committed
ArangoDB: added JsonbSerde
1 parent 2004c05 commit d86dc9e

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBConfiguration.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,27 @@
1111
* Contributors:
1212
*
1313
* Otavio Santana
14+
* Michele Rastelli
1415
*/
1516
package org.eclipse.jnosql.databases.arangodb.communication;
1617

1718

1819
import com.arangodb.ArangoDB;
1920
import com.arangodb.entity.LoadBalancingStrategy;
21+
import com.arangodb.serde.ArangoSerde;
2022
import org.eclipse.jnosql.communication.Settings;
2123

2224
import static java.util.Objects.requireNonNull;
2325

2426
/**
25-
* The base to configuration both key-value and document on mongoDB.
27+
* The base to configuration both key-value and document on ArangoDB.
2628
* To each configuration set, it will change both builder
2729
* {@link ArangoDB.Builder}
2830
*/
2931
public abstract class ArangoDBConfiguration {
3032

31-
32-
protected ArangoDB.Builder builder = new ArangoDB.Builder();
33+
protected ArangoDB.Builder builder = new ArangoDB.Builder()
34+
.serde(new JsonbSerde());
3335

3436
/**
3537
* Adds a host in the arangodb builder
@@ -54,7 +56,6 @@ public void setLoadBalancingStrategy(LoadBalancingStrategy loadBalancingStrategy
5456
builder.loadBalancingStrategy(loadBalancingStrategy);
5557
}
5658

57-
5859
/**
5960
* set the setTimeout
6061
*
@@ -91,6 +92,21 @@ public void setUseSSL(boolean value) {
9192
builder.useSsl(value);
9293
}
9394

95+
/**
96+
* Set the ArangoDB serde for the user data. Note that the provided
97+
* serde must support serializing and deserializing JsonP types,
98+
* i.e. {@link jakarta.json.JsonValue} and its children.
99+
* By default, the builder is configured to use {@link JsonbSerde};
100+
* this setter allows overriding it, i.e. providing an instance of
101+
* {@link JsonbSerde} that uses a specific {@link jakarta.json.bind.Jsonb}
102+
* instance.
103+
*
104+
* @param serde the serde
105+
*/
106+
public void setSerde(ArangoSerde serde) {
107+
builder.serde(serde);
108+
}
109+
94110
/**
95111
* Defines a new builder to sync ArangoDB
96112
*
@@ -102,12 +118,10 @@ public void syncBuilder(ArangoDB.Builder builder) throws NullPointerException {
102118
this.builder = builder;
103119
}
104120

105-
106121
protected ArangoDB getArangoDB(Settings settings) {
107122
ArangoDBBuilderSync aragonDB = new ArangoDBBuilderSync(builder);
108123
ArangoDBBuilders.load(settings, aragonDB);
109124
return aragonDB.build();
110125
}
111126

112-
113127
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2022 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Michele Rastelli
14+
*/
15+
package org.eclipse.jnosql.databases.arangodb.communication;
16+
17+
import com.arangodb.serde.ArangoSerde;
18+
import jakarta.json.bind.Jsonb;
19+
import org.eclipse.jnosql.communication.driver.JsonbSupplier;
20+
21+
import java.nio.charset.StandardCharsets;
22+
23+
/**
24+
* ArangoDB user-data serde that serializes and deserializes user data using JSONB.
25+
* This supports natively JsonP types, i.e. {@link jakarta.json.JsonValue} and its children.
26+
*/
27+
public class JsonbSerde implements ArangoSerde {
28+
29+
private final Jsonb jsonb;
30+
31+
public JsonbSerde() {
32+
this(JsonbSupplier.getInstance().get());
33+
}
34+
35+
/**
36+
* Alternative constructor to provide {@link Jsonb} instance to use,
37+
* i.e. using custom configuration, see {@link jakarta.json.bind.JsonbBuilder#create(jakarta.json.bind.JsonbConfig)}
38+
* @param jsonb Jsonb
39+
*/
40+
public JsonbSerde(Jsonb jsonb) {
41+
this.jsonb = jsonb;
42+
}
43+
44+
@Override
45+
public byte[] serialize(Object value) {
46+
return jsonb.toJson(value).getBytes(StandardCharsets.UTF_8);
47+
}
48+
49+
@Override
50+
public <T> T deserialize(byte[] content, Class<T> type) {
51+
return jsonb.fromJson(new String(content, StandardCharsets.UTF_8), type);
52+
}
53+
54+
}

0 commit comments

Comments
 (0)