We have two systems: external and internal, which are sharing information in JSON format (GSON library).
Information from an external system comes in internal and processed here.
Everything was very good, coming from an external system data in JSON format in the internal system data deserialize and processed. For example:
come string:
{UserLogInEvent:{userName:'Name', time:'00.00.00'}}
this string deserialize in object of this class:
UserLogInEvent implement Event {
private String userName;
private Date time;
public UserLogInEvent (String userName, Date time)
{
this.userName = userName;
this.time = time;
}
private UserLogInEvent()
{
this.userName = null;
this.time = null;
}
public String getUserName()
{
return this.userName;
}
public Date time()
{
return this.time;
}
}
or other example:
{UserModifyFile: {userName:'Name',fileName: 'index.txt' time:'00.00.00'}}
UserModifyEvent implement Event {
private String userName;
private String fileName;
private Date time;
public UserLogInEvent (String userName, String fileName, Date time)
{
this.userName = userName;
this.fileName = fileName;
this.time = time;
}
private UserLogInEvent()
{
this.userName = null;
this.fileName = null;
this.time = null;
}
public String getUserName()
{
return this.userName;
}
public Date time()
{
return this.time;
}
public String getFileName ()
{
return this.fileName;
}
}
The algorithm is very simple:
string -> deserialization -> object events created.
But .. further problems began. These problems I can not decide ..
Added new events.
Information that comes with an external system does not contain all necessary data about the event, for example:
{UpdateProductInfoEvent: {userName:'name', time: '00.00.00', product: {id:'123', name: '???', type: '???', cost:'???'}}}
As you can see, the line does not contain all the data … just deserialized not give a desired result …
To do this, I still need to call a method that will receive information about a product by its Id.
The algorithm is as follows:
JSON string -> processing line -> product information from ID -> object creation * Event.
The following example:
{ModifyProductCatalogEvent:{userName: 'Name', time: '00.00.00', catalog:{id:'321', catalogType:'???', catalogName: '????'}}}
Again I not have all info about catalog…
So, I ask for help, how do I properly construct an algorithm to create objects in case of lack of data?
You can write your own serialization and deserialization methods by overwriting:
which enables you to handle those cases yourself. You can still use the default methods by using out.defaultWriteObject/in.defaultReadObject to only have to handle the cases where data may be missing (or if you have default values for invalid objects, read all fields with the normal methods and then overwrite the invalid fields with the correct data).