json.writer: Ability to turn jsvar-encode substituion on/off via jsvar-encode? dynamic variable. Removed whitespace in tuple>fields json output. Added tests to json.writer-tests.
							parent
							
								
									40c45e08ca
								
							
						
					
					
						commit
						063076cfa5
					
				| 
						 | 
				
			
			@ -1,4 +1,4 @@
 | 
			
		|||
USING: json.writer tools.test json.reader json ;
 | 
			
		||||
USING: hashtables json.writer tools.test json.reader json kernel namespaces ;
 | 
			
		||||
IN: json.writer.tests
 | 
			
		||||
 | 
			
		||||
{ "false" } [ f >json ] unit-test
 | 
			
		||||
| 
						 | 
				
			
			@ -18,3 +18,19 @@ SYMBOL: testSymbol
 | 
			
		|||
{ """"testSymbol"""" } [ testSymbol >json ] unit-test
 | 
			
		||||
 | 
			
		||||
[ { 0.5 } ] [ { 1/2 } >json json> ] unit-test
 | 
			
		||||
 | 
			
		||||
[ "{\"b-b\":\"asdf\"}" ] 
 | 
			
		||||
    [ f jsvar-encode? [ "asdf" "b-b" associate >json ] with-variable ] unit-test
 | 
			
		||||
 | 
			
		||||
[ "{\"b_b\":\"asdf\"}" ]
 | 
			
		||||
    [ t jsvar-encode? [ "asdf" "b-b" associate >json ] with-variable ] unit-test
 | 
			
		||||
 | 
			
		||||
TUPLE: person name age a-a ;
 | 
			
		||||
[ "{\"name\":\"David-David\",\"age\":32,\"a_a\":{\"b_b\":\"asdf\"}}" ]
 | 
			
		||||
    [ t jsvar-encode? 
 | 
			
		||||
        [ "David-David" 32 H{ { "b-b" "asdf" } } person boa >json ] 
 | 
			
		||||
        with-variable ] unit-test
 | 
			
		||||
[ "{\"name\":\"Alpha-Beta\",\"age\":32,\"a-a\":{\"b-b\":\"asdf\"}}" ]
 | 
			
		||||
    [ f jsvar-encode? 
 | 
			
		||||
        [ "Alpha-Beta" 32 H{ { "b-b" "asdf" } } person boa >json ] 
 | 
			
		||||
        with-variable ] unit-test
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
! 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 ;
 | 
			
		||||
prettyprint hashtables mirrors tr json fry ;
 | 
			
		||||
IN: json.writer
 | 
			
		||||
 | 
			
		||||
#! Writes the object out to a stream in JSON format
 | 
			
		||||
| 
						 | 
				
			
			@ -33,20 +33,23 @@ M: real json-print ( num -- )
 | 
			
		|||
M: sequence json-print ( array -- ) 
 | 
			
		||||
    CHAR: [ write1 [ >json ] map "," join write CHAR: ] write1 ;
 | 
			
		||||
 | 
			
		||||
TR: jsvar-encode "-" "_" ;
 | 
			
		||||
SYMBOL: jsvar-encode?
 | 
			
		||||
t jsvar-encode? set-global
 | 
			
		||||
TR: jsvar-encode "-" "_" ; 
 | 
			
		||||
  
 | 
			
		||||
: tuple>fields ( object -- seq )
 | 
			
		||||
    <mirror> [
 | 
			
		||||
        [ swap jsvar-encode >json % " : " % >json % ] "" make
 | 
			
		||||
    ] { } assoc>map ;
 | 
			
		||||
    <mirror>
 | 
			
		||||
    jsvar-encode? get
 | 
			
		||||
    '[ [ swap _ [ jsvar-encode ] when >json % ":" % >json % ] "" make ] { } assoc>map ;
 | 
			
		||||
 | 
			
		||||
M: tuple json-print ( tuple -- )
 | 
			
		||||
    CHAR: { write1 tuple>fields "," join write CHAR: } write1 ;
 | 
			
		||||
 | 
			
		||||
M: hashtable json-print ( hashtable -- )
 | 
			
		||||
    CHAR: { write1 
 | 
			
		||||
    [ [ swap jsvar-encode >json % CHAR: : , >json % ] "" make ]
 | 
			
		||||
    { } assoc>map "," join write 
 | 
			
		||||
    jsvar-encode? get
 | 
			
		||||
    '[ [ swap _ [ jsvar-encode ] when >json % CHAR: : , >json % ] "" make ] { } assoc>map
 | 
			
		||||
    "," join write 
 | 
			
		||||
    CHAR: } write1 ;
 | 
			
		||||
 | 
			
		||||
M: word json-print name>> json-print ;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue