Skip to content

jsonencode

builtin · libnexusinterp/corefcn/jsonencode-proprietary.cc

: text = jsonencode (obj)
: text = jsonencode (…, "ConvertInfAndNaN", tf)
: text = jsonencode (…, "PrettyPrint", tf)

Encode the value obj as a JSON document, returned as a character row.

Numbers use the shortest text that reads back as the same value. Logical values become true and false, character rows become strings (a character matrix gives an array of strings), and empty arrays become []. Vectors become flat arrays, matrices arrays of rows and N-dimensional arrays nested arrays whose indices follow obj(i,j,k). A cell array becomes an array of its encoded elements, a scalar structure an object with the fields in order, and a structure array an array of objects. A table becomes a JSON array of row objects (always an array, even for height 0 or 1). A containers.Map with string keys becomes a JSON object. categorical values become strings (or an array of strings), datetime values become their char form, and enumeration members become their names. A leading run of xHHHH_ in a structure field name (the letter x, four hexadecimal digits and an underscore) is written as that Unicode code point, so x0024_type is the key $type and x0040_context is @context. A field name that does not start with that pattern is written literally. Sparse input is encoded as the corresponding full array.

NaN, Inf and -Inf are written as null, unless "ConvertInfAndNaN" is false, in which case the words NaN, Infinity and -Infinity are used. With "PrettyPrint" true the result is indented by two spaces per level. Option names are not case sensitive.

Complex numbers and function handles cannot be encoded.