diff libinterp/corefcn/jsondecode.cc @ 28624:aae9d7f098bd

Support for RapidJSON 1.1.0 with jsonencode and jsondecode * configure.ac: Define HAVE_RAPIDJSON for RapidJSON 1.1.0 and HAVE_RAPIDJSON_DEV for the development or newer release in the future containing the `rapidjson/cursorstreamwrapper.h` header file. * libinterp/corefcn/jsondecode.cc: Improve docstrings and comments. * libinterp/corefcn/jsonencode.cc (Fjsonencode): Enable the 'PrettyWriter' option only if HAVE_RAPIDJSON_DEV is set to "1". Sort header includes. Improve docstrings and comments. This is the result of GSoC 2020 by Abdallah Elshamy and an addition to cset 5da49e37a6c9.
author Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
date Tue, 18 Aug 2020 14:44:22 +0900
parents 5da49e37a6c9
children 173807014259
line wrap: on
line diff
--- a/libinterp/corefcn/jsondecode.cc	Sat Aug 15 23:41:26 2020 -0400
+++ b/libinterp/corefcn/jsondecode.cc	Tue Aug 18 14:44:22 2020 +0900
@@ -499,36 +499,43 @@
 @example
 @group
 jsondecode ('[1, 2, null, 3]')
-    @result{} 1  2  NaN  3
+    @result{} ans =
+
+      1
+      2
+    NaN
+      3
 @end group
 
 @group
 jsondecode ('["foo", "bar", ["foo", "bar"]]')
     @result{} ans =
-                {
-                  [1,1] = foo
-                  [2,1] = bar
-                  [3,1] =
-                  {
-                    [1,1] = foo
-                    [2,1] = bar
-                  }
+       @{
+         [1,1] = foo
+         [2,1] = bar
+         [3,1] =
+         @{
+           [1,1] = foo
+           [2,1] = bar
+         @}
 
-                }
+       @}
 @end group
 
 @group
-jsondecode ('{"nu#m#ber": 7, "s#tr#ing": "hi"}', 'ReplacementStyle', 'delete')
+jsondecode ('@{"nu#m#ber": 7, "s#tr#ing": "hi"@}', 'ReplacementStyle', 'delete')
     @result{} scalar structure containing the fields:
-                number =  7
-                string = hi
+
+         number = 7
+         string = hi
 @end group
 
 @group
-jsondecode ('{"1": "one", "2": "two"}', 'Prefix', 'm_')
+jsondecode ('@{"1": "one", "2": "two"@}', 'Prefix', 'm_')
     @result{} scalar structure containing the fields:
-                m_1 = one
-                m_2 = two
+
+         m_1 = one
+         m_2 = two
 @end group
 @end example
 
@@ -538,8 +545,7 @@
 #if defined (HAVE_RAPIDJSON)
 
   int nargin = args.length ();
-  // makeValidName options must be in pairs
-  // The number of arguments must be odd
+  // makeValidName options are pairs, the number of arguments must be odd.
   if (! (nargin % 2))
     print_usage ();
 
@@ -549,24 +555,24 @@
   std::string json = args (0).string_value ();
   rapidjson::Document d;
   // DOM is chosen instead of SAX as SAX publishes events to a handler that
-  // decides what to do depending on the event only. This will cause a problem
-  // in decoding JSON arrays as the output may be an array or a cell and that
-  // doesn't only depend on the event (startArray) but also on the types of
-  // the elements inside the array
+  // decides what to do depending on the event only.  This will cause a
+  // problem in decoding JSON arrays as the output may be an array or a cell
+  // and that doesn't only depend on the event (startArray) but also on the
+  // types of the elements inside the array.
   d.Parse <rapidjson::kParseNanAndInfFlag>(json.c_str ());
 
   if (d.HasParseError ())
     error("jsondecode: Parse error at offset %u: %s\n",
-          d.GetErrorOffset (),
+          static_cast<unsigned int> (d.GetErrorOffset ()),
           rapidjson::GetParseError_En (d.GetParseError ()));
-  return decode (d, args.slice (1, nargin-1));
+
+  return decode (d, args.slice (1, nargin - 1));
 
 #else
 
   octave_unused_parameter (args);
 
-  err_disabled_feature ("jsondecode",
-                        "RapidJSON is required for JSON encoding\\decoding");
+  err_disabled_feature ("jsondecode", "JSON decoding through RapidJSON");
 
 #endif
 }