26 #if defined (HAVE_CONFIG_H)
30 #include "builtin-defun-decls.h"
37 #if defined (HAVE_RAPIDJSON)
38 # include <rapidjson/stringbuffer.h>
39 # include <rapidjson/writer.h>
40 # if defined (HAVE_RAPIDJSON_PRETTYWRITER)
41 # include <rapidjson/prettywriter.h>
45 #if defined (HAVE_RAPIDJSON)
60 template <
typename T>
void
62 const bool& ConvertInfAndNaN)
72 else if (fabs (
floor (value) - value) < std::numeric_limits<double>::epsilon ()
73 && value <= 999999 && value >= -999999)
79 writer.Double (value);
81 error (
"jsonencode: unsupported type");
98 template <
typename T>
void
100 const dim_vector& original_dims,
int level = 0)
113 std::string char_vector =
"";
115 char_vector += array(i);
116 writer.String (char_vector.c_str ());
121 std::string char_vector =
"";
123 char_vector += array(i * original_dims(1) + k);
124 writer.String (char_vector.c_str ());
143 for (
int i = level; i < ndims - 1; ++i)
144 writer.StartArray ();
146 encode_string (writer, array.
as_row (), original_dims, level);
149 for (
int i = level; i < ndims - 1; ++i)
157 if (original_dims(level) == 1 && level != 1)
159 writer.StartArray ();
160 encode_string (writer, array, original_dims, level + 1);
170 for (idx = 0; idx < ndims; ++idx)
179 conversion_dims.
resize (ndims - 1);
181 conversion_dims(i) = i + 1;
183 conversion_dims(i) = i + 2;
186 args.append (conversion_dims);
190 writer.StartArray ();
193 encode_string (writer, sub_arrays(i), original_dims,
216 template <
typename T>
void
217 encode_struct (T& writer,
const octave_value& obj,
const bool& ConvertInfAndNaN)
221 bool is_array = (
numel != 1);
225 writer.StartArray ();
229 writer.StartObject ();
232 writer.Key (keys(k).c_str ());
233 encode (writer, struct_array(i).getfield (keys(k)), ConvertInfAndNaN);
255 template <
typename T>
void
256 encode_cell (T& writer,
const octave_value& obj,
const bool& ConvertInfAndNaN)
260 writer.StartArray ();
263 encode (writer, cell(i), ConvertInfAndNaN);
284 template <
typename T>
void
285 encode_array (T& writer,
const octave_value& obj,
const bool& ConvertInfAndNaN,
286 const dim_vector& original_dims,
int level = 0,
287 bool is_logical =
false)
297 writer.StartArray ();
302 writer.StartArray ();
306 encode_numeric (writer,
bool (array(i)), ConvertInfAndNaN);
308 encode_numeric (writer, array(i), ConvertInfAndNaN);
328 for (
int i = level; i < ndims - 1; ++i)
329 writer.StartArray ();
331 encode_array (writer, array.
as_row (), ConvertInfAndNaN,
332 original_dims, level + 1, is_logical);
335 for (
int i = level; i < ndims - 1; ++i)
343 if (original_dims (level) == 1)
345 writer.StartArray ();
346 encode_array (writer, array, ConvertInfAndNaN,
347 original_dims, level + 1, is_logical);
352 for (idx = 0; idx < ndims; ++idx)
362 conversion_dims.
resize (ndims - 1);
364 conversion_dims(i) = i + 1;
366 conversion_dims(i) = i + 2;
369 args.append (conversion_dims);
373 writer.StartArray ();
376 encode_array (writer, sub_arrays(i), ConvertInfAndNaN,
377 original_dims, level + 1, is_logical);
399 template <
typename T>
void
400 encode (T& writer,
const octave_value& obj,
const bool& ConvertInfAndNaN)
403 encode_numeric (writer, obj, ConvertInfAndNaN);
406 encode_array (writer, obj, ConvertInfAndNaN, obj.
dims ());
408 encode_string (writer, obj, obj.
dims ());
410 encode_struct (writer, obj, ConvertInfAndNaN);
412 encode_cell (writer, obj, ConvertInfAndNaN);
413 else if (obj.
class_name () ==
"containers.Map")
420 octave::unwind_action restore_warning_state
431 octave::unwind_action restore_warning_state
440 error (
"jsonencode: unsupported type");
447 DEFUN (jsonencode, args, ,
586 #if defined (HAVE_RAPIDJSON)
588 int nargin = args.length ();
590 if (nargin != 1 && nargin != 3 && nargin != 5)
594 bool ConvertInfAndNaN =
true;
595 bool PrettyPrint =
false;
599 if (! args(i).is_string ())
600 error (
"jsonencode: option must be a string");
601 if (! args(i+1).is_bool_scalar ())
602 error (
"jsonencode: option value must be a logical scalar");
604 std::string option_name = args(i++).string_value ();
606 ConvertInfAndNaN = args(i).bool_value ();
608 PrettyPrint = args(i).bool_value ();
610 error (
"jsonencode: "
611 R
"(Valid options are "ConvertInfAndNaN" and "PrettyPrint")");
614 # if ! defined (HAVE_RAPIDJSON_PRETTYWRITER)
618 R
"(the "PrettyPrint" option of RapidJSON)");
623 rapidjson::StringBuffer json;
626 # if defined (HAVE_RAPIDJSON_PRETTYWRITER)
627 rapidjson::PrettyWriter<rapidjson::StringBuffer, rapidjson::UTF8<>,
628 rapidjson::UTF8<>, rapidjson::CrtAllocator,
629 rapidjson::kWriteNanAndInfFlag> writer (json);
630 writer.SetIndent (
' ', 2);
631 encode (writer, args(0), ConvertInfAndNaN);
636 rapidjson::Writer<rapidjson::StringBuffer, rapidjson::UTF8<>,
637 rapidjson::UTF8<>, rapidjson::CrtAllocator,
638 rapidjson::kWriteNanAndInfFlag> writer (json);
639 encode (writer, args(0), ConvertInfAndNaN);
646 octave_unused_parameter (args);
OCTAVE_EXPORT octave_value_list Fnum2cell(const octave_value_list &args, int)
OCTARRAY_OVERRIDABLE_FUNC_API Array< T, Alloc > as_row(void) const
Return the array as a row vector.
OCTARRAY_OVERRIDABLE_FUNC_API bool isempty(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API bool isvector(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
OCTARRAY_OVERRIDABLE_FUNC_API int ndims(void) const
Size of the specified dimension.
void resize(octave_idx_type n, const double &rfv=0)
Vector representing the dimensions (size) of an Array.
OCTAVE_API int num_ones(void) const
octave_idx_type numel(void) const
string_vector keys(void) const
octave_value getfield(const std::string &key) const
bool bool_value(bool warn=false) const
bool is_bool_scalar(void) const
OCTINTERP_API octave_scalar_map scalar_map_value(void) const
bool isnumeric(void) const
bool is_string(void) const
charNDArray char_array_value(bool frc_str_conv=false) const
bool is_double_type(void) const
Cell cell_value(void) const
std::string class_name(void) const
bool isstruct(void) const
double scalar_value(bool frc_str_conv=false) const
NDArray array_value(bool frc_str_conv=false) const
OCTINTERP_API octave_map map_value(void) const
bool isobject(void) const
bool is_real_scalar(void) const
bool islogical(void) const
dim_vector dims(void) const
octave_idx_type numel(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
OCTINTERP_API void print_usage(void)
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
void error(const char *fmt,...)
octave_value_list set_warning_state(const std::string &id, const std::string &state)
void err_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
void warn_disabled_feature(const std::string &fcn, const std::string &feature, const std::string &pkg)
std::complex< T > floor(const std::complex< T > &x)
T::size_type numel(const T &str)
OCTAVE_API bool strcmpi(const T &str_a, const T &str_b)
True if strings are the same, ignoring case.
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))