Custom Transformers
If you don't like how we set up data serialization or just have super-custom requirements, you can provide your own data marshaller to have full control. Create a CFC that implements the cfcouchbase.data.IDataMarshaller interface. It only needs to have three methods:
  • serializeData() - Returns the data in a string form so it can be persisted in Couchbase
  • deserializeData() - Received the raw string data from Couchbase and inflates it as necessary to the original state
  • setCouchbaseClient() - Gives the marshaller a chance to store a local reference to the client in case it needs to talk back.
1
interface{
2
any function setCouchbaseClient( required couchcbaseClient );
3
any function deserializeData( required string ID, required string data, any inflateTo="", struct deserializeOptions={} );
4
string function serializeData( required any data );
5
}
Copied!

myDataMarshaller.cfc

1
component implements='cfcouchbase.data.IDataMarshaller' {
2
3
any function setCouchbaseClient( required couchcbaseClient ){
4
variables.couchbaseClient = arguments.couchcbaseClient;
5
return this;
6
}
7
8
string function serializeData( required any data ){
9
if( !isSimpleValue( data ) ) {
10
return serializeJSON( data );
11
}
12
return data;
13
}
14
15
any function deserializeData( required string data, any inflateTo="", struct deserializeOptions={} ){
16
if( isJSON( data ) && deserialize ) {
17
return deserializeJSON( data );
18
}
19
return data;
20
}
21
22
}
Copied!
After you have created your custom marshaller, simply pass in an instance of it or the full component path as a config setting:
1
// path
2
couchbase = new cfcouchbase.CouchbaseClient(
3
{
4
bucketName = 'myBucket',
5
dataMarshaller = 'path.to.myDataMarshaller'
6
}
7
);
8
// instance
9
couchbase = new cfcouchbase.CouchbaseClient(
10
{
11
bucketName = 'myBucket',
12
dataMarshaller = new path.to.myDataMarshaller()
13
}
14
);
Copied!
Tip Once you specify a custom data marshaller, you are overriding all Data Serialization functionality above. So you are on your own now buddy! Like good 'ol spidey says: With Much Power Comes Much Responsibility!
Copy link
Edit on GitHub