Skip to content

Commit 9a4d0da

Browse files
committed
Use native JSON functionality if available
1 parent 271b831 commit 9a4d0da

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

phpactor.el

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@
6767
(expand-file-name (locate-user-emacs-file "phpactor/")))
6868
"Directory for setup Phactor. (default `~/.emacs.d/phpactor/')."
6969
:type 'directory)
70+
71+
(defcustom phpactor-use-native-json t
72+
"If non-nil, use native json parsing if available."
73+
:group 'phpactor
74+
:type 'boolean)
7075

7176
;; Variables
7277
;;;###autoload
@@ -191,10 +196,8 @@ of GitHub.")
191196
(defun phpactor--rpc (action arguments)
192197
"Execute Phpactor `ACTION' subcommand with `ARGUMENTS'."
193198
(phpactor--add-history 'phpactor--rpc (list action arguments))
194-
(let ((json (json-encode (list :action action
195-
:parameters arguments)))
196-
(json-object-type 'plist)
197-
(json-array-type 'list)
199+
(let ((json (phpactor--serialize-json (list :action action
200+
:parameters arguments)))
198201
(output (get-buffer-create "*Phpactor Output*"))
199202
(phpactor-executable (phpactor-find-executable))
200203
(cwd (phpactor-get-working-dir)))
@@ -205,9 +208,26 @@ of GitHub.")
205208
(erase-buffer)
206209
(insert json)
207210
(call-process-region (point-min) (point-max) phpactor-executable nil output nil "rpc" (format "--working-dir=%s" default-directory))
208-
(with-current-buffer output
209-
(goto-char (point-min))
211+
(phpactor--parse-json output))))
212+
213+
(defun phpactor--parse-json (buffer)
214+
"Read JSON string from BUFFER."
215+
(with-current-buffer buffer
216+
(goto-char (point-min))
217+
(if (and phpactor-use-native-json
218+
(fboundp 'json-serialize))
219+
(with-no-warnings
220+
(json-parse-buffer :object-type 'plist :array-type 'list))
221+
(let ((json-object-type 'plist) (json-array-type 'list))
210222
(json-read-object)))))
223+
224+
(defun phpactor--serialize-json (params)
225+
"Serialize PARAMS in to a JSON string."
226+
(if (and phpactor-use-native-json
227+
(fboundp 'json-serialize))
228+
(with-no-warnings
229+
(json-serialize params :null-object nil :false-object :json-false))
230+
(json-encode params)))
211231

212232
;;; Phpactor Action
213233

0 commit comments

Comments
 (0)