- All Implemented Interfaces:
JsValue
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionboolean
int
hashCode()
boolean
isBinary()
Returns true if this JsValue is a JsBinary or a JsString with a value that is an array of bytes encoded in base64.static JsBinary
of
(byte[] bytes) Creates a JsBinary from an array of bytesstatic JsBinary
Creates a JsBinary from an array of bytes encoded as a string in base64Returns this JsValue as a JsPrimitive.toString()
Methods inherited from class jsonvalues.JsPrimitive
isJson, isPrimitive
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface jsonvalues.JsValue
ifNothing, ifNull, isArray, isArray, isBigDec, isBigDec, isBigInt, isBigInt, isBool, isDecimal, isDouble, isDouble, isFalse, isInstant, isInstant, isInt, isInt, isIntegral, isJson, isLong, isLong, isNothing, isNotNothing, isNotNull, isNotNumber, isNull, isNumber, isObj, isObj, isSameType, isStr, isStr, isTrue, toJsArray, toJsBigDec, toJsBigInt, toJsBinary, toJsBool, toJsDouble, toJsInstant, toJsInt, toJsLong, toJsNumber, toJsObj, toJson, toJsStr
-
Field Details
-
prism
prism between the sum type JsValue and JsBinary -
value
public final byte[] valuethe array of bytes
-
-
Method Details
-
of
Creates a JsBinary from an array of bytes- Parameters:
bytes
- the array of bytes- Returns:
- an immutable JsBinary
-
of
Creates a JsBinary from an array of bytes encoded as a string in base64- Parameters:
base64
- the string- Returns:
- an immutable JsBinary
- Throws:
IllegalArgumentException
- ifbase64
is not in valid Base64 scheme
-
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
-
equals
-
hashCode
public int hashCode()
-