Skip to content

Fixes #5110: adds missing ObjectMapper.createNonBlockingByteBufferParser() method #5113

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

Merged
merged 2 commits into from
Apr 25, 2025
Merged
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
4 changes: 4 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ Andy Boothe (@sigpwned)
* Contributed #5025: Add support for automatic detection of subtypes (like `@JsonSubTypes`)
from Java 17 sealed types
[3.0.0]

Sébastien Deleuze (@sdeleuze)
* Requested #5110: Add missing `ObjectMapper#createNonBlockingByteBufferParser()` method
[3.0.0]
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Versions: 3.x (for earlier see VERSION-2.x)
to use `Supplier`
#5094: Change the way `BeanDescription` passed during deserializer construction
to use `Supplier`
#5110: Add missing `ObjectMapper#createNonBlockingByteBufferParser()` method
(requested by Sébastien D)

3.0.0-rc3 (13-Apr-2025)

Expand Down
15 changes: 14 additions & 1 deletion src/main/java/tools/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ public JsonParser createParser(DataInput content) throws JacksonException {
* Factory method for constructing non-blocking {@link JsonParser} that is properly
* wired to allow configuration access (and, if relevant for parser, callbacks):
* essentially constructs a {@link ObjectReadContext} and then calls
* {@link TokenStreamFactory#createParser(ObjectReadContext,DataInput)}.
* {@link TokenStreamFactory#createNonBlockingByteArrayParser(ObjectReadContext)}.
*
* @since 3.0
*/
Expand All @@ -746,6 +746,19 @@ public JsonParser createNonBlockingByteArrayParser() throws JacksonException {
return ctxt.assignAndReturnParser(_streamFactory.createNonBlockingByteArrayParser(ctxt));
}

/**
* Factory method for constructing non-blocking {@link JsonParser} that is properly
* wired to allow configuration access (and, if relevant for parser, callbacks):
* essentially constructs a {@link ObjectReadContext} and then calls
* {@link TokenStreamFactory#createNonBlockingByteBufferParser(ObjectReadContext)}.
*
* @since 3.0
*/
public JsonParser createNonBlockingByteBufferParser() throws JacksonException {
DeserializationContextExt ctxt = _deserializationContext();
return ctxt.assignAndReturnParser(_streamFactory.createNonBlockingByteBufferParser(ctxt));
}

/*
/**********************************************************************
/* Public API: constructing Generator that are properly linked
Expand Down
15 changes: 14 additions & 1 deletion src/main/java/tools/jackson/databind/ObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ public JsonParser createParser(DataInput content) throws JacksonException {
* Factory method for constructing non-blocking {@link JsonParser} that is properly
* wired to allow configuration access (and, if relevant for parser, callbacks):
* essentially constructs a {@link ObjectReadContext} and then calls
* {@link TokenStreamFactory#createParser(ObjectReadContext,DataInput)}.
* {@link TokenStreamFactory#createNonBlockingByteArrayParser(ObjectReadContext)}.
*
* @since 3.0
*/
Expand All @@ -970,6 +970,19 @@ public JsonParser createNonBlockingByteArrayParser() throws JacksonException {
return ctxt.assignAndReturnParser(_parserFactory.createNonBlockingByteArrayParser(ctxt));
}

/**
* Factory method for constructing non-blocking {@link JsonParser} that is properly
* wired to allow configuration access (and, if relevant for parser, callbacks):
* essentially constructs a {@link ObjectReadContext} and then calls
* {@link TokenStreamFactory#createNonBlockingByteBufferParser(ObjectReadContext)}.
*
* @since 3.0
*/
public JsonParser createNonBlockingByteBufferParser() throws JacksonException {
DeserializationContextExt ctxt = _deserializationContext();
return ctxt.assignAndReturnParser(_parserFactory.createNonBlockingByteBufferParser(ctxt));
}

/*
/**********************************************************************
/* TreeCodec implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

public class MergePolymorphicTest
{

static class Root {
@JsonMerge
public Child child;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@

import tools.jackson.core.JsonParser;
import tools.jackson.core.ObjectReadContext;
import tools.jackson.core.async.ByteArrayFeeder;
import tools.jackson.core.async.ByteBufferFeeder;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.databind.testutil.DatabindTestUtil;

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

/**
* Tests for checking that test deserialization with non-blocking parsers
*/
public class NonBlockingDeserTest
public class NonBlockingDeserTest extends DatabindTestUtil
{
record Foo(String bar) {}

Expand All @@ -26,24 +28,66 @@ record Foo(String bar) {}
/**********************************************************
*/

private final static int TEST_ITEM_COUNT = 10;

private final ObjectMapper MAPPER = newJsonMapper();

private final byte[] TEST_DOC = _testDoc(TEST_ITEM_COUNT);

@Test
public void testNonBlockingParser()
public void testNonBlockingByteArrayParserViaMapper()
{
final ObjectMapper m = JsonMapper.builder()
//.disable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
.build();
final int len = 10;
Foo[] foos = new Foo[len];
for (int i = 0; i < len; ++i) {
foos[i] = new Foo("bar-" + i);
try (final JsonParser parser =
MAPPER.createNonBlockingByteArrayParser()) {
((ByteArrayFeeder) parser).feedInput(TEST_DOC, 0, TEST_DOC.length);
((ByteArrayFeeder) parser).endOfInput();
Foo[] result = MAPPER.readValue(parser, Foo[].class);
assertEquals(TEST_ITEM_COUNT, result.length);
}
final Foo[] result;
}

@Test
public void testNonBlockingByteArrayParserViaReader()
{
try (final JsonParser parser =
m.tokenStreamFactory().createNonBlockingByteBufferParser(ObjectReadContext.empty())) {
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(m.writeValueAsBytes(foos)));
MAPPER.reader().createNonBlockingByteArrayParser()) {
((ByteArrayFeeder) parser).feedInput(TEST_DOC, 0, TEST_DOC.length);
((ByteArrayFeeder) parser).endOfInput();
Foo[] result = MAPPER.readValue(parser, Foo[].class);
assertEquals(TEST_ITEM_COUNT, result.length);
}
}

@Test
public void testNonBlockingByteBufferParserViaMapper()
{
try (final JsonParser parser =
MAPPER.createNonBlockingByteBufferParser()) {
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(TEST_DOC));
((ByteBufferFeeder) parser).endOfInput();
Foo[] result = MAPPER.readValue(parser, Foo[].class);
assertEquals(TEST_ITEM_COUNT, result.length);
}
}

@Test
public void testNonBlockingByteBufferParserViaReader()
{
try (final JsonParser parser =
MAPPER.reader().createNonBlockingByteBufferParser()) {
((ByteBufferFeeder) parser).feedInput(ByteBuffer.wrap(TEST_DOC));
((ByteBufferFeeder) parser).endOfInput();
result = m.readValue(parser, Foo[].class);
Foo[] result = MAPPER.readValue(parser, Foo[].class);
assertEquals(TEST_ITEM_COUNT, result.length);
}
}

private byte[] _testDoc(int count) {
Foo[] foos = new Foo[count];
for (int i = 0; i < count; ++i) {
foos[i] = new Foo("bar-" + i);
}
assertEquals(len, result.length);
return MAPPER.writeValueAsBytes(foos);
}

}