UBJSON (.ubj)
Background & Context
-
- MIME type: application/ubjson.
- Data interchange binary format.
- Supports full UBJSON syntax.
- UBJSON identifies UBJSON objects with Wolfram Language associations.
- UBJSON is an acronym derived from Universal Binary JSON.
- Binary format with big-endian order for numeric values.
- There is a one-to-one transformation between JSON and UBJSON.
- UBJSON is specified at http://ubjson.org/.
Import & Export
- Import["file.ubj"] imports a UBJSON file as a combination of nested lists and associations.
- ImportString["string","UBJSON"] imports a UBJSON string of bytes.
- Export["file",expr,"UBJSON"] exports a combination of nested lists and associations to a UBJSON file.
- ExportString[expr,"UBJSON"] exports to a UBJSON string of bytes.
- The "UBJSON" format identifies UBJSON objects with associations in the Wolfram Language.
- "UBJSON" identifies UBJSON lists [e1,e2,…] with Wolfram Language lists {e1,e2,…}.
- Single-character strings in the Wolfram Language are represented in UBJSON as characters. Multi-character strings in the Wolfram Language are represented as UBJSON strings.
- Machine-precision integers are represented as the smallest UBJSON integer type they fit in.
- Big integers are represented as a UBJSON string of digits.
- Machine-precision numbers in the Wolfram Language are represented by the smallest compatible UBJSON float type.
- See the following reference pages for full general information:
-
Import, Export import from or export to a file CloudImport, CloudExport import from or export to a cloud object ImportString, ExportString import from or export to a string ImportByteArray, ExportByteArray import from or export to a byte array
Import Elements
- General Import elements:
-
"Elements" list of elements and options available in this file "Summary" summary of the file "Rules" list of rules for all available elements
Options
- Export options:
-
"ConversionFunction" None function to apply for additional conversions "ConversionRules" {} rules to apply to override or add conversions - The setting for "ConversionRules" is used to replace subexpressions before built-in conversions are performed.
- The setting for "ConversionFunction" is applied to subexpressions for which no built-in conversion is defined.
Examples
Basic Examples (7)
Export a List to the UBJSON format:
bytes = ExportString[{1, 2, 3}, "UBJSON"];ImportString[bytes, "UBJSON"]Export an Association to the UBJSON format:
bytes = ExportString[<|"x" -> 1, "y" -> 2.5|>, "UBJSON"];ImportString[bytes, "UBJSON"]Exporting an integer produces an output of variable size:
ExportString[2 ^ 7 - 1, "UBJSON"]//StringLengthExportString[2 ^ 15 - 1, "UBJSON"]//StringLengthExportString[2 ^ 31 - 1, "UBJSON"]//StringLengthExportString[2 ^ 63 - 1, "UBJSON"]//StringLengthExporting a big integer produces an output of size proportional to the number of digits:
ListPlot[Table[
{exponent, StringLength[ExportString[10 ^ exponent, "UBJSON"]]},
{exponent, 20, 40, 2}]]Exporting a ByteArray produces a compact, strongly typed, UBJSON array output:
ExportString[ByteArray[Range[255]], "UBJSON"]//StringLengthExport the same data as a regular List:
ExportString[Range[255], "UBJSON"]//StringLengthExporting a string of one ASCII character produces a UBJSON character:
ExportString["A", "UBJSON"]Exporting non-ASCII characters produces a UBJSON string output:
ExportString["", "UBJSON"]//StringLengthExporting a string produces a UBJSON string:
ExportString["hello", "UBJSON"]//StringLengthUse conversion rules to transform quantities to associations:
data = ExportString[{Quantity["42.195", "Kilometers"], Quantity["21.097", "Kilometers"], Quantity["10", "Kilometers"]}, "UBJSON", "ConversionRules" -> {Quantity[magnitude_, unit_] :> <|"magnitude" -> magnitude, "unit" -> unit|>}];ImportString[data, "UBJSON"]See Also
BinarySerialize BinaryDeserialize
Formats: BSON RawJSON ExpressionJSON JSON JSONLines HDF5 WXF
Related Workflows
- Generate JSON
Related Workflows
▪
History
Introduced in 2017 (11.1)