Commit d4131904d64a11400d766ec5c83c0b6b2fd02599

Thomas de Grivel 2024-08-14T22:25:04

wip json_to_str

diff --git a/json/json.c b/json/json.c
index b01f124..26c35ae 100644
--- a/json/json.c
+++ b/json/json.c
@@ -39,7 +39,9 @@ sw json_buf_inspect (s_buf *buf, const s_tag *tag)
   case TAG_UW:
     return json_buf_inspect_tag_number(buf, tag);
   case TAG_BOOL:
-    return json_buf_inspect_bool(buf, tag->data.bool);
+    return buf_inspect_bool(buf, &tag->data.bool);
+  case TAG_VOID:
+    return json_buf_inspect_void(buf);
   default:
     break;
   }
@@ -193,7 +195,9 @@ sw json_buf_inspect_size (s_pretty *pretty, const s_tag *tag)
   case TAG_UW:
     return json_buf_inspect_tag_number_size(pretty, tag);
   case TAG_BOOL:
-    return json_buf_inspect_bool_size(pretty, tag->data.bool);
+    return buf_inspect_bool_size(pretty, &tag->data.bool);
+  case TAG_VOID:
+    return json_buf_inspect_void_size(pretty);
   default:
     break;
   }
@@ -245,6 +249,18 @@ sw json_buf_inspect_tag_number_size (s_pretty *pretty, const s_tag *tag)
   return r;
 }
 
+sw json_buf_inspect_void (s_buf *buf)
+{
+  assert(pretty);
+  return buf_write_1(buf, "null");
+}
+
+sw json_buf_inspect_void_size (s_pretty *pretty)
+{
+  assert(pretty);
+  return buf_write_1_size(pretty, "null");
+}
+
 s_tag * json_buf_parse (s_buf *buf, s_tag *dest)
 {
   character c;
diff --git a/json/json.h b/json/json.h
index 6b1e35c..07fa535 100644
--- a/json/json.h
+++ b/json/json.h
@@ -24,6 +24,8 @@ sw      json_buf_inspect_size (s_pretty *pretty, const s_tag *tag);
 sw      json_buf_inspect_tag_number (s_buf *buf, const s_tag *tag);
 sw      json_buf_inspect_tag_number_size (s_pretty *pretty,
                                           const s_tag *tag);
+sw      json_buf_inspect_void (s_buf *buf);
+sw      json_buf_inspect_void_size (s_pretty *pretty);
 s_tag * json_buf_parse (s_buf *buf, s_tag *dest);
 s_tag * json_buf_parse_bool (s_buf *buf, s_tag *dest);
 s_tag * json_buf_parse_map (s_buf *buf, s_tag *dest);
diff --git a/test/json/to_str.kc3 b/test/json/to_str.kc3
index 8f52f73..5d4b834 100644
--- a/test/json/to_str.kc3
+++ b/test/json/to_str.kc3
@@ -1,6 +1,14 @@
 quote JSON.to_str(%{})
 JSON.to_str(%{})
+quote JSON.to_str(%{"a" => true})
+JSON.to_str(%{"a" => true})
+quote JSON.to_str(%{"a" => false})
+JSON.to_str(%{"a" => false})
+quote JSON.to_str(%{"a" => void})
+JSON.to_str(%{"a" => void})
 quote JSON.to_str(%{"a" => "b"})
 JSON.to_str(%{"a" => "b"})
 quote JSON.to_str(%{"a" => 1})
 JSON.to_str(%{"a" => 1})
+quote JSON.to_str(%{"a" => %{"b" => %{"c" => 1}}})
+JSON.to_str(%{"a" => %{"b" => %{"c" => 1}}})
diff --git a/test/json/to_str.out.expected b/test/json/to_str.out.expected
index edaa6ca..2f95c0d 100644
--- a/test/json/to_str.out.expected
+++ b/test/json/to_str.out.expected
@@ -1,6 +1,14 @@
 JSON.to_str(%{})
 "{}"
+JSON.to_str(%{"a" => true})
+"{\"a\": true}"
+JSON.to_str(%{"a" => false})
+"{\"a\": false}"
+JSON.to_str(%{"a" => void})
+"{\"a\": null}"
 JSON.to_str(%{"a" => "b"})
 "{\"a\": \"b\"}"
 JSON.to_str(%{"a" => 1})
 "{\"a\": 1}"
+JSON.to_str(%{"a" => %{"b" => %{"c" => 1}}})
+"{\"a\": {\"b\": {\"c\": 1}}}"