Skip to content

Commit af6eebb

Browse files
committed
new serializer API
1 parent 53d3b23 commit af6eebb

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.arangodb.serde;
2+
3+
import java.lang.reflect.Type;
4+
5+
/**
6+
* Contract for serialization/deserialization of user data.
7+
* Implementations of this interface could be used for customizing serialization/deserialization of user related data
8+
* using serialization/deserialization libraries other than Jackson Databind, like:
9+
* - serialization libraries for specific JVM languages (e.g. Scala, Kotlin, ...)
10+
* - serialization libraries already in use in frameworks (e.g. JSON-B, Micronaut Serialization, ...)
11+
* - high performance serialization libraries (e.g. supporting compile-time databinding code generation)
12+
* - lower level libraries without support to data binding
13+
* <p>
14+
* This interface should not be implemented as an adapter to Jackson Databind, since already existing implementations
15+
* can be instantiated providing a custom configured Jackson ObjectMapper, see {@link JsonSerde#...} and {@link VPackSerde#...}.
16+
*/
17+
public interface ArangoSerde {
18+
19+
/**
20+
* @return the data type supported by this implementation
21+
*/
22+
DataType getDataType();
23+
24+
/**
25+
* Serializes the object into the target data type. For data type {@link DataType#JSON}, the serialized JSON string
26+
* must be encoded into a byte array using the UTF-8 charset.
27+
*
28+
* @param value object to serialize
29+
* @return serialized byte array
30+
*/
31+
byte[] serialize(Object value);
32+
33+
/**
34+
* Deserializes the content and binds it to the target data type.
35+
* For data type {@link DataType#JSON}, the byte array is the JSON string encoded using the UTF-8 charset.
36+
*
37+
* @param content byte array to deserialize
38+
* @param clazz class of target data type
39+
* @return deserialized object
40+
*/
41+
default <T> T deserialize(byte[] content, Class<T> clazz) {
42+
return deserialize(content, (Type) clazz);
43+
}
44+
45+
/**
46+
* Deserializes the content and binds it to the target data type.
47+
* For data type {@link DataType#JSON}, the byte array is the JSON string encoded using the UTF-8 charset.
48+
*
49+
* @param content byte array to deserialize
50+
* @param type target data type
51+
* @return deserialized object
52+
*/
53+
<T> T deserialize(byte[] content, Type type);
54+
55+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* DISCLAIMER
3+
*
4+
* Copyright 2016 ArangoDB GmbH, Cologne, Germany
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
*/
20+
package com.arangodb.serde;
21+
22+
public enum DataType {
23+
JSON, VPACK
24+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.arangodb.serde;
2+
3+
import com.fasterxml.jackson.databind.JsonNode;
4+
5+
import java.lang.reflect.Type;
6+
7+
/**
8+
* Internal serializer/deserializer based on Jackson Databind.
9+
*/
10+
public interface InternalSerde extends ArangoSerde {
11+
12+
/**
13+
* Used for logging and debugging.
14+
*
15+
* @param content byte array
16+
* @return JSON string
17+
*/
18+
String toJsonString(byte[] content);
19+
20+
/**
21+
* Extract the nested content pointed by the json pointer.
22+
* Used for extracting nested user data.
23+
*
24+
* @param content byte array
25+
* @param jsonPointer location of user data
26+
* @return byte array
27+
*/
28+
byte[] extract(byte[] content, String jsonPointer);
29+
30+
/**
31+
* Deserializes the parsed json node and binds it to the target data type.
32+
*
33+
* @param node parsed json node
34+
* @param clazz class of target data type
35+
* @return deserialized object
36+
*/
37+
default <T> T deserialize(JsonNode node, Class<T> clazz) {
38+
return deserialize(node, (Type) clazz);
39+
}
40+
41+
/**
42+
* Deserializes the parsed json node and binds it to the target data type.
43+
*
44+
* @param node parsed json node
45+
* @param type target data type
46+
* @return deserialized object
47+
*/
48+
<T> T deserialize(JsonNode node, Type type);
49+
50+
}

0 commit comments

Comments
 (0)