-
Notifications
You must be signed in to change notification settings - Fork 2
/
base.lisp
169 lines (129 loc) · 4.97 KB
/
base.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;;;; json-streams
;;;;
;;;; Copyright (C) 2013 Thomas Bakketun <thomas.bakketun@copyleft.no>
;;;;
;;;; This library is free software: you can redistribute it and/or modify
;;;; it under the terms of the GNU Lesser General Public License as published
;;;; by the Free Software Foundation, either version 3 of the License, or
;;;; (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this library. If not, see <http://www.gnu.org/licenses/>.
(in-package #:json-streams)
(deftype json-object ()
'(cons (member :object) t))
(deftype json-array ()
'(cons (member :array) t))
(deftype json-string ()
'(or string
(cons (member :string) t)))
(when (= char-code-limit #x10000)
(push 'utf-16-strings *features*))
(defconstant +most-positive-json-integer+
(expt 2 53))
(defconstant +most-negative-json-integer+
(- (expt 2 53)))
(defvar *json-stream*)
(defclass json-stream ()
((stream :initarg :stream)
(close-stream :initarg :close-stream)
(state-stack :initform '(:before-json-text))
(duplicate-key-check :initarg :duplicate-key-check)
(key-check-stack :initform nil)))
(defclass json-input-stream (json-stream)
((multiple :initarg :multiple)
(use-ratios :initarg :use-ratios)
(max-exponent :initarg :max-exponent)
(raw-strings :initarg :raw-strings)
(current-char :initform nil)
(position :initarg :position :reader json-stream-position)
(newlines :initform '())
(string-mode :initform nil)))
(defclass json-output-stream (json-stream)
((multiple :initarg :multiple)
(escape-non-ascii :initarg :escape-non-ascii)
(indent :initarg :indent)
(level :initform 0)
(key-encoder :initarg :key-encoder :initform nil)))
(defgeneric %json-close (json-stream))
(defun json-close (json-stream &key abort)
(with-slots (stream close-stream)
json-stream
(unless abort
(%json-close json-stream))
(when close-stream
(close stream :abort abort))))
(defmacro with-open-json-stream ((var stream) &body body)
(let ((errorsp (gensym "ERRORSP")))
`(let ((,var ,stream)
(,errorsp t))
(unwind-protect
(multiple-value-prog1 ,@body
(setf ,errorsp nil))
(json-close ,var :abort ,errorsp)))))
(defun begin-object ()
(with-slots (duplicate-key-check key-check-stack)
*json-stream*
(when duplicate-key-check
(push (make-hash-table :test #'equal) key-check-stack))))
(defun end-object ()
(with-slots (duplicate-key-check key-check-stack)
*json-stream*
(when duplicate-key-check
(pop key-check-stack))))
(defun check-key (key)
(with-slots (duplicate-key-check key-check-stack)
*json-stream*
(when duplicate-key-check
(when (gethash key (car key-check-stack))
(%json-error "Duplicate key ~S in object" key))
(setf (gethash key (car key-check-stack)) t))))
(defmacro state-stack ()
`(slot-value *json-stream* 'state-stack))
(defun push-state (new-state)
(push new-state (state-stack)))
(defun pop-state ()
(pop (state-stack)))
(defun switch-state (new-state)
(pop-state)
(push-state new-state))
(define-condition json-error (error)
((stream :initform *json-stream* :initarg :stream :reader json-error-stream)
(message :initarg :message :reader json-error-message))
(:report (lambda (c stream)
(format stream "JSON parse error from ~A: ~A"
(json-error-stream c)
(json-error-message c)))))
(define-condition json-parse-error (json-error)
()
(:report (lambda (c stream)
(let ((newlines (remove-duplicates (slot-value (json-error-stream c) 'newlines))))
(format stream "JSON parse error at line ~D column ~D: ~A"
(1+ (length newlines))
(- (slot-value (json-error-stream c) 'position)
(or (car newlines) 0))
(json-error-message c))))))
(define-condition json-write-error (json-error)
()
(:report (lambda (c stream)
(format stream "JSON write error: ~A." (json-error-message c)))))
(defun %json-error (message &rest args)
(switch-state :eof)
(error (etypecase *json-stream*
(json-input-stream 'json-parse-error)
(json-output-stream 'json-write-error))
:message (apply #'format nil message args)))
(defmacro ecase* (keyform &body cases)
(let ((key (gensym "KEY")))
`(let ((,key ,keyform))
(case ,key
,@cases
(otherwise (%json-error "Expected one of ~S, got ~S in state ~S."
',(mapcar #'car cases)
,key
(slot-value *json-stream* 'state-stack)))))))