Commit b4899c940cb2849b1747978bfceeb07e1f91fa23

Baptiste 2024-08-12T19:12:23

json handling: first draft

diff --git a/json/json.c b/json/json.c
new file mode 100644
index 0000000..cdb595a
--- /dev/null
+++ b/json/json.c
@@ -0,0 +1,37 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
+#include <libkc3/kc3.h>
+#include "json.h"
+#include "buf.h"
+#include "buf_parse.h"
+#include "tag.h"
+
+static bool parse_json(const s_buf *buf, s_tag *dest) {
+  assert(buf);
+  assert(dest);
+  if (buf_parse_tag_number(buf, dest) == 0)
+    return false;
+  return true;
+}
+
+s_tag *json_parse(const s_buf *buf, s_tag *dest) {
+  assert(buf);
+  assert(dest);
+  s_buf_save save;
+  buf_save_init(buf, &save);
+  if (!parse_json(buf, dest)) {
+    buf_restore_rpos(buf, &save);
+    return NULL;
+  }
+  return dest;
+}
\ No newline at end of file
diff --git a/json/json.h b/json/json.h
new file mode 100644
index 0000000..6e6a106
--- /dev/null
+++ b/json/json.h
@@ -0,0 +1,12 @@
+/* kc3
+ * Copyright 2022,2023,2024 kmx.io <contact@kmx.io>
+ *
+ * Permission is hereby granted to use this software granted the above
+ * copyright notice and this permission paragraph are included in all
+ * copies and substantial portions of this software.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
+ * PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
+ * AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
+ * THIS SOFTWARE.
+ */
diff --git a/lib/kc3/0.1/json/parser.kc3 b/lib/kc3/0.1/json/parser.kc3
new file mode 100644
index 0000000..c7331f6
--- /dev/null
+++ b/lib/kc3/0.1/json/parser.kc3
@@ -0,0 +1,7 @@
+defmodule JSON.Parser do
+
+  dlopen(__DIR__ + "../json.so")
+
+  def parse = cfn Tag "json_parse" (Buf, Result)
+
+end
diff --git a/test/json/basics.kc3 b/test/json/basics.kc3
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/json/basics.kc3