/** * Construct a new {@code JsonbHttpMessageConverter} with the given configuration. * @param config the {@code JsonbConfig} for the underlying delegate */ public JsonbHttpMessageConverter(JsonbConfig config) { this.jsonb = JsonbBuilder.create(config); }
@Override protected Object readInternal(Type resolvedType, Reader reader) throws Exception { return getJsonb().fromJson(reader, resolvedType); }
@Override public void configure(final Env env, final Config config, final Binder binder) { JsonbConfig jsonbConfig = new JsonbConfig(); if (configurer != null) { configurer.accept(jsonbConfig, config); } Jsonb jsonb = JsonbBuilder.create(jsonbConfig); binder.bind(Jsonb.class).toInstance(jsonb); Multibinder.newSetBinder(binder, Parser.class).addBinding() .toInstance(new YassonParser(type, jsonb)); Multibinder.newSetBinder(binder, Renderer.class).addBinding() .toInstance(new YassonRenderer(type, jsonb)); }
public UiSpecService(final Client client) { this.client = client; this.jsonb = JsonbBuilder.create(new JsonbConfig().setProperty("johnzon.cdi.activated", false)); this.closeJsonb = true; }
protected Jsonb getJsonb(Class<?> type) { ContextResolver<Jsonb> contextResolver = providers.getContextResolver(Jsonb.class, MediaType.APPLICATION_JSON_TYPE); if (contextResolver != null) { return contextResolver.getContext(type); } else { if (jsonbObj == null) { JsonProviderImpl jProviderImpl = new JsonProviderImpl(); JsonBindingBuilder jbBuilder = new JsonBindingBuilder(); jsonbObj = jbBuilder.withProvider(jProviderImpl).build(); } return jsonbObj; } }
@Override protected Object readInternal(Type resolvedType, Reader reader) throws Exception { return getJsonb().fromJson(reader, resolvedType); }
/** * Construct a new {@code JsonbHttpMessageConverter} with default configuration. */ public JsonbHttpMessageConverter() { this(JsonbBuilder.create()); }
@Override public void render(final Object object, final Context ctx) throws Exception { if (ctx.accepts(this.type)) { ctx.type(this.type) .send(jsonb.toJson(object)); } }
@Override public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { Jsonb jsonb = getJsonb(type); try { return jsonb.fromJson(entityStream, genericType); } catch (JsonbException e) { throw new ProcessingException(LocalizationMessages.ERROR_JSONB_DESERIALIZATION(), e); } }
/** * Construct a new {@code JsonbHttpMessageConverter} with the given configuration. * @param config the {@code JsonbConfig} for the underlying delegate */ public JsonbHttpMessageConverter(JsonbConfig config) { this.jsonb = JsonbBuilder.create(config); }
@Override public void writeTo(Object o, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { Jsonb jsonb = getJsonb(type); try { entityStream.write(jsonb.toJson(o).getBytes(AbstractMessageReaderWriterProvider.getCharset(mediaType))); entityStream.flush(); } catch (IOException e) { throw new ProcessingException(LocalizationMessages.ERROR_JSONB_SERIALIZATION(), e); } }
@Override public Object parse(final TypeLiteral<?> type, final Context ctx) throws Throwable { MediaType ctype = ctx.type(); if (ctype.isAny()) { // */* return ctx.next(); } if (ctype.matches(this.type)) { return ctx .ifbody(body -> jsonb.fromJson(body.text(), type.getType())) .ifparam(values -> jsonb.fromJson(values.first(), type.getType())); } return ctx.next(); }
/** * Construct a new {@code JsonbHttpMessageConverter} with default configuration. */ public JsonbHttpMessageConverter() { this(JsonbBuilder.create()); }
@Override public String[] getMessages() { try { return new String[]{mapper.toJson(messageList)}; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws java.io.IOException, javax.ws.rs.WebApplicationException { Jsonb jsonb = getJsonb(type); final EmptyCheckInputStream is = new EmptyCheckInputStream(entityStream); try { return jsonb.fromJson(is, genericType); // If null is returned, considered to be empty stream } catch (Throwable e) { if (is.isEmpty()) { return null; } // detail text provided in logger message throw new ProcessingException(Messages.MESSAGES.jsonBDeserializationError(e, e.getMessage()), e); } }
JsonbSingleton() { this.jsonbInstance = JsonbBuilder.create(); } }
@Override public void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws java.io.IOException, javax.ws.rs.WebApplicationException { Jsonb jsonb = getJsonb(type); try { entityStream = new DelegatingOutputStream(entityStream) { @Override public void flush() throws IOException { // don't flush as this is a performance hit on Undertow. // and causes chunked encoding to happen. } }; entityStream.write(jsonb.toJson(t).getBytes(getCharset(mediaType))); entityStream.flush(); } catch (Throwable e) { throw new ProcessingException(Messages.MESSAGES.jsonBSerializationError(e.toString()), e); } } }