package io.quarkus.rest.runtime.providers.serialisers.jsonb;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;

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

@Provider
@Consumes({ "application/json", "application/*+json", "text/json" })
public class JsonbMessageBodyReader implements MessageBodyReader<Object> {

    private final Jsonb json;

    public JsonbMessageBodyReader() {
        InstanceHandle<Jsonb> jsonbInstanceHandle = Arc.container().instance(Jsonb.class);
        if (jsonbInstanceHandle.isAvailable()) {
            this.json = jsonbInstanceHandle.get();
        } else {
            this.json = JsonbBuilder.create();
        }
    }

    @Override
    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return true;
    }

    @Override
    public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
        Type runtimeType = genericType != null ? genericType : type;
        return json.fromJson(entityStream, runtimeType);
    }
}

@@ -501,16 +501,52 @@
 rc.Arc;%0A
+import io.quarkus.arc.ArcContainer;%0A
 import i
@@ -575,16 +575,16 @@
 Handle;%0A
-
 %0A@Provid
@@ -796,24 +796,117 @@
 yReader() %7B%0A
+        ArcContainer arcContainer = Arc.container();%0A        if (arcContainer != null) %7B%0A    
         Inst
@@ -988,24 +988,28 @@
 s);%0A        
+ 
+   
 if (jsonbIns
@@ -1041,32 +1041,36 @@
 ) %7B%0A            
+    
 this.json = json
@@ -1088,24 +1088,110 @@
 ndle.get();%0A
+            %7D else %7B%0A                this.json = JsonbBuilder.create();%0A            %7D%0A
         %7D el

support running without ArC running