Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 29 additions & 28 deletions encode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

(in-package :yason)

(defvar *json-output*)

(defparameter *default-indent* nil
"Set to T or an numeric indentation width in order to have YASON
indent its output by default.")
Expand All @@ -34,6 +32,34 @@
ENCODE-SYMBOL-AS-STRING here.")


(defclass json-output-stream (trivial-gray-streams:fundamental-character-output-stream)
((output-stream :reader output-stream
:initarg :output-stream)
(stack :accessor stack
:initform nil)
(indent-depth :initform 0
:accessor indent-depth)
(indent :initarg :indent
:reader indent
:accessor indent%))
(:default-initargs :indent *default-indent*)
(:documentation "Objects of this class capture the state of a JSON stream encoder."))

(defmethod initialize-instance :after ((stream json-output-stream) &key indent)
(when (eq indent t)
(setf (indent% stream) *default-indent-width*)))

(defgeneric make-json-output-stream (stream &key indent))

(defmethod make-json-output-stream (stream &key (indent t))
"Create a JSON output stream with indentation enabled."
(if indent
(make-instance 'json-output-stream :output-stream stream :indent indent)
stream))

(defvar *json-output* (make-json-output-stream *standard-output*))


(defgeneric encode (object &optional stream)

(:documentation "Encode OBJECT to STREAM in JSON format. May be
Expand Down Expand Up @@ -206,7 +232,7 @@
(*package* (symbol-package sym)))
(if (keywordp sym)
(format nil "~a~s"
(or prefix "")
(or prefix "")
sym)
(format nil "~a~a::~s"
(or prefix "")
Expand Down Expand Up @@ -258,31 +284,6 @@
(write-string "null" stream)
object)

(defclass json-output-stream (trivial-gray-streams:fundamental-character-output-stream)
((output-stream :reader output-stream
:initarg :output-stream)
(stack :accessor stack
:initform nil)
(indent-depth :initform 0
:accessor indent-depth)
(indent :initarg :indent
:reader indent
:accessor indent%))
(:default-initargs :indent *default-indent*)
(:documentation "Objects of this class capture the state of a JSON stream encoder."))

(defmethod initialize-instance :after ((stream json-output-stream) &key indent)
(when (eq indent t)
(setf (indent% stream) *default-indent-width*)))

(defgeneric make-json-output-stream (stream &key indent))

(defmethod make-json-output-stream (stream &key (indent t))
"Create a JSON output stream with indentation enabled."
(if indent
(make-instance 'json-output-stream :output-stream stream :indent indent)
stream))

(defmethod trivial-gray-streams:stream-write-char ((stream json-output-stream) char)
(write-char char (output-stream stream)))

Expand Down