WXF (.wxf)
- Import and Export fully support Wolfram Language WXF files.
- WXF byte arrays can be created with BinarySerialize and read with BinaryDeserialize.
Background & Context
-
- Wolfram exchange format.
- Binary format.
- Represents arbitrary Wolfram Language expressions in a serialized, platform-independent form.
- Versioned format.
- Developed in 2017 by Wolfram Research.
Import & Export
- Import["file.wxf"] imports a WXF file as a Wolfram Language expression.
- ImportString["string","WXF"] imports a WXF string of bytes.
- ImportByteArray[bytearray, "WXF"] imports a WXF array of bytes.
- Export["file.wxf",expr] exports the binary representation of an expr to a WXF file.
- 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 - Import elements:
-
"Expression" returns the serialized expression "HeldExpression" returns the expression wrapped in HoldComplete "ExprStruct" returns the expression as an "ExprStruct" data structure - Import by default uses the "Expression" element for Wolfram Language WXF files.
Options
- Export options:
-
PerformanceGoal Automatic specify a serialization strategy Method Automatic serialization method options
Examples
open all close allBasic Examples (4)
ExportByteArray[{1, 2, 3}, "WXF"]ExportByteArray["hello!", "WXF"]Export a symbolic expression to WXF:
ExportByteArray[f[Cos[2Pi α]], "WXF"]ImportByteArray[%, "WXF"]Export an arbitrary Wolfram Language expression to the WXF format:
bytes = ExportString[Plot3D[Sin[x] Cos[2 y], {x, -2, 2}, {y, -3, 3}], "WXF"];ImportString[bytes, "WXF"]Scope (2)
Serialize an expression to a WXF byte array:
bytes = ExportByteArray[GeoGraphics[GeoCircle[Here, Quantity[3, "Kilometers"]]], "WXF"]Deserialize the output using BinaryDeserialize:
BinaryDeserialize[bytes]Serialize an expression to a WXF byte array:
bytes = BinarySerialize[RandomImage[1, {2, 2}, ColorSpace -> "RGB"]]Import it using automatic format detection:
ImportByteArray[bytes]Import Elements (4)
Import["ExampleData/atoms.wxf", "Elements"]Serialize an unevaluated expression:
bytes = BinarySerialize[Unevaluated[1 + 1]]Import it using the default element "Expression":
ImportByteArray[bytes]Import it in a held form using the element "HeldExpression":
ImportByteArray[bytes, {"WXF", "HeldExpression"}]Get WXF data from an untrusted source:
wxf = ByteArray["ODpmAHMKQ2xvY2tHYXVnZQ=="];Inspect the element "HeldExpression":
ImportByteArray[wxf, {"WXF", "HeldExpression"}]The code is safe; evaluate it:
ImportByteArray[wxf, {"WXF", "Expression"}]Import bytes with the element "ExprStruct":
bytes = BinarySerialize[Unevaluated[Print[1 + 1]]];
ImportByteArray[bytes, {"WXF", "ExprStruct"}]The expression is held in an unevaluated state:
%["ConstructWith", Hold]["Get"]Export Options (5)
PerformanceGoal (2)
Serialize an expression using BinarySerialize to produce a compressed output:
bytes = BinarySerialize[Range[5], PerformanceGoal -> "Size"]Import the resulting byte array:
ImportByteArray[bytes, "WXF"]bytes = ExportString[ExampleData[{"Statistics", "USCityTemperature"} ], "WXF"];Serialize the same dataset with PerformanceGoal set to "Size":
compressed = ExportString[ExampleData[{"Statistics", "USCityTemperature"} ], "WXF", PerformanceGoal -> "Size"];Compute the size of the outputs:
Column[{Quantity[ByteCount[bytes], "Bytes"], Quantity[ByteCount[compressed], "Bytes"]}]Both represent the same expression:
ImportString[bytes, "WXF"] === ImportString[compressed, "WXF"]Method (3)
By default, the WXF serialization of packed arrays of integers uses the smallest integer type that fits the data:
ExportByteArray[Range[3], "WXF"]Export a packed array using a bigger integer type:
ExportByteArray[Range[3], "WXF", Method -> "PackedArrayIntegerType" -> "Integer32"]Create a packed array of real values:
reals = RandomReal[10, 4]By default, the WXF serialization of a packed array of real values uses machine doubles:
ExportByteArray[reals, "WXF"]Export the array using machine floats:
ExportByteArray[reals, "WXF", Method -> {"PackedArrayRealType" -> "Real32"}]Create a packed array of complex values:
complexes = RandomComplex[1 - I, 2]By default, the WXF serialization of packed array of complex values uses two machine doubles to represent one complex value:
ExportByteArray[complexes, "WXF"]Export the array using lower precision:
ExportByteArray[complexes, "WXF", Method -> {"PackedArrayComplexType" -> "Complex64"}]Properties & Relations (2)
ExportByteArray[expr,"WXF"] is effectively equivalent to BinarySerialize[expr]:
ExportByteArray[{1, 2, 3}, "WXF"] === BinarySerialize[{1, 2, 3}]ImportByteArray[ba,"WXF"] is effectively equivalent to BinaryDeserialize[ba]:
ba = ByteArray[{56, 58, 102, 3, 115, 4, 76, 105, 115, 116, 67, 1, 67, 2, 67, 3}];ImportByteArray[ba, "WXF"] === BinaryDeserialize[ba]Tech Notes
Related Guides
History
Introduced in 2018 (11.3)