elisp: implement stdio-read-json
This commit is contained in:
parent
7a504bc311
commit
8e9b157906
3 changed files with 56 additions and 4 deletions
22
elisp/json-test
Normal file
22
elisp/json-test
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
"hello world"
|
||||||
|
2
|
||||||
|
{ "a": 1,
|
||||||
|
"b": 2,
|
||||||
|
"c": 3
|
||||||
|
}
|
||||||
|
|
||||||
|
1
|
||||||
|
|
||||||
|
true
|
||||||
|
|
||||||
|
false
|
||||||
|
|
||||||
|
null
|
||||||
|
|
||||||
|
"hello"
|
||||||
|
|
||||||
|
[ 1, 2,
|
||||||
|
|
||||||
|
3,
|
||||||
|
|
||||||
|
4]
|
|
@ -11,5 +11,14 @@
|
||||||
(while-let ((line (stdio-read-line process)))
|
(while-let ((line (stdio-read-line process)))
|
||||||
(message (format "jx-test: %s" line)))))
|
(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)
|
(provide 'jx)
|
||||||
;;; jx.el ends here
|
;;; jx.el ends here
|
||||||
|
|
|
@ -9,13 +9,14 @@
|
||||||
emacs-process buffer)
|
emacs-process buffer)
|
||||||
|
|
||||||
(defun stdio-make-filter (process)
|
(defun stdio-make-filter (process)
|
||||||
"Return a filter function for PROCESS."
|
"Return a input filter function for PROCESS."
|
||||||
(lambda (_ input)
|
(lambda (_ input)
|
||||||
(setf (stdio-process-buffer process)
|
(setf (stdio-process-buffer process)
|
||||||
(concat (stdio-process-buffer process)
|
(concat (stdio-process-buffer process)
|
||||||
input))))
|
input))))
|
||||||
|
|
||||||
(defvar stdio--process-counter 0)
|
(defvar stdio--process-counter 0)
|
||||||
|
|
||||||
(defun stdio--make-process-name ()
|
(defun stdio--make-process-name ()
|
||||||
"Generate a new uniqe process name."
|
"Generate a new uniqe process name."
|
||||||
(let* ((num stdio--process-counter)
|
(let* ((num stdio--process-counter)
|
||||||
|
@ -54,5 +55,25 @@
|
||||||
(progn (setf (stdio-process-buffer process) "")
|
(progn (setf (stdio-process-buffer process) "")
|
||||||
stdin-buf)))))
|
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)
|
(provide 'stdio)
|
||||||
;;; stdio.el ends here
|
;;; stdio.el ends here
|
||||||
|
|
Loading…
Add table
Reference in a new issue