@Override public <T> void encode(T object, OutputStream outputStream) throws CodecException { try { outputStream.write(encode(object)); } catch (IOException e) { throw new CodecException("I/O error occurred encoding object to output stream: " + e.getMessage(), e); } }
private Object decodeContext( ConversionService<?> conversionService, Argument<?> arg, Object context) { if (ClassUtils.isJavaLangType(arg.getType())) { Object convert = doConvertInput(conversionService, arg, context); if (convert != null) { return convert; } } throw new CodecException("Unable to decode argument from stream: " + arg); }
@Override public <I, B> Flowable<Event<B>> eventStream(io.micronaut.http.HttpRequest<I> request, Argument<B> eventType) { return eventStream(request).map(byteBufferEvent -> { ByteBuffer<?> data = byteBufferEvent.getData(); Optional<MediaTypeCodec> registeredCodec; if (mediaTypeCodecRegistry != null) { registeredCodec = mediaTypeCodecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE); } else { registeredCodec = Optional.empty(); } if (registeredCodec.isPresent()) { B decoded = registeredCodec.get().decode(eventType, data); return Event.of(byteBufferEvent, decoded); } else { throw new CodecException("JSON codec not present"); } }); }
private Object decodeInputArgument( ConversionService<?> conversionService, LocalFunctionRegistry localFunctionRegistry, Argument<?> arg, InputStream input) { Class<?> argType = arg.getType(); ClassLoadingReporter.reportBeanPresent(argType); if (ClassUtils.isJavaLangType(argType)) { Object converted = doConvertInput(conversionService, arg, input); if (converted != null) { return converted; } } else if (argType.isInstance(input)) { return input; } else { if (localFunctionRegistry instanceof MediaTypeCodecRegistry) { Optional<MediaTypeCodec> registeredDecoder = ((MediaTypeCodecRegistry) localFunctionRegistry).findCodec(MediaType.APPLICATION_JSON_TYPE); if (registeredDecoder.isPresent()) { MediaTypeCodec decoder = registeredDecoder.get(); return decoder.decode(arg, input); } } } throw new CodecException("Unable to decode argument from stream: " + arg); }
throw new CodecException("Cannot encode value [" + o + "]. No possible encoders found"); });
} else { MediaTypeCodec jsonCodec = resolveMediaTypeCodecRegistry().findCodec(MediaType.APPLICATION_JSON_TYPE) .orElseThrow(() -> new CodecException("No possible JSON encoders found!")); body = jsonCodec.encode(data, allocator);
@Override public <T> byte[] encode(T object) throws CodecException { try { if (object instanceof byte[]) { return (byte[]) object; } else { return objectMapper.writeValueAsBytes(object); } } catch (JsonProcessingException e) { throw new CodecException("Error encoding object [" + object + "] to JSON: " + e.getMessage()); } }
@Override public <T> void encode(T object, OutputStream outputStream) throws CodecException { try { objectMapper.writeValue(outputStream, object); } catch (IOException e) { throw new CodecException("Error encoding object [" + object + "] to JSON: " + e.getMessage()); } }
@Override public <T> void encode(T object, OutputStream outputStream) throws CodecException { try { outputStream.write(encode(object)); } catch (IOException e) { throw new CodecException("I/O error occurred encoding object to output stream: " + e.getMessage(), e); } }
@Override public <T> void encode(T object, OutputStream outputStream) throws CodecException { byte[] bytes = encode(object); try { outputStream.write(bytes); } catch (IOException e) { throw new CodecException("Error writing encoding bytes to stream: " + e.getMessage(), e); } }
@Override public <T> T decode(Argument<T> type, InputStream inputStream) throws CodecException { if (CharSequence.class.isAssignableFrom(type.getType())) { try { return (T) IOUtils.readText(new BufferedReader(new InputStreamReader(inputStream, defaultCharset))); } catch (IOException e) { throw new CodecException("Error decoding string from stream: " + e.getMessage()); } } throw new UnsupportedOperationException("codec only supports decoding objects to string"); }
@Override public <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException { String text = buffer.toString(defaultCharset); return ConversionService.SHARED .convert(text, type) .orElseThrow(() -> new CodecException("Cannot decode byte buffer with value [" + text + "] to type: " + type)); }
/** * Decodes the given JSON node. * * @param type The type * @param node The Json Node * @param <T> The generic type * @return The decoded object * @throws CodecException When object cannot be decoded */ public <T> T decode(Argument<T> type, JsonNode node) throws CodecException { try { return objectMapper.treeToValue(node, type.getType()); } catch (IOException e) { throw new CodecException("Error decoding JSON stream for type [" + type.getName() + "]: " + e.getMessage()); } }
@SuppressWarnings("Duplicates") @Override public <T> T decode(Argument<T> type, InputStream inputStream) throws CodecException { try { if (type.hasTypeVariables()) { JavaType javaType = constructJavaType(type); return objectMapper.readValue(inputStream, javaType); } else { return objectMapper.readValue(inputStream, type.getType()); } } catch (IOException e) { throw new CodecException("Error decoding JSON stream for type [" + type.getName() + "]: " + e.getMessage()); } }
@SuppressWarnings("Duplicates") @Override public <T> T decode(Argument<T> type, String data) throws CodecException { try { if (type.hasTypeVariables()) { JavaType javaType = constructJavaType(type); return objectMapper.readValue(data, javaType); } else { return objectMapper.readValue(data, type.getType()); } } catch (IOException e) { throw new CodecException("Error decoding JSON stream for type [" + type.getName() + "]: " + e.getMessage()); } }
@Override public <I, B> Flowable<Event<B>> eventStream(io.micronaut.http.HttpRequest<I> request, Argument<B> eventType) { return eventStream(request).map(byteBufferEvent -> { ByteBuffer<?> data = byteBufferEvent.getData(); Optional<MediaTypeCodec> registeredCodec; if (mediaTypeCodecRegistry != null) { registeredCodec = mediaTypeCodecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE); } else { registeredCodec = Optional.empty(); } if (registeredCodec.isPresent()) { B decoded = registeredCodec.get().decode(eventType, data); return Event.of(byteBufferEvent, decoded); } else { throw new CodecException("JSON codec not present"); } }); }
@Override public <I, B> Flowable<Event<B>> eventStream(io.micronaut.http.HttpRequest<I> request, Argument<B> eventType) { return eventStream(request).map(byteBufferEvent -> { ByteBuffer<?> data = byteBufferEvent.getData(); Optional<MediaTypeCodec> registeredCodec; if (mediaTypeCodecRegistry != null) { registeredCodec = mediaTypeCodecRegistry.findCodec(MediaType.APPLICATION_JSON_TYPE); } else { registeredCodec = Optional.empty(); } if (registeredCodec.isPresent()) { B decoded = registeredCodec.get().decode(eventType, data); return Event.of(byteBufferEvent, decoded); } else { throw new CodecException("JSON codec not present"); } }); }
@Override public <T> T decode(Argument<T> type, ByteBuffer<?> buffer) throws CodecException { try { if (CharSequence.class.isAssignableFrom(type.getType())) { return (T) buffer.toString(applicationConfiguration.getDefaultCharset()); } else if (type.hasTypeVariables()) { JavaType javaType = constructJavaType(type); return objectMapper.readValue(buffer.toByteArray(), javaType); } else { return objectMapper.readValue(buffer.toByteArray(), type.getType()); } } catch (IOException e) { throw new CodecException("Error decoding JSON stream for type [" + type.getType() + "]: " + e.getMessage()); } }
throw new CodecException("Cannot encode value [" + o + "]. No possible encoders found"); });
} else { MediaTypeCodec jsonCodec = resolveMediaTypeCodecRegistry().findCodec(MediaType.APPLICATION_JSON_TYPE) .orElseThrow(() -> new CodecException("No possible JSON encoders found!")); body = jsonCodec.encode(data, allocator);