! Copyright (C) 2016 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. USING: assocs fry hashtables io io.encodings.utf8 io.files io.streams.string json.reader json.writer kernel math namespaces sequences strings ; IN: json.prettyprint [ indent-level get nspaces * CHAR: \s write ] when ; M: object pprint-json* json-print ; M: string pprint-json* json-print ; M: f pprint-json* json-print ; M: sequence pprint-json* [ "[ ]" write ] [ "[" print indent-level inc [ "," print ] [ write-spaces pprint-json* ] interleave nl indent-level dec write-spaces "]" write ] if-empty ; M: hashtable pprint-json* dup assoc-empty? [ drop "{ }" write ] [ "{" print indent-level inc >alist [ "," print ] [ first2 [ write-spaces pprint-json* ": " write ] [ pprint-json* ] bi* ] interleave nl indent-level dec write-spaces "}" write ] if ; PRIVATE> : pprint-json ( obj -- ) [ 0 indent-level ] dip '[ _ pprint-json* ] with-variable ; : pprint-json>path ( json path -- ) utf8 [ pprint-json ] with-file-writer ; : pprint-json>string ( json -- string ) [ pprint-json ] with-string-writer ; : pprint-json-file ( path -- ) [ path>json ] [ pprint-json>path ] bi ;