The Octave Forge package repository is no longer actively maintained. Please find Octave Packages at https://packages.octave.org.

Navigation

Operators and Keywords

Function List:

C++ API

: str = toJSON (obj)
: str = toJSON (obj, prec)
: str = toJSON (obj, compact)
: str = toJSON (obj, prec, compact)

Convert any Octave obj into a compact JSON string.

toJSON strives to convert Octave vectors, matrices and/or ND arrays to equivalent JSON arrays. Special provisions are made to handle +/-Inf and complex numbers, which are conventionally not permitted is JSON string.

Input arguments:

  • obj, any Octave object: double, float, int, logical, complex, char, etc. There are no limitations on the class accepted, but classes not permitted in JSON are merely referenced by classname, along the lines of "[octave_com_object]", and the contents are lost.
  • prec, a numeric value, specifies number of significant digits for number-to-string conversion. The default value of prec is 15.
  • compact (logical; default value is FALSE) specifies whether to return Octave struct arrays as arrays of JSON objects or JSON objects of arrays. Consider an Octave struct array with fields ’x’ and ’y’:
    • Leaving as FALSE returns a JSON array of objects, i.e.:

         ’[{"x": ..., "y": ...}, {"x": ..., "y": ...}]’   

    • Changing to TRUE returns a JSON object of arrays, i.e.:

        ’{"x": [...], "y": [...]}’         

Special cases:

The specification for JSON does not allow +/-Inf or complex numbers; nevertheless, provisions are made here to enable these important numbers:

  • Octave numbers +/-Inf return as numeric string ’+/-1e999’ which should automatically revert to +/-Inf when parsed.
  • Complex numbers return as JSON object {"re":..., "im":...}

Apparent JSON strings are left unquoted. This allows recursive use of toJSON. To prevent this, append a whitespace to the string.

The bodies of Octave inline functions are stored as string; however, reference to values external to inline function will be lost. e.g.

           @(x) a*x   =>   "@@(x) a*x"   

See also: fromJSON.

Package: io