PAK Core – IDataTransformer
1. What you will learn
In this short summary you will get an idea how you can transform the format of PAKs output data.
2. Basics
As mentioned in this summary, the IDataTransformer plays a major role in persisting data in PAK. It can be used to map between a specified output format and JSON, which PAK uses internally. By default, the output format is JSON as well.
3. Signature
Before giving a short example on how the interface should be used, this section describes the signature of the IDataTransformer<T>
interface, as well as an overview on which methods have to be implemented.
|
Converts the given JSON to the desired representation |
|
Converts the specified scheme to JSON |
|
Checks if the given object is of the desired output type |
|
Returns a set of class names that are excluded from being serialized, defaults to an empty set |
In this context, scheme refers to the output format, that is specified by the transformer.
All methods but getExcludeFromConversion have to be implemented.
|
4. Usage
This section briefly outlines a few examples demonstrating how the implementing Transformer should be used.
4.1. Data Transformation
Let’s suppose our persistence can only handle data stored in xml. In order to reduce the workload of the IPersistenceService, we implement a custom transformer to handle the conversions.
{
"name" : "Max",
"age" : 33,
"hobbies" : [
{
"name" : "Soccer",
"id" : 0
},
{
"name" : "Coding",
"id" : 1
},
{
"name" : "Automating Workflows",
"id" : 2
}
]
}
Any data passing the persistence will be in the JSON format by default. The persistence service then calls IDataTransformer#convertJSONToScheme(…)
to prepare it for the store operation. In our case the result of this would something like follows.
<person name="Max" age="33">
<hobbies>
<hobby name="Soccer" id="0" />
<hobby name="Coding" id="1" />
<hobby name="Automating Workflows" id="2" />
</hobbies>
</person>
We also need to handle the other way around. In order to prepare our data for PAK (after the find operation), IDataTransformer#convertSchemeToJSON(…)
is called and should result in the JSON above.
The IDataTransformer#isScheme(…)
method is solely responsible for detecting if the given data is already in the desired format, so in our case isScheme(json) = false
and isScheme(xml) = true
.
4.2. Data Exclusion
Sometimes it may be the case that data cannot be converted to json due to limitations. Should such a case be applicable to your environment, you can also choose to exclude specific class/package names to be converted at all.
All referenced classes have to be available at runtime |
If this applies to some of your classes, make sure that your persistence can handle the (de-)serialization of java objects.
5. Default Implementations
-
The
de.asap.pak.extra.impl.datatransformer.PakDefaultDataTransformer
: Transforms data from and to the PAK data scheme. -
The
de.asap.pak.core.testutils.datatransformer.DataTransfomerImpl
: Does not convert any objects at all. Can be used for migrating tests or to simplify
tests, which write content into the persistence. -
The
de.asap.pak.core.testutils.datatransformer.DataTransformerMock
: Helps running tests forIDataTransformer
. Will fallback to the methods
ofTestDataTransformer
when nothing is explicitly mocked. -
The
de.asap.pak.core.testutils.datatransformer.TestDataTransformer
: Provides spy functionality when testing theIDataTransformer
.