generated from micronaut-projects/micronaut-project-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jackson-databind writes byte[] as base64 and supports reading from base64 or array. Serde 2 writes byte[] as array, but does not support reading from base64, so prior to this change we are only compatible with jackson in one direction (serde encoder -> jackson decoder). This patch adds decode support for base64, and for the bson binary type. For oracle jdbc json, a decodeBinary method already exists. On the encode side, there is now a config flag to use format-specific byte array encoding, which is base64 for json. The default remains to write an array of numbers. Fixes #520
- Loading branch information
Showing
20 changed files
with
326 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 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
68 changes: 68 additions & 0 deletions
68
serde-api/src/main/java/io/micronaut/serde/util/BinaryCodecUtil.java
This file contains 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,68 @@ | ||
/* | ||
* 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.util; | ||
|
||
import io.micronaut.core.annotation.Internal; | ||
import io.micronaut.core.type.Argument; | ||
import io.micronaut.serde.Decoder; | ||
import io.micronaut.serde.Encoder; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.Base64; | ||
|
||
/** | ||
* Common implementations for reading/writing byte arrays. | ||
*/ | ||
@Internal | ||
public final class BinaryCodecUtil { | ||
private static final Argument<byte[]> BYTE_ARRAY = Argument.of(byte[].class); | ||
|
||
private BinaryCodecUtil() { | ||
} | ||
|
||
public static byte[] decodeFromArray(Decoder base) throws IOException { | ||
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
try (Decoder arrayDecoder = base.decodeArray(BYTE_ARRAY)) { | ||
while (arrayDecoder.hasNextArrayValue()) { | ||
Byte b = arrayDecoder.decodeByteNullable(); | ||
buffer.write(b == null ? 0 : b); | ||
} | ||
} | ||
return buffer.toByteArray(); | ||
} | ||
|
||
public static byte[] decodeFromString(Decoder base) throws IOException { | ||
String s = base.decodeString(); | ||
try { | ||
return Base64.getDecoder().decode(s); | ||
} catch (IllegalArgumentException e) { | ||
throw base.createDeserializationException("Illegal base64 input: " + e.getMessage(), null); | ||
} | ||
} | ||
|
||
public static void encodeToArray(Encoder encoder, byte[] data) throws IOException { | ||
try (Encoder arrayEncoder = encoder.encodeArray(BYTE_ARRAY)) { | ||
for (byte i : data) { | ||
arrayEncoder.encodeByte(i); | ||
} | ||
} | ||
} | ||
|
||
public static void encodeToString(Encoder encoder, byte[] data) throws IOException { | ||
encoder.encodeString(Base64.getEncoder().encodeToString(data)); | ||
} | ||
} |
This file contains 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 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 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
11 changes: 11 additions & 0 deletions
11
serde-bson/src/test/java/io/micronaut/serde/bson/BinaryTypes.java
This file contains 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,11 @@ | ||
package io.micronaut.serde.bson; | ||
|
||
import io.micronaut.serde.annotation.Serdeable; | ||
|
||
@Serdeable | ||
public record BinaryTypes( | ||
byte[] uuid, | ||
byte[] normal, | ||
byte[] userDefined | ||
) { | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.