Package com.faunadb.client.types
Interface Codec<T>
-
- Type Parameters:
T
- desired resulting type
public interface Codec<T>
Codec instances are used to convert aValue
to a concrete type. There are pre-defined codecs for each FaunaDB primitive types. Each Codec implementation returns aResult
instance for the conversion attempt. If the conversion attempt fails, theResult
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
andFaunaEnum
for more details.- See Also:
FaunaField
,FaunaConstructor
,FaunaEnum
,FaunaIgnore
,Encoder
,Decoder
,Language.Value(Object)
,Result
-
-
Field Summary
Fields Modifier and Type Field Description static Codec<List<Value>>
ARRAY
static Codec<Boolean>
BOOLEAN
static Codec<Byte>
BYTE
static Codec<byte[]>
BYTES
Converts aValue
to an array of bytesstatic Codec<Character>
CHAR
static Codec<LocalDate>
DATE
static Codec<Double>
DOUBLE
static Codec<Float>
FLOAT
static Codec<Integer>
INTEGER
static Codec<Long>
LONG
static Codec<Map<String,Value>>
OBJECT
static Codec<Value.RefV>
REF
Converts aValue
to aValue.RefV
static Codec<Value.SetRefV>
SET_REF
Converts aValue
to aValue.SetRefV
static Codec<Short>
SHORT
static Codec<String>
STRING
static Codec<Instant>
TIME
static Codec<Value>
VALUE
Convert aValue
to itself.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description Result<T>
decode(Value value)
Result<Value>
encode(T value)
-
-
-
Field Detail
-
REF
static final Codec<Value.RefV> REF
Converts aValue
to aValue.RefV
-
SET_REF
static final Codec<Value.SetRefV> SET_REF
Converts aValue
to aValue.SetRefV
-
-