std.json.ObjectParser
class pub ObjectParserA type for parsing an object with a known set of keys.
Fields
parser
let pub @parser: mut PullParserThe parser this ObjectParser instance uses to process input.
Instance methods
bool
Show source codeHide source code
fn pub mut bool(name: String, value: fn (Bool)) -> mut ObjectParser {
key(name, fn move { @parser.bool.map(fn (v) { value.call(v) }) })
}fn pub mut bool(name: String, value: fn (Bool)) -> mut ObjectParserParses the key name as a Bool, passing the parsed value to the value
closure.
float
Show source codeHide source code
fn pub mut float(name: String, value: fn (Float)) -> mut ObjectParser {
key(name, fn move { @parser.float.map(fn (v) { value.call(v) }) })
}fn pub mut float(name: String, value: fn (Float)) -> mut ObjectParserParses the key name as a Float, passing the parsed value to the value
closure.
floats
Show source codeHide source code
fn pub mut floats(name: String, value: fn (Float)) -> mut ObjectParser {
values(name, fn move { @parser.float.map(fn (v) { value.call(v) }) })
}fn pub mut floats(name: String, value: fn (Float)) -> mut ObjectParserParses the key name as an array of integers, passing each Int to the
value closure.
int
Show source codeHide source code
fn pub mut int(name: String, value: fn (Int)) -> mut ObjectParser {
key(name, fn move { @parser.int.map(fn (v) { value.call(v) }) })
}fn pub mut int(name: String, value: fn (Int)) -> mut ObjectParserParses the key name as an Int, passing the parsed value to the value
closure.
ints
Show source codeHide source code
fn pub mut ints(name: String, value: fn (Int)) -> mut ObjectParser {
values(name, fn move { @parser.int.map(fn (v) { value.call(v) }) })
}fn pub mut ints(name: String, value: fn (Int)) -> mut ObjectParserParses the key name as an array of integers, passing each Int to the
value closure.
key
Show source codeHide source code
fn pub mut key(
name: String,
value: fn -> Result[Nil, Error],
) -> mut ObjectParser {
@keys.set(name, value)
self
}fn pub mut key(name: String, value: fn -> Result[Nil, Error]) -> mut ObjectParserParses the key name as a custom value.
It's expected that the value closure parses the value, advancing the
parser in the process.
keys
Show source codeHide source code
fn pub mut keys(
name: String,
value: fn (String) -> Result[Nil, Error],
) -> mut ObjectParser {
key(name, fn move { @parser.keys(fn (key) { value.call(key) }) })
}fn pub mut keys(name: String, value: fn (String) -> Result[Nil, Error]) -> mut ObjectParserParses the key name as an object without a known structure, passing each
key to the value closure. It's expected that the value closure parses
the value, advancing the parser in the process.
parse
Show source codeHide source code
fn pub mut parse -> Result[Nil, Error] {
# We need to skip any leading whitespace such that the offset points to the
# "{" and not any whitespace before it.
# try @parser.skip_whitespace
#
# let start = @parser.offset
@parser.keys(fn (key) {
match @keys.opt_mut(key) {
case Some(func) -> func.call
case _ -> @parser.skip
}
})
}fn pub mut parse -> Result[Nil, Error]Parses the current object according to the rules in self.
When encountering a key for which no rule is defined, its value is (recursively) skipped.
require
Show source codeHide source code
fn pub mut require(keys: ref Array[String]) -> Result[Nil, Error] {
let mut found = Set.new
try @parser.keys(fn (key) {
match @keys.opt_mut(key) {
case Some(func) -> {
found.insert(key)
func.call
}
case _ -> @parser.skip
}
})
keys.iter.try_each(fn (key) {
if found.contains?(key) {
Result.Ok(nil)
} else {
let pos = @parser.offset
Result.Error(
Error(ErrorKind.Generic('the key "${key}" is required'), pos),
)
}
})
}fn pub mut require(keys: ref Array[String]) -> Result[Nil, Error]Parses the current object according to the rules in self, while requiring
the given keys to be present.
This method returns a Result.Error for the first missing key that it
encounters.
require_all
Show source codeHide source code
fn pub mut require_all -> Result[Nil, Error] {
require(@keys.keys.to_array)
}fn pub mut require_all -> Result[Nil, Error]Parses the current object according to the rules in self, and requires all
keys to be present.
See ObjectParser.require and ObjectParser.parse for more details.
string
Show source codeHide source code
fn pub mut string(name: String, value: fn (String)) -> mut ObjectParser {
key(name, fn move { @parser.string.map(fn (v) { value.call(v) }) })
}fn pub mut string(name: String, value: fn (String)) -> mut ObjectParserParses the key name as a String, passing the parsed value to the value
closure.
strings
Show source codeHide source code
fn pub mut strings(name: String, value: fn (String)) -> mut ObjectParser {
values(name, fn move { @parser.string.map(fn (v) { value.call(v) }) })
}fn pub mut strings(name: String, value: fn (String)) -> mut ObjectParserParses the key name as an array of strings, passing each String to the
value closure.
values
Show source codeHide source code
fn pub mut values(
name: String,
value: fn -> Result[Nil, Error],
) -> mut ObjectParser {
key(name, fn move { @parser.values(fn { value.call }) })
}fn pub mut values(name: String, value: fn -> Result[Nil, Error]) -> mut ObjectParserParses the key name as an array of arbitrary values, using the value
closure to parse the values and advance the parser.