use of io.annot8.api.components.Annot8ComponentDescriptor in project annot8 by annot8.
the class Annot8ComponentDescriptorDeserializer method deserialize.
@Override
public Annot8ComponentDescriptor deserialize(JsonParser parser, DeserializationContext ctx, Type type) {
Annot8ComponentDescriptor desc = null;
while (parser.hasNext()) {
JsonParser.Event event = parser.next();
if (event == JsonParser.Event.KEY_NAME) {
String className = parser.getString();
parser.next();
try {
/* TODO: This is not a good way of doing it, as we lose any user provided config (including additional deserializers)
* However, if we don't do this we get a recursive error because we try to deserialize
* with ctx which itself tries to use this deserializer
* To change this, we need Yasson or JSON-B to update how they implement things
* There are a number of open GitHub tickets about this
* https://github.com/eclipse-ee4j/yasson/issues/133
* https://github.com/eclipse-ee4j/yasson/issues/279
* https://github.com/eclipse-ee4j/jsonb-api/issues/147
*/
desc = jb.fromJson(parser.getObject().toString(), Class.forName(className).asSubclass(Annot8ComponentDescriptor.class));
/* This is the correct way to do the above according to the JSON-B documentation,
* but fails with the reference implementation (see above)
*
* desc =
* ctx.deserialize(Class.forName(className).asSubclass(Annot8ComponentDescriptor.class),
* parser);
*/
} catch (ClassNotFoundException e) {
throw new JsonbException("Deserialization failed - could not find class " + className, e);
}
}
}
return desc;
}
use of io.annot8.api.components.Annot8ComponentDescriptor in project annot8 by annot8.
the class Annot8ComponentDescriptorDeserializerTest method testNested.
@Test
public void testNested() {
JsonbConfig config = new JsonbConfig().withDeserializers(new Annot8ComponentDescriptorDeserializer());
Jsonb jb = JsonbBuilder.create(config);
Annot8ComponentDescriptor desc = jb.fromJson("{\"" + Descriptor.class.getName() + "\":{\"name\":\"Test\",\"settings\":{}}}", Annot8ComponentDescriptor.class);
assertEquals(Descriptor.class, desc.getClass());
assertEquals("Test", desc.getName());
assertEquals(Configuration.class, desc.getSettings().getClass());
}
use of io.annot8.api.components.Annot8ComponentDescriptor in project annot8 by annot8.
the class Annot8ComponentDescriptorDeserializerTest method test.
@Test
public void test() {
JsonbConfig config = new JsonbConfig().withDeserializers(new Annot8ComponentDescriptorDeserializer());
Jsonb jb = JsonbBuilder.create(config);
Annot8ComponentDescriptor desc = jb.fromJson("{\"" + TestDescriptor.class.getName() + "\":{\"name\":\"Test\",\"settings\":{\"host\":\"localhost\",\"port\":8080}}}", Annot8ComponentDescriptor.class);
assertEquals(TestDescriptor.class, desc.getClass());
assertEquals("Test", desc.getName());
assertEquals(TestSettings.class, desc.getSettings().getClass());
TestSettings ts = (TestSettings) desc.getSettings();
assertEquals("localhost", ts.getHost());
assertEquals(8080, ts.getPort());
assertEquals(TestProcessor.class, desc.create(null).getClass());
}
Aggregations