json/json.bend relies on unsafe/foreign
raw source on the hub · import 0xbf477e663cf4acb1369a68e0f0fa713b/json/json.bend as Json
JSON values, parsed and encoded as RFC 8259.
1 import
import Base
Types
type Val source · line 10 · raw
Data
Num keeps the number's exact text, so no precision is lost.
NullVal
Flag@on:Bool -> Val
Num@s:String -> Val
Str@s:String -> Val
Arr@xs:List<&2, Val> -> Val
Obj@m:Map<&2, Val> -> Val
type Next source · line 18 · raw
Data
Next@v:Val -> @rest:String -> Next
type Open source · line 23 · raw
Data
An open container: array items so far (reversed), or object fields and the key whose value comes next.
OArr@xs:List<&2, Val> -> Open
OObj@m:Map<&2, Val> -> @key:String -> Open
type St source · line 27 · raw
Data
SValue@s:String -> @stack:List<&2, Open> -> St
SKey@s:String -> @stack:List<&2, Open> -> St
SColon@key:String -> @s:String -> @stack:List<&2, Open> -> St
SStr@s:String -> @acc:String -> @key:Bool -> @stack:List<&2, Open> -> St
SAfter@v:Val -> @s:String -> @stack:List<&2, Open> -> St
SDone@v:Val -> @s:String -> St
SFailSt
type Esc source · line 133 · raw
Data
Esc@cp:U32 -> @rest:String -> Esc
type Item source · line 453 · raw
Data
ILit@s:String -> Item
IVal@v:Val -> Item
Definitions
def is_ws source · line 39 · raw
@+c:U32 -> Bool
def ws.go source · line 44 · raw
@t:String -> @+c:U32 -> @ws:Bool -> String
Bool.pick runs both arms, so loops peek: the next char's class is a Bool param and only the matching arm recurses.
def skip_ws source · line 59 · raw
@s:String -> String
def digit source · line 72 · raw
@+c:U32 -> Bool
def num.trans source · line 75 · raw
@+st:U32 -> @+c:U32 -> U32
def num.accepts source · line 88 · raw
@+st:U32 -> Bool
def num.end source · line 91 · raw
@+st:U32 -> @acc:String -> @rest:String -> Maybe<&2, Next>
def num.go source · line 94 · raw
@t:String -> @+c:U32 -> @+st:U32 -> @acc:String -> @stop:Bool -> Maybe<&2, Next>
def num source · line 110 · raw
@s:String -> Maybe<&2, Next>
def lit.if source · line 120 · raw
@+s:String -> @v:Val -> @n:Nat -> @ok:Bool -> Maybe<&2, Next>
def lit source · line 127 · raw
@+s:String -> @word:String -> @v:Val -> @+n:Nat -> Maybe<&2, Next>
def hexd source · line 136 · raw
@+c:U32 -> U32
def hex4.go source · line 139 · raw
@s:String -> @+acc:U32 -> @n:Nat -> Maybe<&2, Esc>
def hex4 source · line 155 · raw
@s:String -> Maybe<&2, Esc>
def is_hi source · line 158 · raw
@+v:U32 -> Bool
def is_lo source · line 161 · raw
@+v:U32 -> Bool
def pair.lo source · line 164 · raw
@+hi:U32 -> @+rest:String -> @lo:Maybe<&2, Esc> -> Esc
def pair source · line 172 · raw
@+hi:U32 -> @+rest:String -> Esc
§8.2: a high surrogate pairs with a following \uDC00-\uDFFF; a lone one is U+FFFD.
def esc.u source · line 175 · raw
@e:Maybe<&2, Esc> -> Maybe<&2, Esc>
def esc.simple source · line 182 · raw
@+e:U32 -> U32
def esc.one source · line 188 · raw
@t:String -> Maybe<&2, Esc>
One escape after the backslash.
def str.esc source · line 196 · raw
@e:Maybe<&2, Esc> -> @acc:String -> @key:Bool -> @stack:List<&2, Open> -> St
def str.close source · line 203 · raw
@+acc:String -> @t:String -> @key:Bool -> @stack:List<&2, Open> -> St
def str.c source · line 210 · raw
@quote:Bool -> @ctl:Bool -> @bs:Bool -> @c:U32 -> @t:String -> @acc:String -> @key:Bool -> @stack:List<&2, Open> -> St
def str.step source · line 226 · raw
@s:String -> @acc:String -> @key:Bool -> @stack:List<&2, Open> -> St
§7: unescaped chars below U+0020 are not allowed in a string.
def got source · line 236 · raw
@m:Maybe<&2, Next> -> @stack:List<&2, Open> -> St
def open.close source · line 243 · raw
@r:String -> @close:Val -> @stack:List<&2, Open> -> @empty:Bool -> @open:Open -> St
def value.step source · line 254 · raw
@+s:String -> @+c:U32 -> @+t:String -> @+stack:List<&2, Open> -> St
def value source · line 264 · raw
@s:String -> @stack:List<&2, Open> -> St
def key source · line 271 · raw
@s:String -> @stack:List<&2, Open> -> St
def colon.top source · line 278 · raw
@k:String -> @r:String -> @stack:List<&2, Open> -> St
def colon source · line 287 · raw
@k:String -> @s:String -> @stack:List<&2, Open> -> St
def after.arr.c source · line 295 · raw
@comma:Bool -> @close:Bool -> @t:String -> @xs:List<&2, Val> -> @up:List<&2, Open> -> St
A finished value joins its container; then "," or the closing bracket.
def after.arr source · line 306 · raw
@xs:List<&2, Val> -> @r:String -> @up:List<&2, Open> -> St
def after.obj.c source · line 313 · raw
@comma:Bool -> @close:Bool -> @t:String -> @m:Map<&2, Val> -> @up:List<&2, Open> -> St
def after.obj source · line 324 · raw
@m:Map<&2, Val> -> @r:String -> @up:List<&2, Open> -> St
def after source · line 331 · raw
@v:Val -> @s:String -> @stack:List<&2, Open> -> St
def step source · line 340 · raw
@st:St -> St
def done source · line 357 · raw
@v:Val -> @rest:String -> Maybe<&2, Val>
def run source · line 365 · raw
@fuel:Nat -> @st:St -> Maybe<&2, Val>
fuel: every step but the last eats at least one char.
def parse source · line 387 · raw
@+s:String -> Maybe<&2, Val>
One JSON text: a value with optional whitespace around it.
def get.hit source · line 393 · raw
@r:Pair(Map<&2, Val>, Bool) -> Bool
def get.some source · line 397 · raw
@r:Pair(Map<&2, Val>, Val) -> Maybe<&2, Val>
def get.has source · line 401 · raw
@+m:Map<&2, Val> -> @+k:String -> @hit:Bool -> Maybe<&2, Val>
def get.of source · line 408 · raw
@+m:Map<&2, Val> -> @+k:String -> Maybe<&2, Val>
def get source · line 412 · raw
@v:Val -> @k:String -> Maybe<&2, Val>
The value under key k of an object.
def hexc source · line 430 · raw
@+n:U32 -> Char
def esc.put source · line 434 · raw
@+c:U32 -> @+acc:String -> String
The escaped form of one char, reversed (it is pushed onto a reversed acc).
def esc.go source · line 443 · raw
@s:String -> @acc:String -> String
def esc source · line 450 · raw
@s:String -> String
def items.sep source · line 457 · raw
@first:Bool -> @tail:List<&2, Item> -> List<&2, Item>
def items.arr source · line 464 · raw
@xs:List<&2, Val> -> @first:Bool -> @rest:List<&2, Item> -> List<&2, Item>
def items.obj source · line 471 · raw
@kvs:List<&2, Sigma<&2, &2, String, _ => Val>> -> @first:Bool -> @rest:List<&2, Item> -> List<&2, Item>
def put source · line 478 · raw
@s:String -> @racc:String -> String
def encode source · line 506 · raw
@v:Val -> String
Compact JSON; object keys come out sorted.
def at.go source · line 509 · raw
@xs:List<&2, Val> -> @n:Nat -> Maybe<&2, Val>
def at source · line 521 · raw
@v:Val -> @n:Nat -> Maybe<&2, Val>
def u32.over source · line 536 · raw
@+acc:U32 -> @+c:U32 -> Bool
def u32.dig source · line 539 · raw
@s:String -> @+acc:U32 -> @bad:Bool -> Maybe<&2, U32>
def u32.zero source · line 550 · raw
@t:String -> Maybe<&2, U32>
def u32.lead source · line 557 · raw
@+c:U32 -> @t:String -> @zero:Bool -> Maybe<&2, U32>
def u32.num source · line 564 · raw
@s:String -> Maybe<&2, U32>
def u32 source · line 571 · raw
@v:Val -> Maybe<&2, U32>
Unsafe
unsafe enc.go source · line 484 · raw
@items:List<&2, Item> -> @racc:String -> String
ponytail: @unsafe because it walks a work list, not the Val; each step emits text or swaps a container for its strictly smaller parts.