generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 17
imp: ByteBufferSerde #604
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
imp: ByteBufferSerde #604
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fc5ddb4
imp: ByteBufferSerde
sdelamo e492cc7
add comment
sdelamo efe6b37
add ByteBufferBackedInputStream
sdelamo 2cf6366
simpler copying
yawkat ef29774
remove stream
yawkat 6c90498
spotless
yawkat 39d47db
Update test-suite-tck-serde/src/test/java/io/micronaut/serde/tck/test…
timyates File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
serde-support/src/main/java/io/micronaut/serde/support/serdes/ByteBufferSerde.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.serde.support.serdes; | ||
|
||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.type.Argument; | ||
import io.micronaut.serde.Decoder; | ||
import io.micronaut.serde.Encoder; | ||
import io.micronaut.serde.Serde; | ||
import jakarta.inject.Singleton; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
/** | ||
* {@link Serde} implementation of {@link java.nio.ByteBuffer}. | ||
* This is a based on `com.fasterxml.jackson.databind.ser.std.ByteBufferSerializer` which is licenced under the Apache 2.0 licence. | ||
*/ | ||
@Singleton | ||
public class ByteBufferSerde implements Serde<ByteBuffer> { | ||
@Override | ||
public @Nullable ByteBuffer deserialize(@NonNull Decoder decoder, | ||
@NonNull DecoderContext context, | ||
@NonNull Argument<? super ByteBuffer> type) throws IOException { | ||
byte[] b = decoder.decodeBinary(); | ||
return ByteBuffer.wrap(b); | ||
} | ||
|
||
@Override | ||
public void serialize(@NonNull Encoder encoder, | ||
@NonNull EncoderContext context, | ||
@NonNull Argument<? extends ByteBuffer> type, | ||
@NonNull ByteBuffer value) throws IOException { | ||
ByteBuffer slice = value.asReadOnlyBuffer(); | ||
ByteBuffer copy = ByteBuffer.allocate(slice.remaining()); | ||
copy.put(slice); | ||
encoder.encodeBinary(copy.array()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...tck-tests/src/main/java/io/micronaut/serde/tck/tests/ByteArrayWriteValueAsStringTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.serde.tck.tests; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.json.JsonMapper; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
import java.io.IOException; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Property(name = "micronaut.serde.write-binary-as-array", value = StringUtils.FALSE) | ||
@MicronautTest(startApplication = false) | ||
public class ByteArrayWriteValueAsStringTest { | ||
|
||
/** | ||
* byte[] is written as base64 string | ||
* @param jsonMapper JSONMapper either Jackson or Serde implementation | ||
* @throws IOException If an unrecoverable error occurs | ||
*/ | ||
@Test | ||
public void testByteArrayAsString(JsonMapper jsonMapper) throws IOException { | ||
final byte[] INPUT_BYTES = new byte[] { 1, 2, 3, 4, 5 }; | ||
assertEquals("\"AQIDBAU=\"", jsonMapper.writeValueAsString(INPUT_BYTES)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...tests/src/main/java/io/micronaut/serde/tck/tests/bytebuffer/ByteBufferDuplicatedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.serde.tck.tests.bytebuffer; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.json.JsonMapper; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Property(name = "micronaut.serde.write-binary-as-array", value = StringUtils.FALSE) | ||
@MicronautTest(startApplication = false) | ||
public class ByteBufferDuplicatedTest { | ||
/** | ||
* Test ported from com.fasterxml.jackson.databind.ser.jdk.JDKTypeSerializationTest | ||
* @param jsonMapper JSONMapper either Jackson or Serde implementation | ||
* @throws IOException If an unrecoverable error occurs | ||
*/ | ||
@Test | ||
public void testDuplicatedByteBufferWithCustomPosition(JsonMapper jsonMapper) throws IOException { | ||
final byte[] INPUT_BYTES = new byte[] { 1, 2, 3, 4, 5 }; | ||
|
||
String exp = jsonMapper.writeValueAsString(new byte[] { 3, 4, 5 }); | ||
ByteBuffer bbuf = ByteBuffer.wrap(INPUT_BYTES); | ||
bbuf.position(2); | ||
ByteBuffer duplicated = bbuf.duplicate(); | ||
assertEquals(exp, jsonMapper.writeValueAsString(duplicated)); | ||
|
||
// also check differently constructed bytebuffer (noting that | ||
// offset given is the _position_ to use, NOT array offset | ||
exp = jsonMapper.writeValueAsString(new byte[] { 2, 3, 4 }); | ||
bbuf = ByteBuffer.wrap(INPUT_BYTES, 1, 3); | ||
assertEquals(exp, jsonMapper.writeValueAsString(bbuf.duplicate())); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...tck-tests/src/main/java/io/micronaut/serde/tck/tests/bytebuffer/ByteBufferNativeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.serde.tck.tests.bytebuffer; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.json.JsonMapper; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Property(name = "micronaut.serde.write-binary-as-array", value = StringUtils.FALSE) | ||
@MicronautTest(startApplication = false) | ||
public class ByteBufferNativeTest { | ||
/** | ||
* Test ported from com.fasterxml.jackson.databind.ser.jdk.JDKTypeSerializationTest | ||
* @param jsonMapper JSONMapper either Jackson or Serde implementation | ||
* @throws IOException If an unrecoverable error occurs | ||
*/ | ||
@Test | ||
public void testByteBufferNative(JsonMapper jsonMapper) throws IOException { | ||
final byte[] INPUT_BYTES = new byte[]{1, 2, 3, 4, 5}; | ||
String exp = jsonMapper.writeValueAsString(INPUT_BYTES); | ||
// so far so good, but must ensure Native buffers also work: | ||
ByteBuffer bbuf2 = ByteBuffer.allocateDirect(5); | ||
bbuf2.put(INPUT_BYTES); | ||
bbuf2.flip(); | ||
assertEquals(exp, jsonMapper.writeValueAsString(bbuf2)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...tck-tests/src/main/java/io/micronaut/serde/tck/tests/bytebuffer/ByteBufferSlicedTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.serde.tck.tests.bytebuffer; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.json.JsonMapper; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@Property(name = "micronaut.serde.write-binary-as-array", value = StringUtils.FALSE) | ||
@MicronautTest(startApplication = false) | ||
public class ByteBufferSlicedTest { | ||
|
||
/** | ||
* Test ported from com.fasterxml.jackson.databind.ser.jdk.JDKTypeSerializationTest | ||
* @param jsonMapper JSONMapper either Jackson or Serde implementation | ||
* @throws IOException If an unrecoverable error occurs | ||
*/ | ||
@Test | ||
public void testSlicedByteBuffer(JsonMapper jsonMapper) throws IOException { | ||
final byte[] INPUT_BYTES = new byte[] { 1, 2, 3, 4, 5 }; | ||
ByteBuffer bbuf = ByteBuffer.wrap(INPUT_BYTES); | ||
|
||
bbuf.position(2); | ||
ByteBuffer slicedBuf = bbuf.slice(); | ||
|
||
assertEquals(jsonMapper.writeValueAsString(new byte[] { 3, 4, 5 }), | ||
jsonMapper.writeValueAsString(slicedBuf)); | ||
|
||
// but how about offset within? | ||
slicedBuf.position(1); | ||
assertEquals(jsonMapper.writeValueAsString(new byte[] { 4, 5 }), | ||
jsonMapper.writeValueAsString(slicedBuf)); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
serde-tck-tests/src/main/java/io/micronaut/serde/tck/tests/bytebuffer/ByteBufferTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.serde.tck.tests.bytebuffer; | ||
|
||
import io.micronaut.context.annotation.Property; | ||
import io.micronaut.core.util.StringUtils; | ||
import io.micronaut.json.JsonMapper; | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
@Property(name = "micronaut.serde.write-binary-as-array", value = StringUtils.FALSE) | ||
@MicronautTest(startApplication = false) | ||
public class ByteBufferTest { | ||
|
||
/** | ||
* Test ported from com.fasterxml.jackson.databind.ser.jdk.JDKTypeSerializationTest | ||
* @param jsonMapper JSONMapper either Jackson or Serde implementation | ||
* @throws IOException If an unrecoverable error occurs | ||
*/ | ||
@Test | ||
public void testByteBuffer(JsonMapper jsonMapper) throws IOException { | ||
final byte[] INPUT_BYTES = new byte[]{1, 2, 3, 4, 5}; | ||
String exp = jsonMapper.writeValueAsString(INPUT_BYTES); | ||
ByteBuffer bbuf = ByteBuffer.wrap(INPUT_BYTES); | ||
assertEquals(exp, jsonMapper.writeValueAsString(bbuf)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
serde-tck-tests/src/main/java/io/micronaut/serde/tck/tests/bytebuffer/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/** | ||
* Tests ported from Jackson Databind project. | ||
* Jackson-databind is licensed under Apache 2.0. License | ||
*/ | ||
package io.micronaut.serde.tck.tests.bytebuffer; |
10 changes: 10 additions & 0 deletions
10
test-suite-tck-jackson-databind/src/test/resources/logback.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
1 change: 1 addition & 0 deletions
1
...e-tck-serde/src/test/java/io/micronaut/serde/tck/tests/serde/SerializationSerdeSuite.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
<root level="info"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.