Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions extensions/jsonb/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jsonp</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.eclipse.yasson.spi.JsonbComponentInstanceCreator;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;

public class QuarkusJsonbComponentInstanceCreator implements JsonbComponentInstanceCreator {
Expand All @@ -34,17 +35,26 @@ public void close() throws IOException {
@Override
public <T> T getOrCreateComponent(Class<T> componentClass) {
return (T) components.computeIfAbsent(componentClass, c -> {
InstanceHandle<T> beanHandle = Arc.container().instance(componentClass);
ArcContainer container = Arc.container();
if (container == null) { // this class can be used in unit tests where Arc obviously doesn't run
return fallbackCreate(componentClass);
}

InstanceHandle<T> beanHandle = container.instance(componentClass);
if (beanHandle.isAvailable()) {
beanHandles.add(beanHandle);
return beanHandle.get();
}
try {
return componentClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException("Cannot instantiate Jsonb component: " + componentClass, e);
}
return fallbackCreate(componentClass);
});
}

private <T> Object fallbackCreate(Class<T> componentClass) {
try {
return componentClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException("Cannot instantiate JSON-B component: " + componentClass, e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.quarkus.jsonb;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Instant;

import javax.json.bind.JsonbBuilder;
import javax.json.bind.adapter.JsonbAdapter;
import javax.json.bind.annotation.JsonbProperty;
import javax.json.bind.annotation.JsonbTypeAdapter;

import org.junit.jupiter.api.Test;

class JsonbUnitTest {

@Test
void testJsonbWorksProperly() {
SomeClass instance = JsonbBuilder.create().fromJson("{\"time\":1570039000}", SomeClass.class);
assertEquals(Instant.ofEpochSecond(1570039000), instance.getTime());
}

public static class SomeClass {

@JsonbProperty("time")
@JsonbTypeAdapter(EpochSecondsAdapter.class)
private Instant time;

public SomeClass() {
}

public Instant getTime() {
return time;
}

public void setTime(Instant time) {
this.time = time;
}

public static class EpochSecondsAdapter implements JsonbAdapter<Instant, Long> {
@Override
public Long adaptToJson(Instant obj) {
return obj.getEpochSecond();
}

@Override
public Instant adaptFromJson(Long obj) {
return Instant.ofEpochSecond(obj);
}
}
}
}