|
| 1 | +package tools.jackson.databind.deser; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import tools.jackson.core.JsonParser; |
| 5 | +import tools.jackson.core.ObjectReadContext; |
| 6 | +import tools.jackson.core.async.ByteBufferFeeder; |
| 7 | +import tools.jackson.databind.DeserializationFeature; |
| 8 | +import tools.jackson.databind.ObjectMapper; |
| 9 | +import tools.jackson.databind.json.JsonMapper; |
| 10 | + |
| 11 | +import java.nio.ByteBuffer; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +/** |
| 16 | + * Tests for checking that test deserialization with non-blocking parsers |
| 17 | + */ |
| 18 | +public class NonBlockingDeserTest |
| 19 | +{ |
| 20 | + record Foo(String bar) {} |
| 21 | + |
| 22 | + /* |
| 23 | + /********************************************************** |
| 24 | + /* Test methods |
| 25 | + /********************************************************** |
| 26 | + */ |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testNonBlockingParser() |
| 30 | + { |
| 31 | + final ObjectMapper m = JsonMapper.builder() |
| 32 | + //.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS) |
| 33 | + .build(); |
| 34 | + final JsonParser parser = |
| 35 | + m.tokenStreamFactory().createNonBlockingByteBufferParser(ObjectReadContext.empty()); |
| 36 | + final int len = 10; |
| 37 | + Foo[] foos = new Foo[len]; |
| 38 | + for (int i = 0; i < len; ++i) { |
| 39 | + foos[i] = new Foo("bar-" + i); |
| 40 | + } |
| 41 | + try { |
| 42 | + ((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(m.writeValueAsBytes(foos))); |
| 43 | + ((ByteBufferFeeder) parser).endOfInput(); |
| 44 | + final Foo[] result = m.readValue(parser, Foo[].class); |
| 45 | + assertEquals(len, result.length); |
| 46 | + } finally { |
| 47 | + parser.close(); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments