Package com.faunadb.client.types
Interface Codec<T>
-
- Type Parameters:
T- desired resulting type
public interface Codec<T>Codec instances are used to convert aValueto a concrete type. There are pre-defined codecs for each FaunaDB primitive types. Each Codec implementation returns aResultinstance for the conversion attempt. If the conversion attempt fails, theResultinstance 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,FaunaIgnoreandFaunaEnumfor 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>>ARRAYstatic Codec<Boolean>BOOLEANstatic Codec<Byte>BYTEstatic Codec<byte[]>BYTESConverts aValueto an array of bytesstatic Codec<Character>CHARstatic Codec<LocalDate>DATEstatic Codec<Double>DOUBLEstatic Codec<Float>FLOATstatic Codec<Integer>INTEGERstatic Codec<Long>LONGstatic Codec<Map<String,Value>>OBJECTstatic Codec<Value.RefV>REFConverts aValueto aValue.RefVstatic Codec<Value.SetRefV>SET_REFConverts aValueto aValue.SetRefVstatic Codec<Short>SHORTstatic Codec<String>STRINGstatic Codec<Instant>TIMEstatic Codec<Value>VALUEConvert aValueto 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 aValueto aValue.RefV
-
SET_REF
static final Codec<Value.SetRefV> SET_REF
Converts aValueto aValue.SetRefV
-
-