These functions can be used to encode and decode data into other formats, such as base64 and CBOR (Concise Binary Object Representation). It is particularly used when that data needs to be stored and transferred over media that are designed to deal with text. This encoding and decoding helps to ensure that the data remains intact without modification during transport.
For an overview of serialisation, parsing, analysis, and other representation transformations, see Representations and codecs.
Function | Description |
|---|---|
encoding::base64::decode() | Decodes a base64-encoded string into bytes. |
encoding::base64::encode() | Encodes bytes into a base64 string, with optional padding. |
encoding::cbor::decode() | Decodes CBOR-formatted bytes into a SurrealQL value. |
encoding::cbor::encode() | Encodes a SurrealQL value into CBOR-formatted bytes. |
encoding::json::decode() | Decodes a JSON string into a SurrealQL value. |
encoding::json::encode() | Encodes a SurrealQL value into a JSON string. |
encoding::base64::decode()
The encoding::base64::decode() function decodes a string into bytes.
encoding::base64::decode(string) -> bytesThe following example shows this function, and its output, when used in a RETURN statement:
/**[test]
[[test.results]]
value = "b"32333233""
*/
RETURN encoding::base64::decode("MjMyMw");
-- b"32333233"You can also verify that the output of the encoded value matches the original value.
/**[test]
[[test.results]]
value = "true"
*/
RETURN encoding::base64::decode("aGVsbG8") = <bytes>"hello";
-- true encoding::base64::encode()
The encoding::base64::encode() function encodes a bytes to base64 with optionally padded output.
encoding::base64::encode(bytes) -> stringThe following example shows this function, and its output, when used in a RETURN statement:
/**[test]
[[test.results]]
value = "''"
*/
RETURN encoding::base64::encode(<bytes>"");
-- ''/**[test]
[[test.results]]
value = "'MjMyMw'"
*/
RETURN encoding::base64::encode(<bytes>"2323");
-- 'MjMyMw'/**[test]
[[test.results]]
value = "'aGVsbG8'"
*/
RETURN encoding::base64::encode(<bytes>"hello");
-- 'aGVsbG8'You can pass true as the second argument to enable padded base64 outputs:
/**[test]
[[test.results]]
value = "''"
*/
RETURN encoding::base64::encode(<bytes>"", true);
-- ""/**[test]
[[test.results]]
value = "'MjMyMw=='"
[[test.results]]
value = "'MjMyMw=='"
*/
RETURN encoding::base64::encode(<bytes>"2323", true);
"MjMyMw=="/**[test]
[[test.results]]
value = "'aGVsbG8='"
[[test.results]]
value = "'aGVsbG8='"
*/
RETURN encoding::base64::encode(<bytes>"hello", true);
"aGVsbG8=" encoding::cbor::decode()
The encoding::cbor::decode() function decodes bytes in valid CBOR format into a SurrealQL value.
encoding::cbor::decode(string) -> any/**[test]
[[test.results]]
value = "NONE"
[[test.results]]
value = "{ cbor: 'encoding', is: 'pretty neat' }"
*/
LET $some_bytes = encoding::base64::decode("omRjYm9yaGVuY29kaW5nYmlza3ByZXR0eSBuZWF0");
encoding::cbor::decode($some_bytes);{
cbor: 'encoding',
is: 'pretty neat'
} encoding::cbor::encode()
The encoding::cbor::encode() function encodes any SurrealQL value into bytes in CBOR format.
encoding::cbor::encode(any) -> bytes/**[test]
[[test.results]]
value = "b"A26463626F7268656E636F64696E676269736B707265747479206E656174""
*/
encoding::cbor::encode({
cbor: "encoding",
is: "pretty neat"
});b"A26463626F7268656E636F64696E676269736B707265747479206E656174" encoding::json::decode()
The encoding::json::decode() function decodes a JSON string into a SurrealQL value.
encoding::json::decode(string) -> anyExamples of use:
encoding::json::decode('NONE');
LET $json = '{"user":{"name":"Tobie","tags":["admin","user"]}}';
encoding::json::decode($json);-------- Query --------
NONE
-------- Query --------
{
user: {
name: 'Tobie',
tags: [
'admin',
'user'
]
}
} encoding::json::encode()
The encoding::json::encode() function encodes a SurrealQL value into a JSON string.
encoding::json::encode(any) -> stringExamples of use:
encoding::json::encode("8.8");
encoding::json::encode({ some: "data"});-------- Query --------
'"8.8"'
-------- Query --------
'{"some":"data"}'As JSON has fewer data types than SurrealDB, note that a round trip from SurrealQL to JSON and back to SurrealQL is not guaranteed to be the same type.
encoding::json::decode(encoding::json::encode(NONE));
-- NULLIf a round trip is required, a combination of JSON and CBOR functions can be used.
-- Value is '[198,246]'
-- (array from CBOR byte representation of NONE)
LET $json_byte_array = encoding::json::encode(
<array>encoding::cbor::encode(NONE)
);
-- Value is b"C6F6"
-- (CBOR byte representation of NONE)
LET $byte_string = <bytes>encoding::json::decode($json_byte_array);
-- Returns NONE, not NULL
encoding::cbor::decode($byte_string);