Honour ObjectMapper feature in Jackson2Tokenizer

After this commit, Jackson2Tokenizer honours ObjectMapper's
DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS feature when creating
TokenBuffers.

Closes gh-24479
This commit is contained in:
Arjen Poutsma
2020-02-06 10:01:13 +01:00
parent a59a33846e
commit 0d7494ac52
2 changed files with 65 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,10 +19,14 @@ package org.springframework.http.codec.json;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.TokenBuffer;
import org.json.JSONException;
@@ -37,8 +41,10 @@ import org.springframework.core.io.buffer.AbstractLeakCheckingTestCase;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferLimitException;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
/**
* @author Arjen Poutsma
@@ -259,6 +265,36 @@ public class Jackson2TokenizerTests extends AbstractLeakCheckingTestCase {
.verify();
}
@Test
public void useBigDecimalForFloats() {
for (boolean useBigDecimalForFloats : Arrays.asList(false, true)) {
this.objectMapper.configure(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS, useBigDecimalForFloats);
Flux<DataBuffer> source = Flux.just(stringBuffer("1E+2"));
Flux<TokenBuffer> tokens =
Jackson2Tokenizer.tokenize(source, this.jsonFactory, this.objectMapper, false, -1);
StepVerifier.create(tokens)
.assertNext(tokenBuffer -> {
try {
JsonParser parser = tokenBuffer.asParser();
JsonToken token = parser.nextToken();
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, token);
JsonParser.NumberType numberType = parser.getNumberType();
if (useBigDecimalForFloats) {
assertEquals(JsonParser.NumberType.BIG_DECIMAL, numberType);
}
else {
assertEquals(JsonParser.NumberType.DOUBLE, numberType);
}
}
catch (IOException ex) {
fail(ex.getMessage());
}
})
.verifyComplete();
}
}
private Flux<String> decode(List<String> source, boolean tokenize, int maxInMemorySize) {