;;; qr-json.lisp -- Like ST-JSON but represent arrays as arrays and objects as alists ;;; Copyright (c) 2017 Devon Sean McCullough ;;; Licensed under the GNU GPL or the MIT license at your option. (require '#:st-json) (defpackage #:qr-json (:use #:cl) (:export #:|#"-reader| #:|#[-reader| #:|#{-reader| #:read-json #:write-json)) (in-package #:qr-json) (defun lispify (jso) "Clone JSO turning jso objects into alists and lists into arrays." (typecase jso (jso (mapcar (lambda (key-value-pair) (cons (car key-value-pair) (lispify (cdr key-value-pair)))) (jso-alist jso))) (cons (map 'vector #'lispify jso)) (t jso))) (defun objectify (jsl) "Clone JSL turning alists into jso objects and arrays into lists." (typecase jsl (cons (make-jso :alist (mapcar (lambda (key-value-pair) (cons (car key-value-pair) (objectify (cdr key-value-pair)))) jsl))) ((and vector (not string)) (map 'list #'objectify jsl)) (t jsl))) (defun read-json* (in &optional (junk-allowed-p (streamp in) junk-allowed-p-specified)) "Like `read-json' but arrays are arrays and objects are alists." (lispify (if junk-allowed-p-specified (read-json in junk-allowed-p) (read-json in)))) (defun write-json* (element &optional (stream *standard-output*)) "Like `write-json' but arrays are arrays, objects are alists and STREAM is optional." (write-json (objectify element) stream)) (provide '#:st-json-helper) ;;; or-json.lisp end