Mon 22 Jul 2013 04:17:53 AM UTC, comment #4:
OK I've added a couple of tests and checked them for validity with jsonlint.com. I would have run more test cases but it's pretty simple so I'm not sure what else would need to be tested.
<pre>
diff --git a/object2json.m b/object2json.m
index f0bdb87..c44c2c7 100644
--- a/object2json.m
+++ b/object2json.m
@@ -98,6 +98,13 @@ function json = object2json (object)
object = replace_non_JSON_escapes (object);
json = [ '"', object, '"' ];
+ case 'logical'
+ if object
+ json = 'true';
+ else
+ json = 'false';
+ endif
+
otherwise
% We don't know what is it so we'll put the class name
json = [ '"', class(object), '"' ];
@@ -187,3 +194,15 @@ function object = replace_non_JSON_escapes (object)
object = regexprep (object, '(?<!\\)\\(?=(\\\\)*(?!([\"\\\/bfnrt]|([u][0-9A-Fa-f]{4}))+?))', "\\\\");
endfunction
+
+%!test
+%! assert(object2json([logical(1), logical(0)]), '[true,false]');
+
+%!test
+%! car.name = 'Mzd R8';
+%! car.speedsamples = [98, 33, 50; 56, 120, 102; 77, 82, 93];
+%! car.toofast = car.speedsamples >= 90;
+%! car.leased = logical(1);
+%! car.european = logical(0);
+%! assert(object2json(car), '{"name":"Mzd R8","speedsamples":[[98,33,50],[56,120,102],[77,82,93]],"toofast":[[true,false,false],[false,true,true],[false,false,true]],"le
+
</pre>
|
Mon 08 Jul 2013 05:24:29 PM UTC, comment #3:
Just a script (or a transcript from Octave's terminal) would do - I'll add it to the object2json.m file (after consulting you if tit works OK).
OTOH, if you want, you can have a look at how tests are implemented in e.g., Octave's own .m files. Maybe you like it.
Just do e.g., "edit strread.m" and scroll to the bottom - you'll see a mix of examples.
To try tests, just do "test <functionname>.m"
|