use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jaxb2XmlEncoder method encode.
@Override
protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory, ResolvableType type, MimeType mimeType, Map<String, Object> hints) {
try {
DataBuffer buffer = dataBufferFactory.allocateBuffer(1024);
OutputStream outputStream = buffer.asOutputStream();
Class<?> clazz = ClassUtils.getUserClass(value);
Marshaller marshaller = jaxbContexts.createMarshaller(clazz);
marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
marshaller.marshal(value, outputStream);
return Flux.just(buffer);
} catch (JAXBException ex) {
return Flux.error(ex);
}
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonDecoderTests method decodeToList.
@Test
public void decodeToList() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("[{\"bar\":\"b1\",\"foo\":\"f1\"},{\"bar\":\"b2\",\"foo\":\"f2\"}]"));
ResolvableType elementType = ResolvableType.forClassWithGenerics(List.class, Pojo.class);
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType, null, Collections.emptyMap());
StepVerifier.create(mono).expectNext(Arrays.asList(new Pojo("f1", "b1"), new Pojo("f2", "b2"))).expectComplete().verify();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonDecoderTests method decodePojo.
@Test
public void decodePojo() throws Exception {
Flux<DataBuffer> source = Flux.just(stringBuffer("{\"foo\": \"foofoo\", \"bar\": \"barbar\"}"));
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Flux<Object> flux = new Jackson2JsonDecoder().decode(source, elementType, null, Collections.emptyMap());
StepVerifier.create(flux).expectNext(new Pojo("foofoo", "barbar")).expectComplete().verify();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonDecoderTests method decodeEmptyBodyToMono.
@Test
public void decodeEmptyBodyToMono() throws Exception {
Flux<DataBuffer> source = Flux.empty();
ResolvableType elementType = ResolvableType.forClass(Pojo.class);
Mono<Object> mono = new Jackson2JsonDecoder().decodeToMono(source, elementType, null, Collections.emptyMap());
StepVerifier.create(mono).expectNextCount(0).expectComplete().verify();
}
use of org.springframework.core.io.buffer.DataBuffer in project spring-framework by spring-projects.
the class Jackson2JsonEncoderTests method encodeWithType.
@Test
public void encodeWithType() throws Exception {
Flux<ParentClass> source = Flux.just(new Foo(), new Bar());
ResolvableType type = ResolvableType.forClass(ParentClass.class);
Flux<DataBuffer> output = this.encoder.encode(source, this.bufferFactory, type, null, Collections.emptyMap());
StepVerifier.create(output).consumeNextWith(stringConsumer("[{\"type\":\"foo\"},{\"type\":\"bar\"}]")).expectComplete().verify();
}
Aggregations