fabien van mook

all packages "fvm-*"

package "fvm-miscellaneous"

function file "value2json.m"

Function js = value2json(a)
         js = value2json(a,opt);

returns a string with a json representation of the input
variable a, which is obviously an Octave/Matlab scalar,
matrix, cellarray or structure.

Note the following conversions from Octave to json:
  empty matrix []        --> empty array []
  empty cellarray {}     --> empty array []
  1-element matrix       --> scalar
  1-element cellarray    --> 1-element array
  1*(n>0) matrix         --> n-element array
  1*(n>0) cellarray      --> n-element array
  (m>1)*(n>0) matrix     --> m-element array of n-element arrays
  (m>1)*(n>0) cellarray  --> m-element array of n-element arrays
  struct x.a = ...       --> object { "a" : ... }

Three- or more dimensional matrices and cellarrays are
not understood.

Struct arrays (see the help text of struct()) are not
understood, but cellarrays of struct's are understood.

Only reals and integers are understood, but no complex
numbers.

The optional second input argument is a struct for options,
Some of the options are explained below. For the other 
options which relate to subsitutions of json tokens [ ] { } 
etc. by arbitrary strings, and which relate to tagging and 
embellishment of exponents, see into the code within its
interal function value2json_options().

Real and integer values are converted basically by
sprintf(opt.numfrmt,...). String opt.numfrmt is "%1.10g" 
by default.

Note that the special numeric values NA, NaN and Inf are
returned as strings with the same code by sprintf() 
and therefore by value2json().
But in json code, these values are not allowed. The values
null, false and true are valid json code.

Octave booleans are returned as 0 or 1.

Strings and field names in struct's will start with 
opt.oquote ( " at default) and will end with opt.cquote 
(also " at default). Double quotes and other special 
characters within the string or field name are escaped 
by function undo_string_escapes().

Use the strings "_null_", "_true_" and "_false_" to
indicate the json values null, true and false respectively.

The option opt.pre1 is a string by which the output js 
starts. By default, it is "".

The option opt.pre2 is a string consisting of zero or 
more characters (normally spaces), which is prepended 
at the starts of the second, third and following
lines in the json code. By default, opt.pre2 is "".

If the value of the option opt.indent is 1, extra 
spaces are printed to make the json code more readable
by humans. This is default behaviour. If opt.indent
is 0, everything is aligned to the left of each line 
(thus no indentations).

If the value of the option opt.two_to_one is 1, 
two-dimensional matrices and cellarrays are converted
and output as one-dimensional arrays in row-major ordre 
in json code. Such matrices and cellarrays retain only 
visually their form in rows and columns. By default, 
two_to_one is 0.

The function value2json() may call the function in
following non-standard m-file:
-- tagexponents.m

See the official description of json on:
  https://www.json.org/json-en.html


(c) 2026 fabien van mook

2026.08.20 release of this file within package "fvm-miscellaneous" under GNU GPLv3+