Package jsonvalues

Class JsBinary

All Implemented Interfaces:
JsValue

public final class JsBinary extends JsPrimitive
Represents an array of bytes in a JSON-like data structure. The purpose of the JsBinary class is to allow the inclusion of binary data within JSON-like objects, even though binary data is not part of the JSON specification. This class provides a means to serialize binary data into a string using the Base64 encoding scheme, making it compatible with JSON-like structures.

Binary data is often encountered in various data formats, and while JSON itself does not support binary data, the JsBinary class serves as a custom solution for embedding binary content within JSON-like objects.

When serialized to a JSON string, a JsBinary object is represented as a string containing the Base64-encoded binary data. Consequently, a JsBinary object and a JsStr object created from the Base64-encoded string are considered equal if the encoded content matches.

Here is an example of how to use JsBinary:


 // Create a byte array containing binary data (e.g., image bytes)
 byte[] bytes = "foo".getBytes();

 // Encode the binary data as a Base64 string
 String base64 = Base64.getEncoder().encodeToString(bytes);

 // Create a JsBinary object from the byte array
 JsBinary binary = JsBinary.of(bytes);

 // Create a JsStr object from the Base64-encoded string
 JsStr strFromBase64 = JsStr.of(base64);

 // Check if the JsBinary and JsStr objects are considered equal
 boolean areEqual = binary.equals(strFromBase64); // true
 

It's important to note that while JsBinary is a useful tool for representing binary data in JSON-like structures, it is not part of the JSON standard. Its purpose is to provide a means of encoding binary data within a format that can coexist with JSON-like data structures.

See Also:
  • Field Details

    • prism

      public static final fun.optic.Prism<JsValue,byte[]> prism
      prism between the sum type JsValue and JsBinary
    • value

      public final byte[] value
      the array of bytes
  • Method Details

    • of

      public static JsBinary of(byte[] bytes)
      Creates a JsBinary from an array of bytes
      Parameters:
      bytes - the array of bytes
      Returns:
      an immutable JsBinary
    • of

      public static JsBinary of(String base64)
      Creates a JsBinary from an array of bytes encoded as a string in base64
      Parameters:
      base64 - the string
      Returns:
      an immutable JsBinary
      Throws:
      IllegalArgumentException - if base64 is not in valid Base64 scheme
    • toJsPrimitive

      public JsPrimitive toJsPrimitive()
      Description copied from interface: JsValue
      Returns this JsValue as a JsPrimitive.
      Returns:
      This JsValue as a JsPrimitive.
    • isBinary

      public boolean isBinary()
      Description copied from interface: JsValue
      Returns true if this JsValue is a JsBinary or a JsString with a value that is an array of bytes encoded in base64.
      Returns:
      True if this JsValue is a JsBinary or a JsString with a value that is an array of bytes encoded in base64.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object