mirror of
https://github.com/urbit/shrub.git
synced 2025-01-05 19:46:50 +03:00
better json
This commit is contained in:
parent
f60687393e
commit
b0d0eee3fb
179
arvo/zuse.hoon
179
arvo/zuse.hoon
@ -416,98 +416,98 @@
|
||||
?~ myn ~
|
||||
?: =(~ t.myn) (trip i.myn)
|
||||
(weld (trip i.myn) `tape`['/' $(myn t.myn)])
|
||||
|
||||
++ poja
|
||||
|= jas=tape
|
||||
=< (scan jas valu)
|
||||
|%
|
||||
++ valu :: JSON value
|
||||
%+ knee *jval |. ~+
|
||||
;~ pfix spac
|
||||
;~ pose
|
||||
(cold ~ (jest 'null'))
|
||||
(jify %b bool)
|
||||
(jify %s stri)
|
||||
(cook |=(s=tape [%n p=(rap 3 s)]) numb)
|
||||
abox
|
||||
obox
|
||||
==
|
||||
==
|
||||
:: JSON arrays
|
||||
++ arra (ifix [sel (ws ser)] (more (ws com) valu))
|
||||
++ abox (cook |=(elts=(list jval) [%a p=elts]) arra)
|
||||
:: JSON objects
|
||||
++ pair ;~((comp |=([k=@ta v=jval] [k v])) ;~(sfix (ws stri) (ws col)) valu)
|
||||
++ obje (ifix [(ws kel) (ws ker)] (more (ws com) pair))
|
||||
++ obox (cook |=(s=(list ,[@ta jval]) [%o p=(mo s)]) obje)
|
||||
:: JSON booleans
|
||||
++ bool ;~(pose (cold & (jest 'true')) (cold | (jest 'false')))
|
||||
:: JSON strings
|
||||
++ stri
|
||||
(cook |=(s=(list ,@) (rap 3 s)) (ifix [doq doq] (star jcha)))
|
||||
++ jcha :: character in string
|
||||
++ valu :: JSON value
|
||||
%+ knee *jval |. ~+
|
||||
;~ pfix spac
|
||||
;~ pose
|
||||
esca
|
||||
;~ pose
|
||||
:: Non-escape string characters
|
||||
(shim 32 33)
|
||||
(shim 35 91)
|
||||
(shim 93 126)
|
||||
(shim 128 255)
|
||||
==
|
||||
(cold ~ (jest 'null'))
|
||||
(jify %b bool)
|
||||
(jify %s stri)
|
||||
(cook |=(s=tape [%n p=(rap 3 s)]) numb)
|
||||
abox
|
||||
obox
|
||||
==
|
||||
++ esca :: Escaped character
|
||||
;~ pfix bas
|
||||
;~ pose
|
||||
doq
|
||||
fas
|
||||
soq
|
||||
bas
|
||||
(cold 8 (just 'b'))
|
||||
(cold 9 (just 't'))
|
||||
(cold 10 (just 'n'))
|
||||
(cold 12 (just 'f'))
|
||||
(cold 13 (just 'r'))
|
||||
;~(pfix (just 'u') (cook tuft qix:ab)) :: Convert 4-digit hex to UTF-8
|
||||
==
|
||||
==
|
||||
:: JSON arrays
|
||||
++ arra (ifix [sel (ws ser)] (more (ws com) valu))
|
||||
++ abox (cook |=(elts=(list jval) [%a p=elts]) arra)
|
||||
:: JSON objects
|
||||
++ pair ;~((comp |=([k=@ta v=jval] [k v])) ;~(sfix (ws stri) (ws col)) valu)
|
||||
++ obje (ifix [(ws kel) (ws ker)] (more (ws com) pair))
|
||||
++ obox (cook |=(s=(list ,[@ta jval]) [%o p=(mo s)]) obje)
|
||||
:: JSON booleans
|
||||
++ bool ;~(pose (cold & (jest 'true')) (cold | (jest 'false')))
|
||||
:: JSON strings
|
||||
++ stri
|
||||
(cook |=(s=(list ,@) (rap 3 s)) (ifix [doq doq] (star jcha)))
|
||||
++ jcha :: character in string
|
||||
;~ pose
|
||||
esca
|
||||
;~ pose
|
||||
:: Non-escape string characters
|
||||
(shim 32 33)
|
||||
(shim 35 91)
|
||||
(shim 93 126)
|
||||
(shim 128 255)
|
||||
==
|
||||
:: JSON numbers
|
||||
++ numb
|
||||
;~ (comp twel)
|
||||
(mayb (piec hep))
|
||||
;~ pose
|
||||
(piec (just '0'))
|
||||
;~((comp twel) (piec (shim '1' '9')) digs)
|
||||
==
|
||||
(mayb frac)
|
||||
(mayb expo)
|
||||
==
|
||||
++ esca :: Escaped character
|
||||
;~ pfix bas
|
||||
;~ pose
|
||||
doq
|
||||
fas
|
||||
soq
|
||||
bas
|
||||
(cold 8 (just 'b'))
|
||||
(cold 9 (just 't'))
|
||||
(cold 10 (just 'n'))
|
||||
(cold 12 (just 'f'))
|
||||
(cold 13 (just 'r'))
|
||||
;~(pfix (just 'u') (cook tuft qix:ab)) :: Convert 4-digit hex to UTF-8
|
||||
==
|
||||
++ digs (star (shim '0' '9'))
|
||||
++ expo :: Exponent part
|
||||
;~ (comp twel)
|
||||
(piec (mask "eE"))
|
||||
(mayb (piec (mask "+-")))
|
||||
digs
|
||||
==
|
||||
:: JSON numbers
|
||||
++ numb
|
||||
;~ (comp twel)
|
||||
(mayb (piec hep))
|
||||
;~ pose
|
||||
(piec (just '0'))
|
||||
;~((comp twel) (piec (shim '1' '9')) digs)
|
||||
==
|
||||
++ frac :: Fractional part
|
||||
;~ (comp twel)
|
||||
(piec dot)
|
||||
digs
|
||||
==
|
||||
:: whitespace
|
||||
++ spac (star (mask [`@`9 `@`10 `@`13 ' ' ~]))
|
||||
++ ws |*(sef=_rule ;~(pfix spac sef))
|
||||
:: plumbing
|
||||
++ jify |*([t=@ta r=_rule] (cook |*([v=*] [t p=v]) r))
|
||||
++ mayb |*(bus=_rule ;~(pose bus (easy "")))
|
||||
++ twel |=([a=tape b=tape] (weld a b))
|
||||
++ piec
|
||||
|* bus=_rule
|
||||
(cook |=(a=@ [a ~]) bus)
|
||||
--
|
||||
(mayb frac)
|
||||
(mayb expo)
|
||||
==
|
||||
++ digs (star (shim '0' '9'))
|
||||
++ expo :: Exponent part
|
||||
;~ (comp twel)
|
||||
(piec (mask "eE"))
|
||||
(mayb (piec (mask "+-")))
|
||||
digs
|
||||
==
|
||||
++ frac :: Fractional part
|
||||
;~ (comp twel)
|
||||
(piec dot)
|
||||
digs
|
||||
==
|
||||
:: whitespace
|
||||
++ spac (star (mask [`@`9 `@`10 `@`13 ' ' ~]))
|
||||
++ ws |*(sef=_rule ;~(pfix spac sef))
|
||||
:: plumbing
|
||||
++ jify |*([t=@ta r=_rule] (cook |*([v=*] [t p=v]) r))
|
||||
++ mayb |*(bus=_rule ;~(pose bus (easy "")))
|
||||
++ twel |=([a=tape b=tape] (weld a b))
|
||||
++ piec
|
||||
|* bus=_rule
|
||||
(cook |=(a=@ [a ~]) bus)
|
||||
--
|
||||
|
||||
::
|
||||
++ pojo :: print json
|
||||
|= val=jval
|
||||
!:
|
||||
^- tape
|
||||
?~ val "null"
|
||||
?- -.val
|
||||
@ -523,7 +523,15 @@
|
||||
::
|
||||
%b ?:(p.val "true" "false")
|
||||
%n (trip p.val)
|
||||
%s :(weld "\"" (trip p.val) "\"")
|
||||
%s
|
||||
;: weld
|
||||
"\""
|
||||
%+ reel
|
||||
(turn (trip p.val) jesc)
|
||||
=| [p=tape q=tape]
|
||||
|.((weld p q))
|
||||
"\""
|
||||
==
|
||||
%o
|
||||
;: weld
|
||||
"\{"
|
||||
@ -539,6 +547,13 @@
|
||||
"}"
|
||||
==
|
||||
==
|
||||
++ jesc
|
||||
|= a=@
|
||||
?+ a [a ~]
|
||||
10
|
||||
"\\n"
|
||||
==
|
||||
|
||||
::
|
||||
++ tact :: tape to octstream
|
||||
|= tep=tape ^- octs
|
||||
|
Loading…
Reference in New Issue
Block a user