Class JsonText.ByteArrayToStringOrJsonSerializer

java.lang.Object
com.fasterxml.jackson.databind.JsonSerializer<byte[]>
com.fasterxml.jackson.databind.ser.std.StdSerializer<byte[]>
org.diabetestechnology.drh.service.http.util.JsonText.ByteArrayToStringOrJsonSerializer
All Implemented Interfaces:
com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitable, com.fasterxml.jackson.databind.jsonschema.SchemaAware, Serializable
Enclosing class:
JsonText

public static class JsonText.ByteArrayToStringOrJsonSerializer extends com.fasterxml.jackson.databind.ser.std.StdSerializer<byte[]>
Custom serializer for byte arrays.

This serializer attempts to convert a byte array to a string and parse it as JSON. If the string is valid JSON, it serializes the byte array as JSON. If the string is not valid JSON, it serializes the byte array as a plain string.

To use this serializer, annotate the relevant field or class with @JsonSerialize(using = ByteArrayToStringOrJsonSerializer.class).

Example usage:

 
 public class Example {

     @JsonSerialize(using = ByteArrayToStringOrJsonSerializer.class)
     private byte[] byteArray;

     // getters and setters
 }
 
 

In this example, the byteArray field will be serialized using the ByteArrayToStringOrJsonSerializer, which will serialize the value as JSON if it's valid JSON, or as a plain string otherwise.

See Also:
  • Nested Class Summary

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

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

    Constructors
    Constructor
    Description
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    serialize(byte[] value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider)
     

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

    acceptJsonFormatVisitor, getSchema, getSchema, handledType, 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

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

    • ByteArrayToStringOrJsonSerializer

      public ByteArrayToStringOrJsonSerializer()
    • ByteArrayToStringOrJsonSerializer

      public ByteArrayToStringOrJsonSerializer(Class<byte[]> t)
  • Method Details

    • serialize

      public void serialize(byte[] value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider provider) throws IOException
      Specified by:
      serialize in class com.fasterxml.jackson.databind.ser.std.StdSerializer<byte[]>
      Throws:
      IOException