Class ByteArraySerializer

java.lang.Object
com.fasterxml.jackson.databind.JsonSerializer<byte[]>
com.fasterxml.jackson.databind.ser.std.StdSerializer<byte[]>
com.datastax.astra.internal.serdes.tables.ByteArraySerializer
All Implemented Interfaces:
com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable, com.fasterxml.jackson.databind.jsonschema.SchemaAware, Serializable

public class ByteArraySerializer extends com.fasterxml.jackson.databind.ser.std.StdSerializer<byte[]>
A custom serializer for byte arrays that encodes the array as a Base64 string and wraps it in a JSON object with a specific field name.

This serializer converts a byte array into a JSON object with the following format:

 {
   "$binary": "base64EncodedString"
 }
 

Example usage:

 
 ObjectMapper mapper = new ObjectMapper();
 SimpleModule module = new SimpleModule();
 module.addSerializer(new ByteArraySerializer());
 mapper.registerModule(module);

 byte[] data = {1, 2, 3};
 String json = mapper.writeValueAsString(data);
 
 
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class com.fasterxml.jackson.databind.JsonSerializer

    com.fasterxml.jackson.databind.JsonSerializer.None
  • Field Summary

    Fields inherited from class com.fasterxml.jackson.databind.ser.std.StdSerializer

    _handledType
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor that specifies the byte[] type for serialization.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    serialize(byte[] value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider)
    Serializes a byte array as a Base64-encoded string wrapped in a JSON object.

    Methods inherited from class com.fasterxml.jackson.databind.ser.std.StdSerializer

    _neitherNull, _nonEmpty, acceptJsonFormatVisitor, createSchemaNode, createSchemaNode, findAnnotatedContentSerializer, findContextualConvertingSerializer, findConvertingContentSerializer, findFormatFeature, findFormatOverrides, findIncludeOverrides, findPropertyFilter, getSchema, getSchema, handledType, isDefaultSerializer, visitArrayFormat, visitArrayFormat, visitFloatFormat, visitIntFormat, visitIntFormat, visitStringFormat, visitStringFormat, wrapAndThrow, wrapAndThrow

    Methods inherited from class com.fasterxml.jackson.databind.JsonSerializer

    getDelegatee, isEmpty, isEmpty, isUnwrappingSerializer, properties, replaceDelegatee, serializeWithType, unwrappingSerializer, usesObjectId, withFilterId, withIgnoredProperties

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteArraySerializer

      public ByteArraySerializer()
      Default constructor that specifies the byte[] type for serialization.
  • Method Details

    • serialize

      public void serialize(byte[] value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider) throws IOException
      Serializes a byte array as a Base64-encoded string wrapped in a JSON object.
      Specified by:
      serialize in class com.fasterxml.jackson.databind.ser.std.StdSerializer<byte[]>
      Parameters:
      value - the byte array to serialize
      gen - the JsonGenerator used to write the serialized JSON output
      provider - the SerializerProvider that can be used to get serializers for serializing objects contained within this value
      Throws:
      IOException - if an I/O error occurs during serialization