I have huge json document and appropriate jaskson models that are mapped to that json. In some cases it is not possible to build the required json document with all object because some data does not exist.
for instance I have following models:
class First {
private firstField;
private secondField;
etc...
}
class Second {
private firstField;
private secondField;
etc...
}
class General {
private First first;
private Second second;
etc...
}
And there is possibility to populate only First instance:
In usual cases it will be serialized something like this:
{
"first":{
"firstField":"some_value",
"secondField":"some_value"
},
"second":null
}
But my goal is to serialize General class something like this:
{
"first":{
"firstField":"some_value",
"secondField":"some_value"
},
"second":{
"firstField":"null",
"secondField":"null"
}
}
It is possible to achieve this with following changes in General class in order to initialize their members using default constructors by default:
class General {
private First first = new First();
private Second second = new Second()
etc...
}
But this approach will cause too many changes around existing models and I am not sure that it is the best one approach. Is it possible to create some custom serializer that will do this by itself?
Edited according to https://stackoverflow.com/users/1898563/michael suggestion:
So, the main idea is to create serializer that would be able to check whether instance is null and if it is null it should be able to create new instance using default constructor, note: this serializer should not be based on specific Second
class, it should work with any object that is going to be serialized except simple types.