changeset 28657:174550af014f

document and test escape characters in jsonencode * libinterp/corefcn/jsonencode.cc: Add examples and note to documentation. * test/json/jsonencodetest.tst: New test.
author Abdallah Elshamy <abdallah.k.elshamy@gmail.com>
date Fri, 28 Aug 2020 13:49:14 +0900
parents 6dc23a4126b9
children 6dbd32dd2a5f
files libinterp/corefcn/jsonencode.cc test/json/jsonencodetest.tst
diffstat 2 files changed, 21 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/jsonencode.cc	Thu Aug 27 01:21:23 2020 -0400
+++ b/libinterp/corefcn/jsonencode.cc	Fri Aug 28 13:49:14 2020 +0900
@@ -458,6 +458,10 @@
 To preserve the escape characters (e.g. "\n"), use double-quote strings.
 
 @item
+Every character after the null character ("\0") in double-quoted strings will
+be dropped during encoding.
+
+@item
 It is not guaranteed to get the same dimensions for arrays if you encode
 and then decode it.  For example, if you encoded a row vector then decoded it,
 you will get a column vector.
@@ -538,6 +542,18 @@
 @end group
 
 @group
+## Escape characters inside a single-quoted string
+jsonencode ('\0\a\b\t\n\v\f\r')
+@result{} "\\0\\a\\b\\t\\n\\v\\f\\r"
+@end group
+
+@group
+## Escape characters inside a double-quoted string
+jsonencode ("\a\b\t\n\v\f\r")
+@result{} "\u0007\b\t\n\u000B\f\r"
+@end group
+
+@group
 jsonencode ([true; false], "ConvertInfAndNaN", false, "PrettyWriter", true)
 @result{} ans = [
        true,
--- a/test/json/jsonencodetest.tst	Thu Aug 27 01:21:23 2020 -0400
+++ b/test/json/jsonencodetest.tst	Fri Aug 28 13:49:14 2020 +0900
@@ -30,6 +30,11 @@
 %! assert (isequal (jsonencode (['foo'; 'bar']), '["foo","bar"]'));
 %! assert (isequal (jsonencode (['foo', 'bar'; 'foo', 'bar']), '["foobar","foobar"]'));
 
+% escape characters inside single-quoted and double-quoted strings
+%!testif HAVE_RAPIDJSON
+%! assert (isequal (jsonencode ('\0\a\b\t\n\v\f\r'), '"\\0\\a\\b\\t\\n\\v\\f\\r"'));
+%! assert (isequal (jsonencode ("\a\b\t\n\v\f\r"), '"\u0007\b\t\n\u000B\f\r"'));
+
 %!testif HAVE_RAPIDJSON
 %! data = [[['foo'; 'bar']; ['foo'; 'bar']], [['foo'; 'bar']; ['foo'; 'bar']]];
 %! exp  = '["foofoo","barbar","foofoo","barbar"]';