commit 53ab193b198365b54bd1301fc0c9e1d7b1608433
parent a51f33512a0740b9e2a81bc75abb6aec7481d42d
Author: Sebastian <sebastian@sebsite.pw>
Date: Wed, 18 May 2022 23:41:13 -0400
encoding::json: add loadstr
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
1 file changed, 12 insertions(+), 0 deletions(-)
diff --git a/encoding/json/load.ha b/encoding/json/load.ha
@@ -1,3 +1,4 @@
+use bufio;
use io;
use strings;
use types;
@@ -28,6 +29,17 @@ export fn load(src: io::handle, opts: load_option...) (value | error) = {
return _load(&lex, 0, limit);
};
+// Parses a JSON value from the given string, returning the value or an error.
+// The return value is allocated on the heap; use [[finish]] to free it up when
+// you're done using it.
+//
+// See the documentation for [[load]] for information on dealing with
+// potentially malicious inputs.
+export fn loadstr(input: str, opts: load_option...) (value | error) = {
+ let src = bufio::fixed(strings::toutf8(input), io::mode::READ);
+ return load(&src, opts...);
+};
+
fn _load(lexer: *lexer, level: uint, limit: uint) (value | error) = {
const tok = mustscan(lexer)?;
match (tok) {