Код: Выделить всё
abstract class BaseClass{
// see explanation for annotations below
@JsonIgnore private final Thing storedThing;
@JsonProperty private final String thingId;
protected BaseClass(){} // empty constructor
protected BaseClass(Thing thing){
// store thing
}
// other stuff
}
< /code>
Thing
Код: Выделить всё
// SomeClass extends BaseClass
"SomeClass":{
"thingId" : "ahbjhadbv" // target id of stored Thing. All other info is ignored
}
< /code>
The intent being that during deserialization I can look up the actual Thing
Код: Выделить всё
Thing
Код: Выделить всё
// SomeOtherClassextends BaseClass
"SomeOtherClass":{
"thingId" : "asdawetgvd" // target id of stored Thing. All other info is ignored
"someOtherData": {...}
"evenMoreData" : {...}
}
< /code>
Implementing serialization to suit my needs was simple enough with Jackson.
Now the annoying/interesting bit - for reasons I can't go into, I am hamstrung with these constraints:
[*]I cannot use mixins/@JsonTypeInfo
[*] Я не могу предоставить многоуровневый конструктор для обоих вещей и thingid на baseclass .
[*] Даже если я наталкиваю на конструкции, я не имею гарантирования, что реализации Baseclass x.getParameterCount() == 0)
T instance;
if(hasEmptyConstructor){
instance = clazz.getConstructor().newInstance();
} else {
Thing dummyThing = getDummyThing(); // empty thing object needed only for deserialzation
instance = clazz.getConstructor(Thing.class).newInstance(dummyThing);
}
// populate instance with other state data from serializedData
}
< /code>
And now the question - given that the class T[/code] может иметь поля данных состояния, отличные от Thingid , как я могу надежно заполнить поля создания объекта с указанными данными с использованием Джексона? Получите имена поля. Мне нужно как -то использовать Джексона, но я не смог выяснить, как. Большинство доступных методов, по -видимому, требуют типа вместо создания создания объекта, и я не могу найти никакого способа внедрить указанный объект в десериализатор.
Подробнее здесь: https://stackoverflow.com/questions/797 ... ce-without