OpenersPrimaryExamplesSection(Spacer1)Examples  (7)

OpenersExampleSection0(Spacer1)Basic Examples  (7)

Export a (RefLink[List, paclet:ref/List]) to the UBJSON format:

bytes = ExportString[{1,2,3}, "UBJSON"];

Import as an expression:

ImportString[bytes, "UBJSON"]

Export an (RefLink[Association, paclet:ref/Association]) to the UBJSON format:

bytes = ExportString[<|"x" -> 1, "y" -> 2.5|>, "UBJSON"];

Import as an expression:

ImportString[bytes, "UBJSON"]

Exporting an integer produces an output of variable size:

ExportString[2^7-1, "UBJSON"]//StringLength

ExportString[2^15-1, "UBJSON"]//StringLength

ExportString[2^31-1, "UBJSON"]//StringLength

ExportString[2^63-1, "UBJSON"]//StringLength

Exporting 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 (RefLink[ByteArray, paclet:ref/ByteArray]) produces a compact, strongly typed, UBJSON array output:

ExportString[ByteArray[Range[255]], "UBJSON"]//StringLength

Export the same data as a regular (RefLink[List, paclet:ref/List]):

ExportString[Range[255], "UBJSON"]//StringLength

Exporting a string of one ASCII character produces a UBJSON character:

ExportString["A", "UBJSON"]

Exporting non-ASCII characters produces a UBJSON string output:

ExportString["🐺", "UBJSON"]//StringLength

Exporting a string produces a UBJSON string:

ExportString["hello", "UBJSON"]//StringLength

Use conversion rules to transform quantities to associations:

data=ExportString[{(Quantity["42.195", "km", kilometers, "Kilometers"]),(Quantity["21.097", "km", kilometers, "Kilometers"]),(Quantity["10", "km", kilometers, "Kilometers"])}, "UBJSON","ConversionRules"->{Quantity[magnitude_, unit_]:> <|"magnitude"->magnitude,"unit"->unit|>}];

Import as an expression:

ImportString[data, "UBJSON"]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[BinarySerialize, paclet:ref/BinarySerialize])  ▪  (RefLink[BinaryDeserialize, paclet:ref/BinaryDeserialize])}, {Related Formats:  (RefLink[BSON, paclet:ref/format/BSON])  ▪  (RefLink[RawJSON, paclet:ref/format/RawJSON])  ▪  (RefLink[ExpressionJSON, paclet:ref/format/ExpressionJSON])  ▪  (RefLink[JSON, paclet:ref/format/JSON])  ▪  (RefLink[HDF5, paclet:ref/format/HDF5])  ▪  (RefLink[WXF, paclet:ref/format/WXF])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Binary Data, paclet:guide/BinaryData])}, {▪(RefLinkPlain[Importing & Exporting, paclet:guide/ImportingAndExporting])}, {▪(RefLinkPlain[Importing & Exporting Database Formats, paclet:guide/ImportingAndExportingDatabaseFormats])}, {▪(RefLinkPlain[Listing of All Formats, paclet:guide/ListingOfAllFormats])}, {▪(RefLinkPlain[Systems & Utility Formats, paclet:guide/SystemsAndUtilityFormats])}, {▪(RefLinkPlain[Tabular & Spreadsheet Formats, paclet:guide/TabularAndSpreadsheetFormats])}, {▪(RefLinkPlain[Web Formats, paclet:guide/WebFormats])}})}})