Interface Codec<T>

  • Type Parameters:
    T - desired resulting type

    public interface Codec<T>
    Codec instances are used to convert a 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.

    See Also:
    FaunaField, FaunaConstructor, FaunaEnum, FaunaIgnore, Encoder, Decoder, Language.Value(Object), Result