annotate libinterp/corefcn/jsondecode.cc @ 31605:e88a07dec498 stable

maint: Use macros to begin/end C++ namespaces. * oct-conf-post-public.in.h: Define two macros (OCTAVE_BEGIN_NAMESPACE, OCTAVE_END_NAMESPACE) that can be used to start/end a namespace. * mk-opts.pl, build-env.h, build-env.in.cc, __betainc__.cc, __contourc__.cc, __dsearchn__.cc, __eigs__.cc, __expint__.cc, __ftp__.cc, __gammainc__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __lin_interpn__.cc, __magick_read__.cc, __pchip_deriv__.cc, __qp__.cc, amd.cc, auto-shlib.cc, auto-shlib.h, balance.cc, base-text-renderer.cc, base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, colloc.cc, conv2.cc, daspk.cc, dasrt.cc, dassl.cc, data.cc, data.h, debug.cc, defaults.cc, defaults.h, defun-int.h, defun.cc, det.cc, dirfns.cc, display.cc, display.h, dlmread.cc, dmperm.cc, dot.cc, dynamic-ld.cc, dynamic-ld.h, eig.cc, ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc, fftn.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, getgrent.cc, getpwent.cc, getrusage.cc, givens.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, graphics.in.h, gsvd.cc, gtk-manager.cc, gtk-manager.h, hash.cc, help.cc, help.h, hess.cc, hex2num.cc, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, inv.cc, jsondecode.cc, jsonencode.cc, kron.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-ascii-helper.cc, ls-ascii-helper.h, ls-oct-text.cc, ls-utils.cc, ls-utils.h, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex-private.h, mex.cc, mgorth.cc, nproc.cc, oct-fstrm.cc, oct-fstrm.h, oct-hdf5-types.cc, oct-hdf5-types.h, oct-hist.cc, oct-hist.h, oct-iostrm.cc, oct-iostrm.h, oct-opengl.h, oct-prcstrm.cc, oct-prcstrm.h, oct-procbuf.cc, oct-procbuf.h, oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.cc, oct-strstrm.h, oct-tex-lexer.in.ll, oct-tex-parser.yy, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-flt-fmt.cc, pr-output.cc, procstream.cc, procstream.h, psi.cc, qr.cc, quad.cc, quadcc.cc, qz.cc, rand.cc, rcond.cc, regexp.cc, schur.cc, settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xdiv.cc, sparse-xdiv.h, sparse-xpow.cc, sparse-xpow.h, sparse.cc, spparms.cc, sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfind.cc, strfns.cc, sub2ind.cc, svd.cc, sylvester.cc, symbfact.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, syscalls.cc, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, tril.cc, tsearch.cc, typecast.cc, url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, xdiv.cc, xdiv.h, xnorm.cc, xnorm.h, xpow.cc, xpow.h, __delaunayn__.cc, __fltk_uigetfile__.cc, __glpk__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audiodevinfo.cc, audioread.cc, convhulln.cc, fftw.cc, gzip.cc, mk-build-env-features.sh, mk-builtins.pl, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-builtin.h, ov-cell.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-complex.cc, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-java.cc, ov-java.h, ov-mex-fcn.h, ov-null-mat.cc, ov-oncleanup.cc, ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, octave.cc, octave.h, mk-ops.sh, op-b-b.cc, op-b-bm.cc, op-b-sbm.cc, op-bm-b.cc, op-bm-bm.cc, op-bm-sbm.cc, op-cdm-cdm.cc, op-cell.cc, op-chm.cc, op-class.cc, op-cm-cm.cc, op-cm-cs.cc, op-cm-m.cc, op-cm-s.cc, op-cm-scm.cc, op-cm-sm.cc, op-cs-cm.cc, op-cs-cs.cc, op-cs-m.cc, op-cs-s.cc, op-cs-scm.cc, op-cs-sm.cc, op-dm-dm.cc, op-dm-scm.cc, op-dm-sm.cc, op-dm-template.cc, op-dms-template.cc, op-fcdm-fcdm.cc, op-fcm-fcm.cc, op-fcm-fcs.cc, op-fcm-fm.cc, op-fcm-fs.cc, op-fcn.cc, op-fcs-fcm.cc, op-fcs-fcs.cc, op-fcs-fm.cc, op-fcs-fs.cc, op-fdm-fdm.cc, op-fm-fcm.cc, op-fm-fcs.cc, op-fm-fm.cc, op-fm-fs.cc, op-fs-fcm.cc, op-fs-fcs.cc, op-fs-fm.cc, op-fs-fs.cc, op-i16-i16.cc, op-i32-i32.cc, op-i64-i64.cc, op-i8-i8.cc, op-int-concat.cc, op-m-cm.cc, op-m-cs.cc, op-m-m.cc, op-m-s.cc, op-m-scm.cc, op-m-sm.cc, op-mi.cc, op-pm-pm.cc, op-pm-scm.cc, op-pm-sm.cc, op-pm-template.cc, op-range.cc, op-s-cm.cc, op-s-cs.cc, op-s-m.cc, op-s-s.cc, op-s-scm.cc, op-s-sm.cc, op-sbm-b.cc, op-sbm-bm.cc, op-sbm-sbm.cc, op-scm-cm.cc, op-scm-cs.cc, op-scm-m.cc, op-scm-s.cc, op-scm-scm.cc, op-scm-sm.cc, op-sm-cm.cc, op-sm-cs.cc, op-sm-m.cc, op-sm-s.cc, op-sm-scm.cc, op-sm-sm.cc, op-str-m.cc, op-str-s.cc, op-str-str.cc, op-struct.cc, op-ui16-ui16.cc, op-ui32-ui32.cc, op-ui64-ui64.cc, op-ui8-ui8.cc, ops.h, anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, lex.ll, oct-lvalue.cc, oct-lvalue.h, oct-parse.yy, parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-vm-eval.cc, pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h : Use new macros to begin/end C++ namespaces.
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 14:23:45 -0800
parents 796f54d4ddbf
children aac27ad79be6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
1 ////////////////////////////////////////////////////////////////////////
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
2 //
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 30390
diff changeset
3 // Copyright (C) 2020-2022 The Octave Project Developers
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
4 //
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
5 // See the file COPYRIGHT.md in the top-level directory of this
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
6 // distribution or <https://octave.org/copyright/>.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
7 //
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
8 // This file is part of Octave.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
9 //
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
10 // Octave is free software: you can redistribute it and/or modify it
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
11 // under the terms of the GNU General Public License as published by
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
12 // the Free Software Foundation, either version 3 of the License, or
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
13 // (at your option) any later version.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
14 //
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
15 // Octave is distributed in the hope that it will be useful, but
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
18 // GNU General Public License for more details.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
19 //
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
20 // You should have received a copy of the GNU General Public License
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
21 // along with Octave; see the file COPYING. If not, see
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
22 // <https://www.gnu.org/licenses/>.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
23 //
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
24 ////////////////////////////////////////////////////////////////////////
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
25
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
26 #if defined (HAVE_CONFIG_H)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
27 # include "config.h"
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
28 #endif
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
29
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
30 #include "defun.h"
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
31 #include "error.h"
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
32 #include "errwarn.h"
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
33 #include "oct-string.h"
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
34 #include "ovl.h"
29092
38e22065d9ec jsondecode.cc: use `octave::make_valid_name` not `matlab.lang.makeValidName` (patch #9998)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28801
diff changeset
35 #include "utils.h"
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
36
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
37 #if defined (HAVE_RAPIDJSON)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
38 # include <rapidjson/document.h>
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
39 # include <rapidjson/error/en.h>
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
40 #endif
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
41
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
42 OCTAVE_BEGIN_NAMESPACE(octave)
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
43
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
44 #if defined (HAVE_RAPIDJSON)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
45
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
46 static octave_value
29092
38e22065d9ec jsondecode.cc: use `octave::make_valid_name` not `matlab.lang.makeValidName` (patch #9998)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28801
diff changeset
47 decode (const rapidjson::Value& val,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
48 const octave::make_valid_name_options *options);
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
49
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
50 //! Decodes a numerical JSON value into a scalar number.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
51 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
52 //! @param val JSON value that is guaranteed to be a numerical value.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
53 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
54 //! @return @ref octave_value that contains the numerical value of @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
55 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
56 //! @b Example:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
57 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
58 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
59 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
60 //! d.Parse ("123");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
61 //! octave_value num = decode_number (d);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
62 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
63
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
64 static octave_value
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
65 decode_number (const rapidjson::Value& val)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
66 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
67 if (val.IsUint ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
68 return octave_value (val.GetUint ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
69 else if (val.IsInt ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
70 return octave_value (val.GetInt ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
71 else if (val.IsUint64 ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
72 return octave_value (val.GetUint64 ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
73 else if (val.IsInt64 ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
74 return octave_value (val.GetInt64 ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
75 else if (val.IsDouble ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
76 return octave_value (val.GetDouble ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
77 else
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
78 error ("jsondecode: unidentified type");
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
79 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
80
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
81 //! Decodes a JSON object into a scalar struct.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
82 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
83 //! @param val JSON value that is guaranteed to be a JSON object.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
84 //! @param options @c ReplacementStyle and @c Prefix options with their values.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
85 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
86 //! @return @ref octave_value that contains the equivalent scalar struct of @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
87 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
88 //! @b Example:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
89 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
90 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
91 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
92 //! d.Parse ("{\"a\": 1, \"b\": 2}");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
93 //! octave_value struct = decode_object (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
94 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
95
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
96 static octave_value
29092
38e22065d9ec jsondecode.cc: use `octave::make_valid_name` not `matlab.lang.makeValidName` (patch #9998)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28801
diff changeset
97 decode_object (const rapidjson::Value& val,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
98 const octave::make_valid_name_options *options)
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
99 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
100 octave_scalar_map retval;
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
101
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
102 for (const auto& pair : val.GetObject ())
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
103 {
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
104 // Validator function "matlab.lang.makeValidName" to guarantee legitimate
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
105 // variable name.
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
106 std::string varname = pair.name.GetString ();
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
107 if (options != nullptr)
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
108 octave::make_valid_name (varname, *options);
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
109 retval.assign (varname, decode (pair.value, options));
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
110 }
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
111
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
112 return retval;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
113 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
114
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
115 //! Decodes a JSON array that contains only numerical or null values
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
116 //! into an NDArray.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
117 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
118 //! @param val JSON value that is guaranteed to be a numeric array.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
119 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
120 //! @return @ref octave_value that contains the equivalent NDArray of @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
121 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
122 //! @b Example:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
123 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
124 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
125 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
126 //! d.Parse ("[1, 2, 3, 4]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
127 //! octave_value numeric_array = decode_numeric_array (d);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
128 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
129
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
130 static octave_value
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
131 decode_numeric_array (const rapidjson::Value& val)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
132 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
133 NDArray retval (dim_vector (val.Size (), 1));
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
134 octave_idx_type index = 0;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
135 for (const auto& elem : val.GetArray ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
136 retval(index++) = elem.IsNull () ? octave_NaN
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
137 : decode_number (elem).double_value ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
138 return retval;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
139 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
140
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
141 //! Decodes a JSON array that contains only boolean values into a boolNDArray.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
142 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
143 //! @param val JSON value that is guaranteed to be a boolean array.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
144 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
145 //! @return @ref octave_value that contains the equivalent boolNDArray of @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
146 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
147 //! @b Example:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
148 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
149 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
150 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
151 //! d.Parse ("[true, false, true]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
152 //! octave_value boolean_array = decode_boolean_array (d);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
153 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
154
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
155 static octave_value
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
156 decode_boolean_array (const rapidjson::Value& val)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
157 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
158 boolNDArray retval (dim_vector (val.Size (), 1));
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
159 octave_idx_type index = 0;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
160 for (const auto& elem : val.GetArray ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
161 retval(index++) = elem.GetBool ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
162 return retval;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
163 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
164
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
165 //! Decodes a JSON array that contains different types
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
166 //! or string values only into a Cell.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
167 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
168 //! @param val JSON value that is guaranteed to be a mixed or string array.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
169 //! @param options @c ReplacementStyle and @c Prefix options with their values.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
170 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
171 //! @return @ref octave_value that contains the equivalent Cell of @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
172 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
173 //! @b Example (decoding a string array):
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
174 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
175 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
176 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
177 //! d.Parse ("[\"foo\", \"bar\", \"baz\"]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
178 //! octave_value cell = decode_string_and_mixed_array (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
179 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
180 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
181 //! @b Example (decoding a mixed array):
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
182 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
183 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
184 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
185 //! d.Parse ("[\"foo\", 123, true]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
186 //! octave_value cell = decode_string_and_mixed_array (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
187 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
188
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
189 static octave_value
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
190 decode_string_and_mixed_array (const rapidjson::Value& val,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
191 const octave::make_valid_name_options *options)
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
192 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
193 Cell retval (dim_vector (val.Size (), 1));
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
194 octave_idx_type index = 0;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
195 for (const auto& elem : val.GetArray ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
196 retval(index++) = decode (elem, options);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
197 return retval;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
198 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
199
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
200 //! Decodes a JSON array that contains only objects into a Cell or struct array
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
201 //! depending on the similarity of the objects' keys.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
202 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
203 //! @param val JSON value that is guaranteed to be an object array.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
204 //! @param options @c ReplacementStyle and @c Prefix options with their values.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
205 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
206 //! @return @ref octave_value that contains the equivalent Cell
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
207 //! or struct array of @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
208 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
209 //! @b Example (returns a struct array):
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
210 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
211 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
212 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
213 //! d.Parse ("[{\"a\":1,\"b\":2},{\"a\":3,\"b\":4}]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
214 //! octave_value object_array = decode_object_array (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
215 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
216 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
217 //! @b Example (returns a Cell):
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
218 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
219 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
220 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
221 //! d.Parse ("[{\"a\":1,\"b\":2},{\"b\":3,\"a\":4}]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
222 //! octave_value object_array = decode_object_array (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
223 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
224
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
225 static octave_value
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
226 decode_object_array (const rapidjson::Value& val,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
227 const octave::make_valid_name_options *options)
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
228 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
229 Cell struct_cell = decode_string_and_mixed_array (val, options).cell_value ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
230 string_vector field_names = struct_cell(0).scalar_map_value ().fieldnames ();
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
231
28625
173807014259 jsondecode.cc: Prefer STL to custom comparison function
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28624
diff changeset
232 bool same_field_names = true;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
233 for (octave_idx_type i = 1; i < struct_cell.numel (); ++i)
28625
173807014259 jsondecode.cc: Prefer STL to custom comparison function
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28624
diff changeset
234 if (field_names.std_list ()
173807014259 jsondecode.cc: Prefer STL to custom comparison function
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28624
diff changeset
235 != struct_cell(i).scalar_map_value ().fieldnames ().std_list ())
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
236 {
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
237 same_field_names = false;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
238 break;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
239 }
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
240
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
241 if (same_field_names)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
242 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
243 octave_map struct_array;
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
244 dim_vector struct_array_dims = dim_vector (struct_cell.numel (), 1);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
245
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
246 if (field_names.numel ())
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
247 {
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
248 Cell value (struct_array_dims);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
249 for (octave_idx_type i = 0; i < field_names.numel (); ++i)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
250 {
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
251 for (octave_idx_type k = 0; k < struct_cell.numel (); ++k)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
252 value(k) = struct_cell(k).scalar_map_value ()
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
253 .getfield (field_names(i));
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
254 struct_array.assign (field_names(i), value);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
255 }
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
256 }
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
257 else
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
258 struct_array.resize (struct_array_dims, true);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
259
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
260 return struct_array;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
261 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
262 else
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
263 return struct_cell;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
264 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
265
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
266 //! Decodes a JSON array that contains only arrays into a Cell or an NDArray
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
267 //! depending on the dimensions and element types of the sub-arrays.
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
268 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
269 //! @param val JSON value that is guaranteed to be an array of arrays.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
270 //! @param options @c ReplacementStyle and @c Prefix options with their values.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
271 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
272 //! @return @ref octave_value that contains the equivalent Cell
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
273 //! or NDArray of @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
274 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
275 //! @b Example (returns an NDArray):
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
276 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
277 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
278 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
279 //! d.Parse ("[[1, 2], [3, 4]]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
280 //! octave_value array = decode_array_of_arrays (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
281 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
282 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
283 //! @b Example (returns a Cell):
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
284 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
285 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
286 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
287 //! d.Parse ("[[1, 2], [3, 4, 5]]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
288 //! octave_value cell = decode_array_of_arrays (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
289 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
290
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
291 static octave_value
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
292 decode_array_of_arrays (const rapidjson::Value& val,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
293 const octave::make_valid_name_options *options)
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
294 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
295 // Some arrays should be decoded as NDArrays and others as cell arrays
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
296 Cell cell = decode_string_and_mixed_array (val, options).cell_value ();
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
297
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
298 // Only arrays with sub-arrays of booleans and numericals will return NDArray
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
299 bool is_bool = cell(0).is_bool_matrix ();
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
300 bool is_struct = cell(0).isstruct ();
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
301 string_vector field_names = is_struct ? cell(0).map_value ().fieldnames ()
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
302 : string_vector ();
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
303 dim_vector sub_array_dims = cell(0).dims ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
304 octave_idx_type sub_array_ndims = cell(0).ndims ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
305 octave_idx_type cell_numel = cell.numel ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
306 for (octave_idx_type i = 0; i < cell_numel; ++i)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
307 {
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
308 // If one element is cell return the cell array as at least one of the
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
309 // sub-arrays area either an array of: strings, objects or mixed array
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
310 if (cell(i).iscell ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
311 return cell;
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
312 // If not the same dim of elements or dim = 0, return cell array
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
313 if (cell(i).dims () != sub_array_dims || sub_array_dims == dim_vector ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
314 return cell;
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
315 // If not numeric sub-arrays only or bool sub-arrays only,
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
316 // return cell array
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
317 if (cell(i).is_bool_matrix () != is_bool)
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
318 return cell;
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
319 // If not struct arrays only, return cell array
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
320 if (cell(i).isstruct () != is_struct)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
321 return cell;
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
322 // If struct arrays have different fields, return cell array
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
323 if (is_struct && (field_names.std_list ()
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
324 != cell(i).map_value ().fieldnames ().std_list ()))
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
325 return cell;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
326 }
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
327
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
328 // Calculate the dims of the output array
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
329 dim_vector array_dims;
28801
ffa6c50c2eaa Decode JSON boolean arrays into logical arrays (bug #59135).
Abdallah-Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28761
diff changeset
330 array_dims.resize (sub_array_ndims + 1);
ffa6c50c2eaa Decode JSON boolean arrays into logical arrays (bug #59135).
Abdallah-Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28761
diff changeset
331 array_dims(0) = cell_numel;
ffa6c50c2eaa Decode JSON boolean arrays into logical arrays (bug #59135).
Abdallah-Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28761
diff changeset
332 for (auto i = 1; i < sub_array_ndims + 1; i++)
ffa6c50c2eaa Decode JSON boolean arrays into logical arrays (bug #59135).
Abdallah-Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28761
diff changeset
333 array_dims(i) = sub_array_dims(i-1);
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
334
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
335 if (is_struct)
29708
fbcc3a680826 Improve performance (> 100X) of jsondecode (bug #60625).
Rik <rik@octave.org>
parents: 29359
diff changeset
336 {
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
337 octave_map struct_array;
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
338 array_dims.chop_trailing_singletons ();
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
339
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
340 if (field_names.numel ())
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
341 {
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
342 Cell value (array_dims);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
343 octave_idx_type sub_array_numel = sub_array_dims.numel ();
28801
ffa6c50c2eaa Decode JSON boolean arrays into logical arrays (bug #59135).
Abdallah-Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28761
diff changeset
344
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
345 for (octave_idx_type j = 0; j < field_names.numel (); ++j)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
346 {
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
347 // Populate the array with specific order to generate
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
348 // MATLAB-identical output.
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
349 for (octave_idx_type k = 0; k < cell_numel; ++k)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
350 {
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
351 Cell sub_array_value = cell(k).map_value ()
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
352 .getfield (field_names(j));
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
353 for (octave_idx_type i = 0; i < sub_array_numel; ++i)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
354 value(k + i * cell_numel) = sub_array_value(i);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
355 }
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
356 struct_array.assign (field_names(j), value);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
357 }
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
358 }
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
359 else
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
360 struct_array.resize(array_dims, true);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
361
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
362 return struct_array;
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
363 }
28801
ffa6c50c2eaa Decode JSON boolean arrays into logical arrays (bug #59135).
Abdallah-Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28761
diff changeset
364 else
29994
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
365 {
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
366 NDArray array (array_dims);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
367
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
368 // Populate the array with specific order to generate MATLAB-identical
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
369 // output.
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
370 octave_idx_type sub_array_numel = array.numel () / cell_numel;
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
371 for (octave_idx_type k = 0; k < cell_numel; ++k)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
372 {
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
373 NDArray sub_array_value = cell(k).array_value ();
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
374 for (octave_idx_type i = 0; i < sub_array_numel; ++i)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
375 array(k + i * cell_numel) = sub_array_value(i);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
376 }
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
377
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
378 if (is_bool)
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
379 return boolNDArray (array);
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
380 else
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
381 return array;
eb768fc5e6b7 jsondecode.cc: Handle array of arrays and arrays of structs correctly (bug #60688)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 29978
diff changeset
382 }
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
383 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
384
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
385 //! Decodes any type of JSON arrays. This function only serves as an interface
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
386 //! by choosing which function to call from the previous functions.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
387 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
388 //! @param val JSON value that is guaranteed to be an array.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
389 //! @param options @c ReplacementStyle and @c Prefix options with their values.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
390 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
391 //! @return @ref octave_value that contains the output of decoding @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
392 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
393 //! @b Example:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
394 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
395 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
396 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
397 //! d.Parse ("[[1, 2], [3, 4, 5]]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
398 //! octave_value array = decode_array (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
399 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
400
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
401 static octave_value
29092
38e22065d9ec jsondecode.cc: use `octave::make_valid_name` not `matlab.lang.makeValidName` (patch #9998)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28801
diff changeset
402 decode_array (const rapidjson::Value& val,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
403 const octave::make_valid_name_options *options)
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
404 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
405 // Handle empty arrays
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
406 if (val.Empty ())
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
407 return NDArray ();
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
408
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
409 // Compare with other elements to know if the array has multiple types
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
410 rapidjson::Type array_type = val[0].GetType ();
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
411 // Check if the array is numeric and if it has multiple types
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
412 bool same_type = true;
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
413 bool is_numeric = true;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
414 for (const auto& elem : val.GetArray ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
415 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
416 rapidjson::Type current_elem_type = elem.GetType ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
417 if (is_numeric && ! (current_elem_type == rapidjson::kNullType
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
418 || current_elem_type == rapidjson::kNumberType))
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
419 is_numeric = false;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
420 if (same_type && (current_elem_type != array_type))
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
421 // RapidJSON doesn't have kBoolean Type it has kTrueType and kFalseType
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
422 if (! ((current_elem_type == rapidjson::kTrueType
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
423 && array_type == rapidjson::kFalseType)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
424 || (current_elem_type == rapidjson::kFalseType
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
425 && array_type == rapidjson::kTrueType)))
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
426 same_type = false;
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
427 }
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
428
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
429 if (is_numeric)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
430 return decode_numeric_array (val);
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
431
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
432 if (same_type && (array_type != rapidjson::kStringType))
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
433 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
434 if (array_type == rapidjson::kTrueType
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
435 || array_type == rapidjson::kFalseType)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
436 return decode_boolean_array (val);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
437 else if (array_type == rapidjson::kObjectType)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
438 return decode_object_array (val, options);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
439 else if (array_type == rapidjson::kArrayType)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
440 return decode_array_of_arrays (val, options);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
441 else
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
442 error ("jsondecode: unidentified type");
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
443 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
444 else
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
445 return decode_string_and_mixed_array (val, options);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
446 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
447
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
448 //! Decodes any JSON value. This function only serves as an interface
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
449 //! by choosing which function to call from the previous functions.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
450 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
451 //! @param val JSON value.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
452 //! @param options @c ReplacementStyle and @c Prefix options with their values.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
453 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
454 //! @return @ref octave_value that contains the output of decoding @p val.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
455 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
456 //! @b Example:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
457 //!
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
458 //! @code{.cc}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
459 //! rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
460 //! d.Parse ("[{\"a\":1,\"b\":2},{\"b\":3,\"a\":4}]");
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
461 //! octave_value value = decode (d, octave_value_list ());
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
462 //! @endcode
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
463
29995
4628ae890642 move jsondecode functions to octave namespace and make local functions static
John W. Eaton <jwe@octave.org>
parents: 29994
diff changeset
464 static octave_value
29092
38e22065d9ec jsondecode.cc: use `octave::make_valid_name` not `matlab.lang.makeValidName` (patch #9998)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28801
diff changeset
465 decode (const rapidjson::Value& val,
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
466 const octave::make_valid_name_options *options)
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
467 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
468 if (val.IsBool ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
469 return val.GetBool ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
470 else if (val.IsNumber ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
471 return decode_number (val);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
472 else if (val.IsString ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
473 return val.GetString ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
474 else if (val.IsObject ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
475 return decode_object (val, options);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
476 else if (val.IsNull ())
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
477 return NDArray ();
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
478 else if (val.IsArray ())
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
479 return decode_array (val, options);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
480 else
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
481 error ("jsondecode: unidentified type");
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
482 }
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
483
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
484 #endif
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
485
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
486 DEFUN (jsondecode, args, ,
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
487 doc: /* -*- texinfo -*-
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
488 @deftypefn {} {@var{object} =} jsondecode (@var{JSON_txt})
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
489 @deftypefnx {} {@var{object} =} jsondecode (@dots{}, "ReplacementStyle", @var{rs})
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
490 @deftypefnx {} {@var{object} =} jsondecode (@dots{}, "Prefix", @var{pfx})
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
491 @deftypefnx {} {@var{object} =} jsondecode (@dots{}, "makeValidName", @var{TF})
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
492
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
493 Decode text that is formatted in JSON.
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
494
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
495 The input @var{JSON_txt} is a string that contains JSON text.
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
496
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
497 The output @var{object} is an Octave object that contains the result of
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
498 decoding @var{JSON_txt}.
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
499
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
500 For more information about the options @qcode{"ReplacementStyle"} and
30377
26f2daafa270 doc: Use consistent styling for @ref, @xref, @pxref statements.
Rik <rik@octave.org>
parents: 30355
diff changeset
501 @qcode{"Prefix"},
26f2daafa270 doc: Use consistent styling for @ref, @xref, @pxref statements.
Rik <rik@octave.org>
parents: 30355
diff changeset
502 @pxref{XREFmatlab_lang_makeValidName,,@code{matlab.lang.makeValidName}}.
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
503
30354
c980f938fdd8 doc: grammarcheck C++ files in libinterp/ directory ahead of 7.1 release.
Rik <rik@octave.org>
parents: 29995
diff changeset
504 If the value of the option @qcode{"makeValidName"} is false then names
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
505 will not be changed by @code{matlab.lang.makeValidName} and the
30355
6a6efd7bb3ec doc: grammarcheck texi files of Octave manual ahead of 7.1 release
Rik <rik@octave.org>
parents: 30354
diff changeset
506 @qcode{"ReplacementStyle"} and @qcode{"Prefix"} options will be ignored.
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
507
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
508 NOTE: Decoding and encoding JSON text is not guaranteed to reproduce the
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
509 original text as some names may be changed by @code{matlab.lang.makeValidName}.
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
510
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
511 This table shows the conversions from JSON data types to Octave data types:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
512
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
513 @multitable @columnfractions 0.50 0.50
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
514 @headitem JSON data type @tab Octave data type
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
515 @item Boolean @tab scalar logical
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
516 @item Number @tab scalar double
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
517 @item String @tab vector of characters
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
518 @item Object @tab scalar struct (field names of the struct may be different from the keys of the JSON object due to @code{matlab_lang_makeValidName}
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
519 @item null, inside a numeric array @tab @code{NaN}
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
520 @item null, inside a non-numeric array @tab empty double array @code{[]}
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
521 @item Array, of different data types @tab cell array
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
522 @item Array, of Booleans @tab logical array
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
523 @item Array, of Numbers @tab double array
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
524 @item Array, of Strings @tab cell array of character vectors (@code{cellstr})
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
525 @item Array of Objects, same field names @tab struct array
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
526 @item Array of Objects, different field names @tab cell array of scalar structs
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
527 @end multitable
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
528
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
529 Examples:
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
530
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
531 @example
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
532 @group
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
533 jsondecode ('[1, 2, null, 3]')
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
534 @result{} ans =
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
535
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
536 1
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
537 2
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
538 NaN
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
539 3
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
540 @end group
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
541
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
542 @group
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
543 jsondecode ('["foo", "bar", ["foo", "bar"]]')
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
544 @result{} ans =
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
545 @{
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
546 [1,1] = foo
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
547 [2,1] = bar
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
548 [3,1] =
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
549 @{
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
550 [1,1] = foo
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
551 [2,1] = bar
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
552 @}
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
553
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
554 @}
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
555 @end group
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
556
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
557 @group
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
558 jsondecode ('@{"nu#m#ber": 7, "s#tr#ing": "hi"@}', ...
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
559 'ReplacementStyle', 'delete')
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
560 @result{} scalar structure containing the fields:
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
561
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
562 number = 7
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
563 string = hi
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
564 @end group
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
565
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
566 @group
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
567 jsondecode ('@{"nu#m#ber": 7, "s#tr#ing": "hi"@}', ...
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
568 'makeValidName', false)
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
569 @result{} scalar structure containing the fields:
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
570
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
571 nu#m#ber = 7
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
572 s#tr#ing = hi
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
573 @end group
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
574
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
575 @group
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
576 jsondecode ('@{"1": "one", "2": "two"@}', 'Prefix', 'm_')
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
577 @result{} scalar structure containing the fields:
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
578
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
579 m_1 = one
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
580 m_2 = two
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
581 @end group
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
582 @end example
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
583
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
584 @seealso{jsonencode, matlab.lang.makeValidName}
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
585 @end deftypefn */)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
586 {
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
587 #if defined (HAVE_RAPIDJSON)
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
588
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
589 int nargin = args.length ();
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
590
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
591 // makeValidName options are pairs, the number of arguments must be odd.
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
592 if (! (nargin % 2))
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
593 print_usage ();
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
594
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
595 // Detect if the user wants to use makeValidName
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
596 bool use_makeValidName = true;
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
597 octave_value_list make_valid_name_params;
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
598 for (auto i = 1; i < nargin; i = i + 2)
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
599 {
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
600 std::string parameter = args(i).xstring_value ("jsondecode: "
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
601 "option argument must be a string");
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
602 if (string::strcmpi (parameter, "makeValidName"))
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
603 {
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
604 use_makeValidName = args(i + 1).xbool_value ("jsondecode: "
30390
a61e1a0f6024 maint: style check C++ files in libinterp/ ahead of 7.1 release.
Rik <rik@octave.org>
parents: 30377
diff changeset
605 "'makeValidName' value must be a bool");
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
606 }
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
607 else
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
608 make_valid_name_params.append (args.slice(i, 2));
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
609 }
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
610
29978
c7c69808356f jsondecode.cc: Avoid dereferencing invalid pointer.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29977
diff changeset
611 make_valid_name_options *options
c7c69808356f jsondecode.cc: Avoid dereferencing invalid pointer.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29977
diff changeset
612 = use_makeValidName ? new make_valid_name_options (make_valid_name_params)
c7c69808356f jsondecode.cc: Avoid dereferencing invalid pointer.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29977
diff changeset
613 : nullptr;
29977
0d67814eb038 jsondecode.cc: New option "makeValidName" to disable string validation.
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 29961
diff changeset
614
29978
c7c69808356f jsondecode.cc: Avoid dereferencing invalid pointer.
Markus Mützel <markus.muetzel@gmx.de>
parents: 29977
diff changeset
615 unwind_action del_opts ([options] (void) { if (options) delete options; });
29092
38e22065d9ec jsondecode.cc: use `octave::make_valid_name` not `matlab.lang.makeValidName` (patch #9998)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28801
diff changeset
616
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
617 if (! args(0).is_string ())
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
618 error ("jsondecode: JSON_TXT must be a character string");
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
619
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
620 std::string json = args(0).string_value ();
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
621 rapidjson::Document d;
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
622 // DOM is chosen instead of SAX as SAX publishes events to a handler that
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
623 // decides what to do depending on the event only. This will cause a
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
624 // problem in decoding JSON arrays as the output may be an array or a cell
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
625 // and that doesn't only depend on the event (startArray) but also on the
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
626 // types of the elements inside the array.
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
627 d.Parse <rapidjson::kParseNanAndInfFlag> (json.c_str ());
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
628
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
629 if (d.HasParseError ())
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
630 error ("jsondecode: parse error at offset %u: %s\n",
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
631 static_cast<unsigned int> (d.GetErrorOffset ()) + 1,
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
632 rapidjson::GetParseError_En (d.GetParseError ()));
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
633
29092
38e22065d9ec jsondecode.cc: use `octave::make_valid_name` not `matlab.lang.makeValidName` (patch #9998)
Kai T. Ohlhus <k.ohlhus@gmail.com>
parents: 28801
diff changeset
634 return decode (d, options);
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
635
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
636 #else
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
637
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
638 octave_unused_parameter (args);
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
639
28624
aae9d7f098bd Support for RapidJSON 1.1.0 with jsonencode and jsondecode
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents: 28615
diff changeset
640 err_disabled_feature ("jsondecode", "JSON decoding through RapidJSON");
28615
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
641
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
642 #endif
5da49e37a6c9 New functions jsondecode and jsonencode (bug #53100).
Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
parents:
diff changeset
643 }
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
644
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
645 /*
28761
b5461b1dc0ca Review jsonecode/jsondecode BIST tests.
Rik <rik@octave.org>
parents: 28750
diff changeset
646 Functional BIST tests are located in test/json/jsondecode_BIST.tst
28750
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
647
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
648 ## Input validation tests
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
649 %!testif HAVE_RAPIDJSON
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
650 %! fail ("jsondecode ()");
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
651 %! fail ("jsondecode ('1', 2)");
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
652 %! fail ("jsondecode (1)", "JSON_TXT must be a character string");
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
653 %! fail ("jsondecode ('12-')", "parse error at offset 3");
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
654
80857685105b Code review for jsonencode/jsondecode functions.
Rik <rik@octave.org>
parents: 28626
diff changeset
655 */
29958
32c3a5805893 move DEFUN and DEFMETHOD functions inside octave namespace
John W. Eaton <jwe@octave.org>
parents: 29708
diff changeset
656
31605
e88a07dec498 maint: Use macros to begin/end C++ namespaces.
Rik <rik@octave.org>
parents: 30564
diff changeset
657 OCTAVE_END_NAMESPACE(octave)