Skip to content

Commit 994dfba

Browse files
authored
Fixes #5110: adds missing ObjectMapper.createNonBlockingByteBufferParser() method (#5113)
1 parent facee9c commit 994dfba

File tree

6 files changed

+92
-17
lines changed

6 files changed

+92
-17
lines changed

release-notes/CREDITS

+4
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ Andy Boothe (@sigpwned)
4040
* Contributed #5025: Add support for automatic detection of subtypes (like `@JsonSubTypes`)
4141
from Java 17 sealed types
4242
[3.0.0]
43+
44+
Sébastien Deleuze (@sdeleuze)
45+
* Requested #5110: Add missing `ObjectMapper#createNonBlockingByteBufferParser()` method
46+
[3.0.0]

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Versions: 3.x (for earlier see VERSION-2.x)
1111
to use `Supplier`
1212
#5094: Change the way `BeanDescription` passed during deserializer construction
1313
to use `Supplier`
14+
#5110: Add missing `ObjectMapper#createNonBlockingByteBufferParser()` method
15+
(requested by Sébastien D)
1416

1517
3.0.0-rc3 (13-Apr-2025)
1618

src/main/java/tools/jackson/databind/ObjectMapper.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ public JsonParser createParser(DataInput content) throws JacksonException {
737737
* Factory method for constructing non-blocking {@link JsonParser} that is properly
738738
* wired to allow configuration access (and, if relevant for parser, callbacks):
739739
* essentially constructs a {@link ObjectReadContext} and then calls
740-
* {@link TokenStreamFactory#createParser(ObjectReadContext,DataInput)}.
740+
* {@link TokenStreamFactory#createNonBlockingByteArrayParser(ObjectReadContext)}.
741741
*
742742
* @since 3.0
743743
*/
@@ -746,6 +746,19 @@ public JsonParser createNonBlockingByteArrayParser() throws JacksonException {
746746
return ctxt.assignAndReturnParser(_streamFactory.createNonBlockingByteArrayParser(ctxt));
747747
}
748748

749+
/**
750+
* Factory method for constructing non-blocking {@link JsonParser} that is properly
751+
* wired to allow configuration access (and, if relevant for parser, callbacks):
752+
* essentially constructs a {@link ObjectReadContext} and then calls
753+
* {@link TokenStreamFactory#createNonBlockingByteBufferParser(ObjectReadContext)}.
754+
*
755+
* @since 3.0
756+
*/
757+
public JsonParser createNonBlockingByteBufferParser() throws JacksonException {
758+
DeserializationContextExt ctxt = _deserializationContext();
759+
return ctxt.assignAndReturnParser(_streamFactory.createNonBlockingByteBufferParser(ctxt));
760+
}
761+
749762
/*
750763
/**********************************************************************
751764
/* Public API: constructing Generator that are properly linked

src/main/java/tools/jackson/databind/ObjectReader.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ public JsonParser createParser(DataInput content) throws JacksonException {
961961
* Factory method for constructing non-blocking {@link JsonParser} that is properly
962962
* wired to allow configuration access (and, if relevant for parser, callbacks):
963963
* essentially constructs a {@link ObjectReadContext} and then calls
964-
* {@link TokenStreamFactory#createParser(ObjectReadContext,DataInput)}.
964+
* {@link TokenStreamFactory#createNonBlockingByteArrayParser(ObjectReadContext)}.
965965
*
966966
* @since 3.0
967967
*/
@@ -970,6 +970,19 @@ public JsonParser createNonBlockingByteArrayParser() throws JacksonException {
970970
return ctxt.assignAndReturnParser(_parserFactory.createNonBlockingByteArrayParser(ctxt));
971971
}
972972

973+
/**
974+
* Factory method for constructing non-blocking {@link JsonParser} that is properly
975+
* wired to allow configuration access (and, if relevant for parser, callbacks):
976+
* essentially constructs a {@link ObjectReadContext} and then calls
977+
* {@link TokenStreamFactory#createNonBlockingByteBufferParser(ObjectReadContext)}.
978+
*
979+
* @since 3.0
980+
*/
981+
public JsonParser createNonBlockingByteBufferParser() throws JacksonException {
982+
DeserializationContextExt ctxt = _deserializationContext();
983+
return ctxt.assignAndReturnParser(_parserFactory.createNonBlockingByteBufferParser(ctxt));
984+
}
985+
973986
/*
974987
/**********************************************************************
975988
/* TreeCodec implementation

src/test/java/tools/jackson/databind/deser/MergePolymorphicTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
public class MergePolymorphicTest
1717
{
18-
1918
static class Root {
2019
@JsonMerge
2120
public Child child;

src/test/java/tools/jackson/databind/deser/NonBlockingDeserTest.java

+58-14
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,19 @@
66

77
import tools.jackson.core.JsonParser;
88
import tools.jackson.core.ObjectReadContext;
9+
import tools.jackson.core.async.ByteArrayFeeder;
910
import tools.jackson.core.async.ByteBufferFeeder;
1011

1112
import tools.jackson.databind.ObjectMapper;
1213
import tools.jackson.databind.json.JsonMapper;
14+
import tools.jackson.databind.testutil.DatabindTestUtil;
1315

1416
import static org.junit.jupiter.api.Assertions.assertEquals;
1517

1618
/**
1719
* Tests for checking that test deserialization with non-blocking parsers
1820
*/
19-
public class NonBlockingDeserTest
21+
public class NonBlockingDeserTest extends DatabindTestUtil
2022
{
2123
record Foo(String bar) {}
2224

@@ -26,24 +28,66 @@ record Foo(String bar) {}
2628
/**********************************************************
2729
*/
2830

31+
private final static int TEST_ITEM_COUNT = 10;
32+
33+
private final ObjectMapper MAPPER = newJsonMapper();
34+
35+
private final byte[] TEST_DOC = _testDoc(TEST_ITEM_COUNT);
36+
2937
@Test
30-
public void testNonBlockingParser()
38+
public void testNonBlockingByteArrayParserViaMapper()
3139
{
32-
final ObjectMapper m = JsonMapper.builder()
33-
//.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
34-
.build();
35-
final int len = 10;
36-
Foo[] foos = new Foo[len];
37-
for (int i = 0; i < len; ++i) {
38-
foos[i] = new Foo("bar-" + i);
40+
try (final JsonParser parser =
41+
MAPPER.createNonBlockingByteArrayParser()) {
42+
((ByteArrayFeeder) parser).feedInput(TEST_DOC, 0, TEST_DOC.length);
43+
((ByteArrayFeeder) parser).endOfInput();
44+
Foo[] result = MAPPER.readValue(parser, Foo[].class);
45+
assertEquals(TEST_ITEM_COUNT, result.length);
3946
}
40-
final Foo[] result;
47+
}
48+
49+
@Test
50+
public void testNonBlockingByteArrayParserViaReader()
51+
{
4152
try (final JsonParser parser =
42-
m.tokenStreamFactory().createNonBlockingByteBufferParser(ObjectReadContext.empty())) {
43-
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(m.writeValueAsBytes(foos)));
53+
MAPPER.reader().createNonBlockingByteArrayParser()) {
54+
((ByteArrayFeeder) parser).feedInput(TEST_DOC, 0, TEST_DOC.length);
55+
((ByteArrayFeeder) parser).endOfInput();
56+
Foo[] result = MAPPER.readValue(parser, Foo[].class);
57+
assertEquals(TEST_ITEM_COUNT, result.length);
58+
}
59+
}
60+
61+
@Test
62+
public void testNonBlockingByteBufferParserViaMapper()
63+
{
64+
try (final JsonParser parser =
65+
MAPPER.createNonBlockingByteBufferParser()) {
66+
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(TEST_DOC));
67+
((ByteBufferFeeder) parser).endOfInput();
68+
Foo[] result = MAPPER.readValue(parser, Foo[].class);
69+
assertEquals(TEST_ITEM_COUNT, result.length);
70+
}
71+
}
72+
73+
@Test
74+
public void testNonBlockingByteBufferParserViaReader()
75+
{
76+
try (final JsonParser parser =
77+
MAPPER.reader().createNonBlockingByteBufferParser()) {
78+
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(TEST_DOC));
4479
((ByteBufferFeeder) parser).endOfInput();
45-
result = m.readValue(parser, Foo[].class);
80+
Foo[] result = MAPPER.readValue(parser, Foo[].class);
81+
assertEquals(TEST_ITEM_COUNT, result.length);
82+
}
83+
}
84+
85+
private byte[] _testDoc(int count) {
86+
Foo[] foos = new Foo[count];
87+
for (int i = 0; i < count; ++i) {
88+
foos[i] = new Foo("bar-" + i);
4689
}
47-
assertEquals(len, result.length);
90+
return MAPPER.writeValueAsBytes(foos);
4891
}
92+
4993
}

0 commit comments

Comments
 (0)