com.basho.riak.client.convert
Class JSONConverter<T>

java.lang.Object
  extended by com.basho.riak.client.convert.JSONConverter<T>
All Implemented Interfaces:
Converter<T>

public class JSONConverter<T>
extends Object
implements Converter<T>

Converts a RiakObject's value to an instance of T. T must have a field annotated with RiakKey or you must construct the converter with a key to use. RiakObject's value *must* be a JSON string.

At present user meta data and RiakLinks are not converted. This means they are essentially lost in translation.

Author:
russell

Constructor Summary
JSONConverter(Class<T> clazz, String bucket)
          Create a JSONConverter for creating instances of clazz from JSON and instances of IRiakObject with a JSON payload from instances of clazz
JSONConverter(Class<T> clazz, String bucket, String defaultKey)
          Create a JSONConverter for creating instances of clazz from JSON and instances of IRiakObject with a JSON payload from instances of clazz
 
Method Summary
 IRiakObject fromDomain(T domainObject, VClock vclock)
          Converts domainObject to a JSON string and sets that as the payload of a IRiakObject.
 T toDomain(IRiakObject riakObject)
          Converts the value of riakObject to an instance of T.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JSONConverter

public JSONConverter(Class<T> clazz,
                     String bucket)
Create a JSONConverter for creating instances of clazz from JSON and instances of IRiakObject with a JSON payload from instances of clazz

Parameters:
clazz - the type to convert to/from
b - the bucket

JSONConverter

public JSONConverter(Class<T> clazz,
                     String bucket,
                     String defaultKey)
Create a JSONConverter for creating instances of clazz from JSON and instances of IRiakObject with a JSON payload from instances of clazz

Parameters:
clazz - the type to convert to/from
bucket - the bucket
defaultKey - for cases where clazz does not have a RiakKey annotated field, pass the key to use in this conversion.
Method Detail

fromDomain

public IRiakObject fromDomain(T domainObject,
                              VClock vclock)
                       throws ConversionException
Converts domainObject to a JSON string and sets that as the payload of a IRiakObject. Also set the content-type to application/json;charset=UTF-8

Specified by:
fromDomain in interface Converter<T>
Parameters:
domainObject - to be converted
vclock - the vector clock from Riak
Returns:
a RiakObject populated from domainObject
Throws:
ConversionException

toDomain

public T toDomain(IRiakObject riakObject)
           throws ConversionException
Converts the value of riakObject to an instance of T.

Beware: at present links and user meta are not converted at present: this is on the way.

Specified by:
toDomain in interface Converter<T>
Parameters:
riakObject - the IRiakObject to convert to instance of T. NOTE: riakObject.getValue() must be a JSON string. The charset from riakObject.getContentType() is used.
Returns:
an instance of type T
Throws:
ConversionException


Copyright © 2012. All Rights Reserved.