2e(2-3): Print & Parse JSON

+json:html

Container core for JSON decoding/encoding functions.

+en:json:html

Print json

Encode jon, a $json structure, in a $cord.

Accepts

jon is any $json.

Produces

A $cord.

Source

++en:sjon:html
++  en                                              ::  encode JSON to tape
  ~%  %en  +>+  ~
  |^  |=  jon=^json
      ^-  cord
      (rap 3 (flop (onto jon ~)))
  ::                                                ::  ++onto:en:json:html
  ++  onto
    |=  [val=^json out=(list @t)]
    ^+  out
    ?~  val  ['null' out]
    ?-    -.val
        %a
      ?~  p.val  ['[]' out]
      =.  out    ['[' out]
      !.
      |-  ^+  out
      =.  out  ^$(val i.p.val)
      ?~(t.p.val [']' out] $(p.val t.p.val, out [',' out]))
    ::
        %b
      [?:(p.val 'true' 'false') out]
    ::
        %n
      [p.val out]
    ::
        %s
      [(scap p.val) out]
    ::
        %o
      =/  viz  ~(tap by p.val)
      ?~  viz  ['{}' out]
      =.  out  ['{' out]
      !.
      |-  ^+  out
      =.  out  ^$(val q.i.viz, out [':' [(scap p.i.viz) out]])
      ?~(t.viz ['}' out] $(viz t.viz, out [',' out]))
    ==
  ::                                                ::  ++scap:en:json:html
  ++  scap
    |=  val=@t
    ^-  @t
    =/  out=(list @t)  ['"' ~]
    =/  len  (met 3 val)
    =|  [i=@ud pos=@ud]
    |-  ^-  @t
    ?:  =(len i)
      (rap 3 (flop ['"' (rsh [3 pos] val) out]))
    =/  car  (cut 3 [i 1] val)
    ?:  ?&  (gth car 0x1f)
            !=(car 0x22)
            !=(car 0x5C)
            !=(car 0x7F)
        ==
      $(i +(i))
    =/  cap
      ?+  car  (crip '\\' 'u' ((x-co 4):co car))
        %10    '\\n'
        %'"'   '\\"'
        %'\\'  '\\\\'
      ==
    $(i +(i), pos +(i), out [cap (cut 3 [pos (sub i pos)] val) out])
  --  ::en

Examples


+de:json:html

Parse json

Decode txt, a $cord of stringified JSON, to a (unit json) which is null if parsing failed.

Accepts

txt is a $cord.

Produces

A (unit json).

Source

++de:json:html

Examples


+en-json:html

Print json

Deprecated: use +en:json:html instead.

Encode +val, a $json structure, in a $tape.

Accepts

+val is any $json.

Produces

A $tape.

Source

Examples


+de-json:html

Parse json

Deprecated: use +de:json:html instead.

Parse cord .a to a $json structure. The result is wrapped in a unit which is null if parsing failed.

Accepts

.a is a $cord in which JSON is encoded.

Produces

A (unit json). The +unit is ~ if parsing failed.

Source

Examples

Last updated