~/bend-docscommunity

json.bend relies on unsafe/foreign

raw source on the hub · import 0xe7b62cfd70190f15c1a2ba2201be65b0/json.bend as Json

1 import
import Base

Types

type Val source · line 9 · raw

Data

Num keeps the number's exact text, so no precision is lost.

type Next source · line 17 · raw

Data

type Open source · line 22 · raw

Data

An open container: array items so far (reversed), or object fields and the key whose value comes next.

type St source · line 26 · raw

Data

type Esc source · line 132 · raw

Data

type Item source · line 452 · raw

Data

Definitions

def is_ws source · line 38 · raw

@+c:U32 -> Bool

def ws.go source · line 43 · 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 58 · raw

@s:String -> String

def digit source · line 71 · raw

@+c:U32 -> Bool

def num.trans source · line 74 · raw

@+st:U32 -> @+c:U32 -> U32

def num.accepts source · line 87 · raw

@+st:U32 -> Bool

def num.end source · line 90 · raw

@+st:U32 -> @acc:String -> @rest:String -> Maybe<&2, Next>

def num.go source · line 93 · raw

@t:String -> @+c:U32 -> @+st:U32 -> @acc:String -> @stop:Bool -> Maybe<&2, Next>

def num source · line 109 · raw

@s:String -> Maybe<&2, Next>

def lit.if source · line 119 · raw

@+s:String -> @v:Val -> @n:Nat -> @ok:Bool -> Maybe<&2, Next>

def lit source · line 126 · raw

@+s:String -> @word:String -> @v:Val -> @+n:Nat -> Maybe<&2, Next>

def hexd source · line 135 · raw

@+c:U32 -> U32

def hex4.go source · line 138 · raw

@s:String -> @+acc:U32 -> @n:Nat -> Maybe<&2, Esc>

def hex4 source · line 154 · raw

@s:String -> Maybe<&2, Esc>

def is_hi source · line 157 · raw

@+v:U32 -> Bool

def is_lo source · line 160 · raw

@+v:U32 -> Bool

def pair.lo source · line 163 · raw

@+hi:U32 -> @+rest:String -> @lo:Maybe<&2, Esc> -> Esc

def pair source · line 171 · 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 174 · raw

@e:Maybe<&2, Esc> -> Maybe<&2, Esc>

def esc.simple source · line 181 · raw

@+e:U32 -> U32

def esc.one source · line 187 · raw

@t:String -> Maybe<&2, Esc>

One escape after the backslash.

def str.esc source · line 195 · raw

@e:Maybe<&2, Esc> -> @acc:String -> @key:Bool -> @stack:List<&2, Open> -> St

def str.close source · line 202 · raw

@+acc:String -> @t:String -> @key:Bool -> @stack:List<&2, Open> -> St

def str.c source · line 209 · 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 225 · 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 235 · raw

@m:Maybe<&2, Next> -> @stack:List<&2, Open> -> St

def open.close source · line 242 · raw

@r:String -> @close:Val -> @stack:List<&2, Open> -> @empty:Bool -> @open:Open -> St

def value.step source · line 253 · raw

@+s:String -> @+c:U32 -> @+t:String -> @+stack:List<&2, Open> -> St

def value source · line 263 · raw

@s:String -> @stack:List<&2, Open> -> St

def key source · line 270 · raw

@s:String -> @stack:List<&2, Open> -> St

def colon.top source · line 277 · raw

@k:String -> @r:String -> @stack:List<&2, Open> -> St

def colon source · line 286 · raw

@k:String -> @s:String -> @stack:List<&2, Open> -> St

def after.arr.c source · line 294 · 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 305 · raw

@xs:List<&2, Val> -> @r:String -> @up:List<&2, Open> -> St

def after.obj.c source · line 312 · raw

@comma:Bool -> @close:Bool -> @t:String -> @m:Map<&2, Val> -> @up:List<&2, Open> -> St

def after.obj source · line 323 · raw

@m:Map<&2, Val> -> @r:String -> @up:List<&2, Open> -> St

def after source · line 330 · raw

@v:Val -> @s:String -> @stack:List<&2, Open> -> St

def step source · line 339 · raw

@st:St -> St

def done source · line 356 · raw

@v:Val -> @rest:String -> Maybe<&2, Val>

def run source · line 364 · raw

@fuel:Nat -> @st:St -> Maybe<&2, Val>

fuel: every step but the last eats at least one char.

def parse source · line 386 · raw

@+s:String -> Maybe<&2, Val>

One JSON text: a value with optional whitespace around it.

def get.hit source · line 392 · raw

@r:Pair(Map<&2, Val>, Bool) -> Bool

def get.some source · line 396 · raw

@r:Pair(Map<&2, Val>, Val) -> Maybe<&2, Val>

def get.has source · line 400 · raw

@+m:Map<&2, Val> -> @+k:String -> @hit:Bool -> Maybe<&2, Val>

def get.of source · line 407 · raw

@+m:Map<&2, Val> -> @+k:String -> Maybe<&2, Val>

def get source · line 411 · raw

@v:Val -> @k:String -> Maybe<&2, Val>

The value under key k of an object.

def hexc source · line 429 · raw

@+n:U32 -> Char

def esc.put source · line 433 · 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 442 · raw

@s:String -> @acc:String -> String

def esc source · line 449 · raw

@s:String -> String

def items.sep source · line 456 · raw

@first:Bool -> @tail:List<&2, Item> -> List<&2, Item>

def items.arr source · line 463 · raw

@xs:List<&2, Val> -> @first:Bool -> @rest:List<&2, Item> -> List<&2, Item>

def items.obj source · line 470 · raw

@kvs:List<&2, Sigma<&2, &2, String, _ => Val>> -> @first:Bool -> @rest:List<&2, Item> -> List<&2, Item>

def put source · line 477 · raw

@s:String -> @racc:String -> String

def encode source · line 505 · raw

@v:Val -> String

Compact JSON; object keys come out sorted.

Unsafe

unsafe enc.go source · line 483 · 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.