Generate toRecord And fromRecord Using The GenerateRecordSerializers Tool
Updated: Jan 15, 2024
This example shows how to generate toRecord()
and fromRecord()
methods for any given class. These methods are very useful for Record and JSON serialization in DataPipeline.
Java Code Listing
package com.northconcepts.datapipeline.examples.cookbook; import java.util.HashSet; import com.northconcepts.datapipeline.foundations.tools.GenerateRecordSerializers; public class GenerateRecordSerializersExample { public static void main(String[] args) throws Throwable { HashSettypes = new HashSet<>(); types.add(SimpleOrganization.class.getName()); types.add(SimpleUser.class.getName()); GenerateRecordSerializers serializers = new GenerateRecordSerializers(); serializers.generate(types); } } class SimpleUser { private int id; private String name; private String address; private String phoneNumber; } class SimpleOrganization { private int id; private String name; private SimpleUser manager; }
Code walkthrough
- There are two classes (or DTO/entity) that have some properties.
SimpleUser
&SimpleOrganization
- In
main
method, add these two class names to a set. - Create an instance of
GenerateRecordSerializers
. - Pass the set to
GenerateRecordSerializers.generate(types)
- This will print
toRecord()
&fromRecord()
on console as follows.
Console Output
com.northconcepts.datapipeline.examples.cookbook.GenerateRecordSerializersExample$SimpleUser ------------------------------------- com.northconcepts.datapipeline.examples.cookbook.SimpleUser ------------------------------------- // TODO: Review record serialization @Override public Record toRecord() { // TODO: call Record record = super.toRecord() instead of the following if appropriate Record record = new Record() .setField("id", id) .setField("name", name) .setField("address", address) .setField("phoneNumber", phoneNumber) ; return record; } // TODO: Review record deserialization @Override public SimpleUser fromRecord(Record source) { // TODO: call super.fromRecord(source); if appropriate this.id = source.getFieldValueAsInteger("id", 0); this.name = source.getFieldValueAsString("name", null); this.address = source.getFieldValueAsString("address", null); this.phoneNumber = source.getFieldValueAsString("phoneNumber", null); return this; } com.northconcepts.datapipeline.examples.cookbook.SimpleOrganization ------------------------------------- // TODO: Review record serialization @Override public Record toRecord() { // TODO: call Record record = super.toRecord() instead of the following if appropriate Record record = new Record() .setField("id", id) .setField("name", name) ; // TODO write code to serialize manager return record; } // TODO: Review record deserialization @Override public SimpleOrganization fromRecord(Record source) { // TODO: call super.fromRecord(source); if appropriate this.id = source.getFieldValueAsInteger("id", 0); this.name = source.getFieldValueAsString("name", null); // TODO: write the code to deserialize: SimpleUser manager return this; }