-
Notifications
You must be signed in to change notification settings - Fork 3
/
njson.lisp
90 lines (79 loc) · 3.07 KB
/
njson.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
;;;; SPDX-FileCopyrightText: Atlas Engineer LLC
;;;; SPDX-License-Identifier: BSD-3-Clause
(in-package #:njson)
(defgeneric decode-from-stream (stream)
(:method (stream)
(declare (ignore stream))
(signal 'decode-from-stream-not-implemented))
(:documentation "Decode JSON from STREAM.
Specialize on `stream' to make NJSON decode JSON."))
(defgeneric decode-from-string (string)
(:method (string)
(with-input-from-string (stream string)
(decode-from-stream stream)))
(:documentation "Decode JSON from STRING.
Specialize on `string' to make NJSON better decode JSON strings.
Uses `decode-from-stream' by default."))
(defgeneric decode-from-file (file)
(:method (file)
(with-open-file (stream file :direction :input)
(decode-from-stream stream)))
(:documentation "Decode JSON from FILE.
Specialize on `pathname' to make NJSON better decode JSON files.
Uses `decode-from-stream' by default."))
(defgeneric decode (from)
(:method ((from stream))
(decode-from-stream from))
(:method ((from pathname))
(decode-from-file from))
(:method ((from string))
(decode-from-string from))
(:documentation "Decode OBJECT from JSON source FROM.
FROM can be a string, stream, pathname, or byte array.
Distinguishes between null/false and arrays/objects.
Decodes:
- null as :NULL,
- false as nil,
- true as t,
- arrays as vectors,
- objects as hash-tables."))
(defgeneric encode-to-stream (object stream)
(:method (object stream)
(declare (ignore object stream))
(signal 'encode-to-stream-not-implemented))
(:documentation "Encode OBJECT to STREAM as JSON.
Specialize on `stream' (and, optionally, OBJECT types) to make NJSON encode JSON."))
(defgeneric encode-to-string (object)
(:method (object)
(with-output-to-string (stream)
(encode-to-stream object stream)
(get-output-stream-string stream)))
(:documentation "Encode OBJECT to JSON string.
Specialize on `string' (and, optionally, OBJECT types) to make NJSON better encode JSON to strings.
Uses `encode-to-stream' by default."))
(defgeneric encode-to-file (object file)
(:method (object file)
(with-open-file (stream file)
(encode-to-stream object stream)))
(:documentation "Encode OBJECT to FILE.
Specialize on `pathname' (and, optionally, OBJECT types) to make NJSON better encode JSON to files.
Uses `encode-to-stream' by default."))
(defgeneric encode (object &optional to)
(:method :around (object &optional to)
(typecase to
(null (encode-to-string object))
(pathname (encode-to-file object to))
(stream (call-next-method object to))
((eql t) (call-next-method object *standard-output*))))
(:method (object &optional to)
(encode-to-stream object to))
(:documentation "Encode OBJECT to JSON output spec TO.
TO can be:
- T, in which case `*standard-output*' is used as encoding stream.
- NIL, in which case OBJECT is encoded to a string.
- STREAM, in which case OBJECT is encoded to it.
- PATHNAME, in which case OBJECT is encoded to the file designated by the pathname.
Distinguishes between null and false.
Encodes:
- :NULL as null,
- nil as false."))