From 255f8ef5be85c211f95932eff821205070a1b22a Mon Sep 17 00:00:00 2001 From: Nicolas Williams Date: Fri, 29 Nov 2013 16:16:52 -0600 Subject: Add tojson and fromjson builtins --- builtin.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'builtin.c') diff --git a/builtin.c b/builtin.c index 07682266..0bdc3320 100644 --- a/builtin.c +++ b/builtin.c @@ -226,6 +226,19 @@ static jv f_contains(jv a, jv b) { } } +static jv f_dump(jv input) { + return jv_dump_string(input, 0); +} + +static jv f_json_parse(jv input) { + if (jv_get_kind(input) != JV_KIND_STRING) + return type_error(input, "only strings can be parsed"); + jv res = jv_parse_sized(jv_string_value(input), + jv_string_length_bytes(jv_copy(input))); + jv_free(input); + return res; +} + static jv f_tonumber(jv input) { if (jv_get_kind(input) == JV_KIND_NUMBER) { return input; @@ -536,6 +549,8 @@ static const struct cfunction function_list[] = { {(cfunction_ptr)f_multiply, "_multiply", 3}, {(cfunction_ptr)f_divide, "_divide", 3}, {(cfunction_ptr)f_mod, "_mod", 3}, + {(cfunction_ptr)f_dump, "tojson", 1}, + {(cfunction_ptr)f_json_parse, "fromjson", 1}, {(cfunction_ptr)f_tonumber, "tonumber", 1}, {(cfunction_ptr)f_tostring, "tostring", 1}, {(cfunction_ptr)f_keys, "keys", 1}, -- cgit v1.2.3