Eclipse Vert.x の JSON 型は RFC-7493 を実装します。以前のリリースの Eclipse Vert.x では、実装は Base64URL ではなく Base64 エンコーダーが誤って使用されていました。Eclipse Vert.x 4 で修正され、JSON 型で Base64URL エンコーダーが使用されました。
java -Dvertx.json.base64=legacy ...
java -Dvertx.json.base64=legacy ...
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
アプリケーションを部分的に移行した場合は、Eclipse Vert.x 3.x から Eclipse Vert.x 4 への移行中に、バージョン 3 と 4 の両方にアプリケーションが存在します。2 つのバージョンの Eclipse Vert.x がある場合は、次のユーティリティーを使用して Base64 文字列を Base64URL に変換できます。
public String toBase64(String base64Url) {
return base64Url
.replace('+', '-')
.replace('/', '_');
}
public String toBase64Url(String base64) {
return base64
.replace('-', '+')
.replace('_', '/');
}
public String toBase64(String base64Url) {
return base64Url
.replace('+', '-')
.replace('/', '_');
}
public String toBase64Url(String base64) {
return base64
.replace('-', '+')
.replace('_', '/');
}
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
String base64url = someJsonObject.getString("base64encodedElement")
String base64 = toBase64(base64url);
String base64url = someJsonObject.getString("base64encodedElement")
String base64 = toBase64(base64url);
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow
// simple deserializer from Base64 to byte[]
class ByteArrayDeserializer extends JsonDeserializer<byte[]> {
ByteArrayDeserializer() {
}
public byte[] deserialize(JsonParser p, DeserializationContext ctxt) {
String text = p.getText();
return Base64.getDecoder()
.decode(text);
}
}
// ...
ObjectMapper mapper = new ObjectMapper();
// create a custom module to address the Base64 decoding
SimpleModule module = new SimpleModule();
module.addDeserializer(byte[].class, new ByteArrayDeserializer());
mapper.registerModule(module);
// JSON to POJO with custom deserializer
mapper.readValue(json, MyClass.class);
// simple deserializer from Base64 to byte[]
class ByteArrayDeserializer extends JsonDeserializer<byte[]> {
ByteArrayDeserializer() {
}
public byte[] deserialize(JsonParser p, DeserializationContext ctxt) {
String text = p.getText();
return Base64.getDecoder()
.decode(text);
}
}
// ...
ObjectMapper mapper = new ObjectMapper();
// create a custom module to address the Base64 decoding
SimpleModule module = new SimpleModule();
module.addDeserializer(byte[].class, new ByteArrayDeserializer());
mapper.registerModule(module);
// JSON to POJO with custom deserializer
mapper.readValue(json, MyClass.class);
Copy to Clipboard
Copied!
Toggle word wrap
Toggle overflow