~/bend-docscommunity

main.bend checks

raw source on the hub · import emerging-ezjson@1.3.0.0/main.bend as Main

ezjson: JSON for Bend 2, with a parser, a printer and a pull cursor

parse reads text into Maybe Json. print writes compact text and pretty indented text. cursor reads one event at a time. Build null, booleans, numbers, strings, arrays, and objects; read them with get, at, and as_bool, as_str, as_num, as_u32, as_f32; has and len tell a missing key or index from a null; items lists an array's elements, and is_arr and is_obj tell the containers apart.

6 imports
import Base
import ./src/value.bend as Value
import ./src/parse.bend as Parse
import ./src/print.bend as Print
import ./src/pull.bend as Pull
import ./src/fixed.bend as Fixed

Definitions

def parse source · line 17 · raw

@txt:String -> Maybe<&2, 0xdab377b48b4edd8247427c10e2120805/src/value.Json>

a text as a value, or None when it is not JSON

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> String

a value as compact JSON

def pretty source · line 26 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> String

a value as JSON indented two spaces a level, one element or member a line, with no newline at the end

def null source · line 30 · raw

0xdab377b48b4edd8247427c10e2120805/src/value.Json

null

def bool source · line 34 · raw

@flag:Bool -> 0xdab377b48b4edd8247427c10e2120805/src/value.Json

a boolean

def str source · line 38 · raw

@txt:String -> 0xdab377b48b4edd8247427c10e2120805/src/value.Json

a string

def num source · line 42 · raw

@raw:String -> 0xdab377b48b4edd8247427c10e2120805/src/value.Json

a JSON number from its text, or null when that text is not one

def obj source · line 46 · raw

@pairs:List<&1, Pair(String, 0xdab377b48b4edd8247427c10e2120805/src/value.Json)> -> 0xdab377b48b4edd8247427c10e2120805/src/value.Json

an object from pairs

def arr source · line 50 · raw

@items:List<&2, 0xdab377b48b4edd8247427c10e2120805/src/value.Json> -> 0xdab377b48b4edd8247427c10e2120805/src/value.Json

an array from a list

def get source · line 55 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> @key:String -> 0xdab377b48b4edd8247427c10e2120805/src/value.Json

an object's value at a key; JNull when it is not an object or has no such key

def at source · line 59 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> @idx:U32 -> 0xdab377b48b4edd8247427c10e2120805/src/value.Json

an array's item at an index; JNull when it is not an array or has none

def has source · line 64 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> @key:String -> Bool

whether an object has a key; false when the value is not an object. Tells a missing key from a key whose value is null, which get does not

def len source · line 68 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> U32

how many elements an array, or members an object, has; 0 for anything else

def items source · line 73 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> List<&2, 0xdab377b48b4edd8247427c10e2120805/src/value.Json>

an array's elements, in order; [] when the value is not an array. One walk of the array, where a loop over at walks it again for every index

def is_arr source · line 77 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> Bool

whether a value is an array

def is_obj source · line 81 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> Bool

whether a value is an object

def as_bool source · line 85 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> Maybe<&2, Bool>

a boolean, or none when the value is not one

def as_str source · line 89 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> Maybe<&2, String>

a string, or none when the value is not one

def as_num source · line 93 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> Maybe<&2, String>

a number's text, or none when the value is not a number

def num.fixed source · line 101 · raw

@raw:String -> @places:Nat -> Maybe<&2, String>

a number's text written with places decimals, rounded half to even on its decimal digits (not through a binary float), as Python's '%.Nf' writes a value whose decimal text is exact; None when the text is not a JSON number. A - is kept even when the result is zero: -0.04 to one place is -0.0

def as_u32 source · line 105 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> Maybe<&2, U32>

a number as a U32, or none when it is not one or does not fit

def as_f32 source · line 109 · raw

@val:0xdab377b48b4edd8247427c10e2120805/src/value.Json -> Maybe<&2, F32>

a number as an F32, or none when it is not one or does not fit

def cursor source · line 115 · raw

@src:String -> 0xdab377b48b4edd8247427c10e2120805/src/pull.Cur

a cursor at the start of a JSON text. It holds the unread suffix of that text. A span event keeps its own suffix, so it stays valid after next moves the cursor, and it is gone when the event is dropped

def next source · line 120 · raw

@cur:0xdab377b48b4edd8247427c10e2120805/src/pull.Cur -> Pair(0xdab377b48b4edd8247427c10e2120805/src/pull.Ev, 0xdab377b48b4edd8247427c10e2120805/src/pull.Cur)

the next event. Commas and colons are not events. After the root value, the next call is the end, or an error if another value follows

def skip source · line 124 · raw

@cur:0xdab377b48b4edd8247427c10e2120805/src/pull.Cur -> 0xdab377b48b4edd8247427c10e2120805/src/pull.Cur

drop the next value, scalar or container, without building it

def text source · line 128 · raw

@ev:0xdab377b48b4edd8247427c10e2120805/src/pull.Ev -> Maybe<&2, String>

the text of a string, key, or number event, copying a span. None otherwise