Search in sources :

Example 1 with Annot8ComponentDescriptor

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;
}
Also used : Annot8ComponentDescriptor(io.annot8.api.components.Annot8ComponentDescriptor) JsonbException(jakarta.json.bind.JsonbException) JsonParser(jakarta.json.stream.JsonParser)

Example 2 with Annot8ComponentDescriptor

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());
}
Also used : JsonbConfig(jakarta.json.bind.JsonbConfig) Jsonb(jakarta.json.bind.Jsonb) Annot8ComponentDescriptor(io.annot8.api.components.Annot8ComponentDescriptor) Annot8ComponentDescriptor(io.annot8.api.components.Annot8ComponentDescriptor) Descriptor(io.annot8.common.serialization.TestNested.Descriptor) Test(org.junit.jupiter.api.Test)

Example 3 with Annot8ComponentDescriptor

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());
}
Also used : JsonbConfig(jakarta.json.bind.JsonbConfig) Jsonb(jakarta.json.bind.Jsonb) Annot8ComponentDescriptor(io.annot8.api.components.Annot8ComponentDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

Annot8ComponentDescriptor (io.annot8.api.components.Annot8ComponentDescriptor)3 Jsonb (jakarta.json.bind.Jsonb)2 JsonbConfig (jakarta.json.bind.JsonbConfig)2 Test (org.junit.jupiter.api.Test)2 Descriptor (io.annot8.common.serialization.TestNested.Descriptor)1 JsonbException (jakarta.json.bind.JsonbException)1 JsonParser (jakarta.json.stream.JsonParser)1