~/bend-docscommunity

json.bend relies on unsafe/foreign

raw source on the hub · import bend-kit-json@0.5.0.0/json.bend as Json

JSON values, parsed and encoded as RFC 8259. Source: https://github.com/paymog/bend-kit/tree/main/json

2 imports
import Base
import 0x49814d83de8f70993a43e1002be29ecd/bytes.bend as Bytes

Types

type Val source · line 12 · raw

Type

Num keeps the number's exact text, so no precision is lost. Obj keeps its fields in document order, repeated keys included.

type Open source · line 22 · raw

Type

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

type W source · line 31 · raw

Type

Writer ------ Octets being written: the buffer, its size in words, the count so far, and the word being filled. Bytes enter at the top of w, as in Bytes.from_string.

type Sb source · line 165 · raw

Type

type Cp source · line 179 · raw

Data

type Item source · line 775 · raw

Type

Definitions

def w.room source · line 35 · raw

@grow:Bool -> @buf:Array<U32> -> @+k:U32 -> Array<U32>

The buffer doubles when word k does not fit.

def w.store source · line 42 · raw

@full:Bool -> @buf:Array<U32> -> @+cap:U32 -> @+n:U32 -> @+w:U32 -> W

def w.put source · line 51 · raw

@o:W -> @+b:U32 -> W

def w.shrink source · line 55 · raw

@fit:Bool -> @+n:U32 -> @buf:Array<U32> -> Array<U32>

def w.done source · line 63 · raw

@o:W -> 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes

Stores the last partial word, then copies into the fewest words, as Bytes requires.

def w.cont source · line 72 · raw

@+c:U32 -> @+k:Nat -> U32

def w.cp.of source · line 75 · raw

@+n:U32 -> @+c:U32 -> @o:W -> W

def w.cp source · line 87 · raw

@+c:U32 -> @o:W -> W

One code point in UTF-8.

def w.hex source · line 90 · raw

@+n:U32 -> U32

def w.new source · line 93 · raw

W

def w.of.at source · line 96 · raw

@+len:U32 -> @+cap:U32 -> @r:Pair(Array<U32>, U32) -> W

def w.of source · line 102 · raw

@b:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> W

A writer that appends to b.

def w.text source · line 106 · raw

@s:String -> @o:W -> W

def utf8 source · line 114 · raw

@s:String -> 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes

Text, one Char per code point, as UTF-8.

def digit source · line 123 · raw

@+c:U32 -> Bool

def num.trans source · line 126 · raw

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

def num.accepts source · line 139 · raw

@+st:U32 -> Bool

def hexd source · line 145 · raw

@+c:U32 -> U32

def is_hi source · line 148 · raw

@+v:U32 -> Bool

def is_lo source · line 151 · raw

@+v:U32 -> Bool

def esc.simple source · line 154 · raw

@+e:U32 -> U32

def pk.if source · line 182 · raw

@ok:Bool -> @a:Array<U32> -> @+i:U32 -> Pair(Array<U32>, U32)

def pk source · line 190 · raw

@a:Array<U32> -> @+len:U32 -> @+i:U32 -> Pair(Array<U32>, U32)

Byte i, or 256 at the end, which no rule accepts.

def word.go source · line 193 · raw

@w:String -> @ok:Bool -> @r:Pair(Array<U32>, U32) -> @+len:U32 -> @+j:U32 -> Pair(Array<U32>, Bool)

def word source · line 203 · raw

@a:Array<U32> -> @+len:U32 -> @w:String -> @+j:U32 -> Pair(Array<U32>, Bool)

Do the bytes from j spell w?

def hex4.b.go source · line 206 · raw

@n:Nat -> @r:Pair(Array<U32>, U32) -> @+len:U32 -> @+j:U32 -> @+acc:U32 -> Pair(Array<U32>, U32)

def hex4.b source · line 217 · raw

@a:Array<U32> -> @+len:U32 -> @+j:U32 -> Pair(Array<U32>, U32)

Four hex digits from byte j, or 65536 if one is not a hex digit.

def cont source · line 220 · raw

@+b:U32 -> Bool

def utf8.n source · line 224 · raw

@+b0:U32 -> @+b1:U32 -> @+b2:U32 -> @+b3:U32 -> Cp

RFC 3629 §4: overlong forms, surrogates, and code points past U+10FFFF are invalid, width 0.

def utf8.b3 source · line 236 · raw

@+b0:U32 -> @+b1:U32 -> @+b2:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Cp)

def utf8.b2 source · line 240 · raw

@+len:U32 -> @+i:U32 -> @+b0:U32 -> @+b1:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Cp)

def utf8.b1 source · line 244 · raw

@+len:U32 -> @+i:U32 -> @+b0:U32 -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Cp)

def utf8.at source · line 249 · raw

@a:Array<U32> -> @+len:U32 -> @+i:U32 -> @+b0:U32 -> Pair(Array<U32>, Cp)

The code point whose lead byte b0 is at i.

def slice.of source · line 252 · raw

@+n:U32 -> @r:Pair(Array<U32>, Array<U32>) -> Pair(Array<U32>, 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes)

def slice source · line 257 · raw

@+n:U32 -> @+start:U32 -> @a:Array<U32> -> Pair(Array<U32>, 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes)

The n bytes from start, copied.

def str.b.done source · line 260 · raw

@key:Bool -> @s:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> @+i:U32 -> @stack:List<&1, Open> -> Sb

def esc.b.lo.if source · line 267 · raw

@ok:Bool -> @+i:U32 -> @+hi:U32 -> @+lo:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> Sb

def esc.b.lo source · line 274 · raw

@+i:U32 -> @+hi:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Sb)

def esc.b.pair source · line 279 · raw

@+len:U32 -> @+i:U32 -> @+hi:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, Bool) -> Pair(Array<U32>, Sb)

§8.2: a high surrogate pairs with a following \uDC00-\uDFFF; a lone one is U+FFFD.

def u.class source · line 288 · raw

@+v:U32 -> U32

0: not four hex digits; 1: high surrogate; 2: low surrogate; 3: other.

def esc.b.u.of source · line 291 · raw

@+k:U32 -> @a:Array<U32> -> @+len:U32 -> @+i:U32 -> @+v:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def esc.b.u source · line 302 · raw

@+len:U32 -> @+i:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Sb)

def esc.b.one source · line 307 · raw

@+len:U32 -> @+i:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, U32) -> Pair(Array<U32>, Sb)

The escape whose backslash is at i.

def esc.b.cp source · line 316 · raw

@acc:W -> @key:Bool -> @stack:List<&1, Open> -> @+i:U32 -> @r:Pair(Array<U32>, Cp) -> Pair(Array<U32>, Sb)

def esc.b.other source · line 321 · raw

@ascii:Bool -> @+c:U32 -> @a:Array<U32> -> @+len:U32 -> @+i:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

§7: unescaped chars below U+0020 are not allowed in a string.

def esc.b source · line 329 · raw

@r:Pair(Array<U32>, U32) -> @+len:U32 -> @+i:U32 -> @acc:W -> @key:Bool -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

A string after its first escape.

def str.b.close source · line 341 · raw

@key:Bool -> @+i:U32 -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes) -> Pair(Array<U32>, Sb)

def str.b.esc source · line 345 · raw

@+len:U32 -> @+i:U32 -> @key:Bool -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes) -> Pair(Array<U32>, Sb)

def str.b.cp source · line 349 · raw

@+start:U32 -> @key:Bool -> @stack:List<&1, Open> -> @+i:U32 -> @r:Pair(Array<U32>, Cp) -> Pair(Array<U32>, Sb)

def str.b.other source · line 353 · raw

@ascii:Bool -> @+c:U32 -> @a:Array<U32> -> @+len:U32 -> @+i:U32 -> @+start:U32 -> @key:Bool -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def str.b source · line 361 · raw

@r:Pair(Array<U32>, U32) -> @+len:U32 -> @+i:U32 -> @+start:U32 -> @key:Bool -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

A string whose text so far starts at start and has no escape.

def num.b.fin source · line 373 · raw

@+i:U32 -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes) -> Pair(Array<U32>, Sb)

def num.b.stop source · line 377 · raw

@ok:Bool -> @a:Array<U32> -> @+i:U32 -> @+start:U32 -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def num.b.if source · line 384 · raw

@stop:Bool -> @a:Array<U32> -> @+i:U32 -> @+start:U32 -> @+st:U32 -> @+n:U32 -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def num.b source · line 391 · raw

@r:Pair(Array<U32>, U32) -> @+i:U32 -> @+start:U32 -> @+st:U32 -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def lit.b source · line 396 · raw

@+i:U32 -> @+n:U32 -> @v:Val -> @stack:List<&1, Open> -> @r:Pair(Array<U32>, Bool) -> Pair(Array<U32>, Sb)

def value.b source · line 404 · raw

@r:Pair(Array<U32>, U32) -> @+len:U32 -> @+i:U32 -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def first.b.end source · line 431 · raw

@open:Open -> @+c:U32 -> @+i:U32 -> @stack:List<&1, Open> -> Sb

After "[" or "{": the closing bracket, or the first item.

def first.b.open source · line 440 · raw

@open:Open -> @+i:U32 -> @stack:List<&1, Open> -> Sb

def first.b source · line 449 · raw

@r:Pair(Array<U32>, U32) -> @+i:U32 -> @open:Open -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def key.b source · line 467 · raw

@r:Pair(Array<U32>, U32) -> @+i:U32 -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def colon.b.top source · line 483 · raw

@k:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> @+i:U32 -> @stack:List<&1, Open> -> Sb

def colon.b source · line 494 · raw

@r:Pair(Array<U32>, U32) -> @k:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> @+i:U32 -> @stack:List<&1, Open> -> Pair(Array<U32>, Sb)

def after.b source · line 511 · raw

@v:Val -> @+i:U32 -> @stack:List<&1, Open> -> Sb

A finished value joins its container.

def sep.b.comma source · line 522 · raw

@open:Open -> @+i:U32 -> @up:List<&1, Open> -> Sb

def sep.b.close source · line 531 · raw

@open:Open -> @+c:U32 -> @+i:U32 -> @up:List<&1, Open> -> Sb

def sep.b source · line 541 · raw

@r:Pair(Array<U32>, U32) -> @+i:U32 -> @open:Open -> @up:List<&1, Open> -> Pair(Array<U32>, Sb)

After an item: "," or the closing bracket.

def end.b source · line 557 · raw

@r:Pair(Array<U32>, U32) -> @v:Val -> @+i:U32 -> Pair(Array<U32>, Sb)

def runb source · line 574 · raw

@fuel:Nat -> @r:Pair(Array<U32>, Sb) -> @+len:U32 -> Maybe<&1, Val>

fuel: a step eats a byte, or ends a number, a value, or an opening bracket, so 4 * len + 4 steps are enough.

def parse.bytes source · line 607 · raw

@b:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> Maybe<&1, Val>

One JSON text in UTF-8. None for bad JSON or bad UTF-8 (RFC 8259 §8.1).

def get.pick source · line 614 · raw

@v:Val -> @found:Maybe<&1, Val> -> @r:Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Bool)) -> Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Maybe<&1, Val>)

def get.step source · line 618 · raw

@k:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> @v:Val -> @st:Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Maybe<&1, Val>) -> Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Maybe<&1, Val>)

def get.go source · line 622 · raw

@kvs:List<&1, Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Val)> -> @st:Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Maybe<&1, Val>) -> Maybe<&1, Val>

def get source · line 631 · raw

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

§4: the value under key k of an object; the last one when k repeats, as most parsers do.

def at.go source · line 646 · raw

@xs:List<&1, Val> -> @n:Nat -> Maybe<&1, Val>

def at source · line 658 · raw

@v:Val -> @n:Nat -> Maybe<&1, Val>

Item n of an array.

def u32.over source · line 673 · raw

@+acc:U32 -> @+c:U32 -> Bool

def u32.dig source · line 676 · raw

@s:String -> @+acc:U32 -> @bad:Bool -> Maybe<&2, U32>

def u32.zero source · line 687 · raw

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

def u32.lead source · line 694 · raw

@+c:U32 -> @t:String -> @zero:Bool -> Maybe<&2, U32>

def u32.num source · line 701 · raw

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

def u32 source · line 709 · raw

@v:Val -> Maybe<&2, U32>

A number that is a whole U32, with no sign, fraction or exponent.

def w.ctl source · line 727 · raw

@ctl:Bool -> @+c:U32 -> @o:W -> W

def w.esc.c source · line 735 · raw

@+c:U32 -> @o:W -> W

§7: quote, backslash and control chars are escaped. Other bytes are already UTF-8.

def w.bytes.go source · line 750 · raw

@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @o:W -> W

def w.bytes source · line 758 · raw

@b:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> @o:W -> W

def w.str.go source · line 762 · raw

@n:Nat -> @r:Pair(Array<U32>, U32) -> @+i:U32 -> @o:W -> W

def w.str source · line 771 · raw

@b:0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes -> @o:W -> W

A quoted, escaped string.

def items.sep source · line 780 · raw

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

def items.arr source · line 787 · raw

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

def items.obj source · line 794 · raw

@kvs:List<&1, Pair(0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes, Val)> -> @first:Bool -> @rest:List<&1, Item> -> List<&1, Item>

def encode.bytes source · line 828 · raw

@v:Val -> 0x49814d83de8f70993a43e1002be29ecd/bytes.Bytes

Compact JSON in UTF-8, with object fields in their order.

Unsafe

unsafe encb.go source · line 804 · raw

@items:List<&1, Item> -> @o:W -> W

ponytail: @unsafe because it walks a work list, not the Val; each step emits text or swaps a container for its strictly smaller parts.