 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors kernel io.streams.string io strings splitting
 sequences math math.parser assocs classes words namespaces make
-prettyprint hashtables mirrors tr json fry ;
+prettyprint hashtables mirrors tr json fry combinators ;
 IN: json.writer
 
 #! Writes the object out to a stream in JSON format

    drop "null" write ;

M: string json-print ( obj -- )
    CHAR: " write1 "\"" split "\\\"" join CHAR: \r swap remove "\n" split "\\r\\n" join write CHAR: " write1 ;

M: integer json-print ( num -- )
    number>string write ;

M: real json-print ( num -- )
    >float number>string write ;

M: sequence json-print ( array -- )
    CHAR: [ write1 [
        unclip-last-slice swap
        [ json-print CHAR: , write1 ] each
        json-print
    ] unless-empty CHAR: ] write1 ;

SYMBOL: jsvar-encode?
t jsvar-encode? set-global
TR: jsvar-encode "-" "_" ;

<PRIVATE

: json-print-assoc ( assoc -- )
    CHAR: { write1 >alist [
        unclip-last-slice swap
        jsvar-encode? get [
            [
                [ first jsvar-encode json-print ]
                [ CHAR: : write1 second json-print ] bi
                CHAR: , write1
            ] each
            [ first jsvar-encode json-print ]
            [ CHAR: : write1 second json-print ] bi
        ] [
            [
                [ first json-print ]
                [ CHAR: : write1 second json-print ] bi
                CHAR: , write1
            ] each
            [ first json-print ]
            [ CHAR: : write1 second json-print ] bi
        ] if
    ] unless-empty CHAR: } write1 ;

PRIVATE>

M: tuple json-print ( tuple -- ) <mirror> json-print-assoc ;

M: hashtable json-print ( hashtable -- ) json-print-assoc ;

M: word json-print name>> json-print ;
