elisp: implement stdio-read-json

This commit is contained in:
Michael Volz 2025-01-25 00:55:11 +01:00
parent 7a504bc311
commit 8e9b157906
3 changed files with 56 additions and 4 deletions

22
elisp/json-test Normal file
View file

@ -0,0 +1,22 @@
"hello world"
2
{ "a": 1,
"b": 2,
"c": 3
}
1
true
false
null
"hello"
[ 1, 2,
3,
4]

View file

@ -11,5 +11,14 @@
(while-let ((line (stdio-read-line process)))
(message (format "jx-test: %s" line)))))
(defun jx-text-json ()
"test the jx json input."
(interactive)
(let ((process (stdio-call (list "cat" "json-test"))))
(while-let ((object (stdio-read-json process
:object-type 'plist
:array-type 'list)))
(message (format "jx-json-object: %s" object)))))
(provide 'jx)
;;; jx.el ends here

View file

@ -9,17 +9,18 @@
emacs-process buffer)
(defun stdio-make-filter (process)
"Return a filter function for PROCESS."
"Return a input filter function for PROCESS."
(lambda (_ input)
(setf (stdio-process-buffer process)
(concat (stdio-process-buffer process)
input))))
(defvar stdio--process-counter 0)
(defun stdio--make-process-name ()
"Generate a new uniqe process name."
(let* ((num stdio--process-counter)
(name (format "stdio-subp-%d" num)))
(name (format "stdio-subp-%d" num)))
(setq stdio--process-counter (+ stdio--process-counter 1))
name))
@ -51,8 +52,28 @@
(stdio-read-line process)
;; no more output from process
(when (> (length stdin-buf) 0)
(progn (setf (stdio-process-buffer process) "")
stdin-buf)))))
(progn (setf (stdio-process-buffer process) "")
stdin-buf)))))
(defun stdio-read-json (process &rest JSON-ARGS)
"Read next json element from PROCESS.
The arguments JSON-ARGS are directly passed to `json-parse-string'
and control how json elements are represented as Lisp constructs.
Json elements need to be seperated by newlines."
(let (next-line input-string json-object done)
(while (not done)
(setq next-line (stdio-read-line process)
input-string (concat input-string next-line)
json-object (condition-case err
(apply #'json-parse-string input-string JSON-ARGS)
(json-trailing-content (signal (car err) (cdr err)))
(json-end-of-file nil)
(json-parse-error nil))
done (or (not next-line)
json-object)))
json-object))
(provide 'stdio)
;;; stdio.el ends here