Skip to content

AbstractJackson2Decoder#decodeToMono support non-blocking parsing #34586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
import org.jspecify.annotations.Nullable;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.context.ContextView;

import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Hints;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.MediaType;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
Expand Down Expand Up @@ -58,4 +62,15 @@ public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementTy
throw new UnsupportedOperationException("Does not support stream decoding yet");
}

@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
return Mono.deferContextual(contextView -> {

Map<String, Object> hintsToUse = contextView.isEmpty() ? hints :
Hints.merge(hints, ContextView.class.getName(), contextView);

return DataBufferUtils.join(input, this.getMaxInMemorySize()).flatMap(dataBuffer ->
Mono.justOrEmpty(decode(dataBuffer, elementType, mimeType, hintsToUse)));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public boolean canDecode(ResolvableType elementType, @Nullable MimeType mimeType
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

return decodeToFlux(input, elementType, mimeType, hints, null);
}

private Flux<Object> decodeToFlux(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints,@Nullable Boolean tokenizeArrays) {
ObjectMapper mapper = selectObjectMapper(elementType, mimeType);
if (mapper == null) {
return Flux.error(new IllegalStateException("No ObjectMapper for " + elementType));
Expand All @@ -138,8 +142,10 @@ public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementTy
forceUseOfBigDecimal = true;
}

boolean tokenizeArrays = (!elementType.isArray() &&
!Collection.class.isAssignableFrom(elementType.resolve(Object.class)));
if (tokenizeArrays == null) {
tokenizeArrays = (!elementType.isArray() &&
!Collection.class.isAssignableFrom(elementType.resolve(Object.class)));
}

Flux<DataBuffer> processed = processInput(input, elementType, mimeType, hints);
Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(processed, mapper.getFactory(), mapper,
Expand Down Expand Up @@ -188,15 +194,7 @@ protected Flux<DataBuffer> processInput(Publisher<DataBuffer> input, ResolvableT
@Override
public Mono<Object> decodeToMono(Publisher<DataBuffer> input, ResolvableType elementType,
@Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {

return Mono.deferContextual(contextView -> {

Map<String, Object> hintsToUse = contextView.isEmpty() ? hints :
Hints.merge(hints, ContextView.class.getName(), contextView);

return DataBufferUtils.join(input, this.maxInMemorySize).flatMap(dataBuffer ->
Mono.justOrEmpty(decode(dataBuffer, elementType, mimeType, hintsToUse)));
});
return decodeToFlux(input, elementType, mimeType, hints, false).singleOrEmpty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ private boolean isTopLevelArrayToken(JsonToken token) {

private void assertInMemorySize(int currentBufferSize, List<TokenBuffer> result) {
if (this.maxInMemorySize >= 0) {
if (currentBufferSize > this.maxInMemorySize) {
raiseLimitException();
}
if (!result.isEmpty()) {
this.byteCount = 0;
}
Expand Down
Loading