From 830c1364e54803a2c82c1331bc6fd6fcb59220be Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 30 May 2021 13:12:59 -0700 Subject: json: convenience I/O routines. * lisplib.c (getput_set_entries): Register autoload for get-jsons, put-jsons, file-get-json, file-put-json, file-append-json, file-get-jsons, file-put-jsons, file-append-jsons, command-get-json, command-put-json, command-get-jsons and command-put-jsons. * share/txr/stdlib/getput.tl (get-jsons, put-jsons, file-get-json, file-put-json, file-append-json, file-get-jsons, file-put-jsons, file-append-jsons, command-get-json, command-put-json, command-get-jsons and command-put-jsons): New functions. --- lisplib.c | 5 +++++ share/txr/stdlib/getput.tl | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/lisplib.c b/lisplib.c index a58a8972..26367cba 100644 --- a/lisplib.c +++ b/lisplib.c @@ -517,15 +517,20 @@ static val package_instantiate(val set_fun) static val getput_set_entries(val dlt, val fun) { val name[] = { + lit("get-jsons"), lit("put-jsons"), lit("file-get"), lit("file-put"), lit("file-append"), lit("file-get-string"), lit("file-put-string"), lit("file-append-string"), lit("file-get-lines"), lit("file-put-lines"), lit("file-append-lines"), lit("file-get-buf"), lit("file-put-buf"), lit("file-place-buf"), lit("file-append-buf"), + lit("file-get-json"), lit("file-put-json"), lit("file-append-json"), + lit("file-get-jsons"), lit("file-put-jsons"), lit("file-append-jsons"), lit("command-get"), lit("command-put"), lit("command-get-string"), lit("command-put-string"), lit("command-get-lines"), lit("command-put-lines"), lit("command-get-buf"), lit("command-put-buf"), + lit("command-get-json"), lit("command-put-json"), + lit("command-get-jsons"), lit("command-put-jsons"), nil }; set_dlt_entries(dlt, name, fun); diff --git a/share/txr/stdlib/getput.tl b/share/txr/stdlib/getput.tl index 7f9c2af2..55d0f866 100644 --- a/share/txr/stdlib/getput.tl +++ b/share/txr/stdlib/getput.tl @@ -45,6 +45,19 @@ (buf-set-length b (min (+ p p) bytes))))) b)) +(defun get-jsons (: (s *stdin*)) + (build + (catch* + (while t + (add (get-json s))) + (syntax-error (type . args) + (if (parse-errors s) + (throw type . args)))))) + +(defun put-jsons (list : (s *stdout*) flat-p) + (each ((obj list)) + (put-jsonl obj s flat-p))) + (defun file-get (name) (with-stream (s (open-file name)) (read s))) @@ -100,6 +113,30 @@ (with-stream (s (open-file name "ab")) (put-buf buf 0 s))) +(defun file-get-json (name) + (with-stream (s (open-file name)) + (get-json s))) + +(defun file-put-json (name : flat-p) + (with-stream (s (open-file name)) + (put-jsonl s flat-p))) + +(defun file-append-json (name : flat-p) + (with-stream (s (open-file name "a")) + (put-jsonl s flat-p))) + +(defun file-get-jsons (name) + (with-stream (s (open-file name)) + (get-jsons s))) + +(defun file-put-jsons (name : flat-p) + (with-stream (s (open-file name)) + (put-jsons s flat-p))) + +(defun file-append-jsons (name : flat-p) + (with-stream (s (open-file name "a")) + (put-jsons s flat-p))) + (defun command-get (cmd) (with-stream (s (open-command cmd)) (read s))) @@ -130,3 +167,19 @@ (defun command-put-buf (cmd buf) (with-stream (s (open-command cmd "wb")) (put-buf buf 0 s))) + +(defun command-get-json (cmd) + (with-stream (s (open-command cmd)) + (get-json s))) + +(defun command-put-json (cmd : flat-p) + (with-stream (s (open-command cmd "w")) + (put-jsonl s flat-p))) + +(defun command-get-jsons (cmd) + (with-stream (s (open-command cmd)) + (get-jsons s))) + +(defun command-put-jsons (cmd : flat-p) + (with-stream (s (open-command cmd "w")) + (put-jsons s flat-p))) -- cgit v1.2.3