From 3ba1090d774128fbb9b7cc49bd54044cdc5d979a Mon Sep 17 00:00:00 2001 From: Edward Langley Date: Fri, 23 Sep 2022 12:23:44 -0700 Subject: [PATCH 1/2] Allow YASON:ENCODE to work without setup Fixes https://github.com/phmarek/yason/issues/72 --- encode.lisp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/encode.lisp b/encode.lisp index 9b3cea2..6f3a75d 100644 --- a/encode.lisp +++ b/encode.lisp @@ -7,7 +7,7 @@ (in-package :yason) -(defvar *json-output*) +(defvar *json-output* (make-json-output-stream *standard-output*)) (defparameter *default-indent* nil "Set to T or an numeric indentation width in order to have YASON @@ -206,7 +206,7 @@ (*package* (symbol-package sym))) (if (keywordp sym) (format nil "~a~s" - (or prefix "") + (or prefix "") sym) (format nil "~a~a::~s" (or prefix "") From b915de71a3ead1f6cd9b2c86ea2f4f28306e6317 Mon Sep 17 00:00:00 2001 From: Edward Langley Date: Mon, 26 Sep 2022 21:32:18 -0700 Subject: [PATCH 2/2] Reorder definitions to allow loading into a fresh image --- encode.lisp | 55 +++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/encode.lisp b/encode.lisp index 6f3a75d..c84b528 100644 --- a/encode.lisp +++ b/encode.lisp @@ -7,8 +7,6 @@ (in-package :yason) -(defvar *json-output* (make-json-output-stream *standard-output*)) - (defparameter *default-indent* nil "Set to T or an numeric indentation width in order to have YASON indent its output by default.") @@ -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 @@ -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)))