T
- desired resulting typepublic interface Codec<T>
Value
to a concrete type.
There are pre-defined codecs for each FaunaDB primitive types.
Each Codec implementation returns a Result
instance for the conversion attempt.
If the conversion attempt fails, the Result
instance will contain an error message.
Custom codecs can be created by implementing the Codec interface.
Example:
class User {
static final Codec<User> USER_CODEC = new Codec<User>() {
public Result<User> decode(Value value) {
return Result.success(new User(
value.at("username").to(String.class).getOrElse("<no name>"),
value.at("password").to(String.class).getOrElse(null)
));
}
public Result<Value> encode(User user) {
return Value.from(ImmutableMap.of(
"username", user.username,
"password", person.password
));
}
}
final String username, password;
Person(String username, String password) {
this.firstName = firstName;
this.lastName = lastName;
}
}
//...
Value result = client.query(getUser).get();
Result<User> user = result.at("data").to(User.USER_CODEC);
It is possible to annotate classes and let the internal framework encode/decode instances of the class automatically.
Refer to the annotations FaunaField
, FaunaConstructor
, FaunaIgnore
and FaunaEnum
for more details.
FaunaField
,
FaunaConstructor
,
FaunaEnum
,
FaunaIgnore
,
Encoder
,
Decoder
,
Language.Value(Object)
,
Result
Modifier and Type | Field and Description |
---|---|
static Codec<java.util.List<Value>> |
ARRAY
|
static Codec<java.lang.Boolean> |
BOOLEAN
Converts a
Value to a Boolean |
static Codec<java.lang.Byte> |
BYTE
Converts a
Value to a Byte |
static Codec<byte[]> |
BYTES
Converts a
Value to an array of bytes |
static Codec<java.lang.Character> |
CHAR
Converts a
Value to a Character |
static Codec<java.time.LocalDate> |
DATE
Converts a
Value to a LocalDate |
static Codec<java.lang.Double> |
DOUBLE
Converts a
Value to a Double |
static Codec<java.lang.Float> |
FLOAT
Converts a
Value to a Float |
static Codec<java.lang.Integer> |
INTEGER
Converts a
Value to a Integer |
static Codec<java.lang.Long> |
LONG
Converts a
Value to a Long |
static Codec<java.util.Map<java.lang.String,Value>> |
OBJECT
|
static Codec<Value.RefV> |
REF
Converts a
Value to a Value.RefV |
static Codec<Value.SetRefV> |
SET_REF
Converts a
Value to a Value.SetRefV |
static Codec<java.lang.Short> |
SHORT
Converts a
Value to a Short |
static Codec<java.lang.String> |
STRING
Converts a
Value to a String |
static Codec<java.time.Instant> |
TIME
Converts a
Value to an Instant |
static Codec<Value> |
VALUE
Convert a
Value to itself. |
Modifier and Type | Method and Description |
---|---|
Result<T> |
decode(Value value) |
Result<Value> |
encode(T value) |
static final Codec<Value.RefV> REF
Value
to a Value.RefV
static final Codec<Value.SetRefV> SET_REF
Value
to a Value.SetRefV