Mercurial > octave
diff libinterp/corefcn/jsonencode.cc @ 28853:97d8ef9b7348
From e2bcb18a88f3075348dd53b5768110d621d8455d Mon Sep 17 00:00:00 2001
Encode N-dimensional logical arrays into JSON boolean arrays (bug #59198).
* jsonencode.cc (encode_array): add another optional parameter that indicates
if the array is logical and encode the array accordingly.
author | Abdallah-Elshamy <abdallah.k.elshamy@gmail.com> |
---|---|
date | Thu, 01 Oct 2020 23:26:28 +0200 |
parents | 445e5ac1f58d |
children | 10a35049bad7 |
line wrap: on
line diff
--- a/libinterp/corefcn/jsonencode.cc Fri Oct 02 23:20:11 2020 +0200 +++ b/libinterp/corefcn/jsonencode.cc Thu Oct 01 23:26:28 2020 +0200 @@ -272,6 +272,7 @@ //! @param ConvertInfAndNaN @c bool that converts @c Inf and @c NaN to @c null. //! @param original_dims The original dimensions of the array being encoded. //! @param level The level of recursion for the function. +//! @param is_logical optional @c bool that indicates if the array is logical. //! //! @b Example: //! @@ -282,9 +283,15 @@ template <typename T> void encode_array (T& writer, const octave_value& obj, const bool& ConvertInfAndNaN, - const dim_vector& original_dims, int level = 0) + const dim_vector& original_dims, int level = 0, + bool is_logical = false) { NDArray array = obj.array_value (); + // is_logical is assigned at level 0. I think this is better than changing + // many places in the code, and it makes the function more modular. + if (level == 0) + is_logical = obj.islogical (); + if (array.isempty ()) { writer.StartArray (); @@ -295,7 +302,7 @@ writer.StartArray (); for (octave_idx_type i = 0; i < array.numel (); ++i) { - if (obj.islogical ()) + if (is_logical) encode_numeric (writer, bool (array(i)), ConvertInfAndNaN); else encode_numeric (writer, array(i), ConvertInfAndNaN); @@ -322,7 +329,7 @@ writer.StartArray (); encode_array (writer, array.as_row (), ConvertInfAndNaN, - original_dims); + original_dims, level + 1, is_logical); if (level != 0) for (int i = level; i < ndims - 1; ++i) @@ -337,7 +344,7 @@ { writer.StartArray (); encode_array (writer, array, ConvertInfAndNaN, - original_dims, level + 1); + original_dims, level + 1, is_logical); writer.EndArray (); } else @@ -367,7 +374,7 @@ for (octave_idx_type i = 0; i < sub_arrays.numel (); ++i) encode_array (writer, sub_arrays(i), ConvertInfAndNaN, - original_dims, level + 1); + original_dims, level + 1, is_logical); writer.EndArray (); }