diff options
author | Nicolas Williams <nico@cryptonector.com> | 2013-11-29 16:16:52 -0600 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2013-12-04 18:21:40 -0600 |
commit | 255f8ef5be85c211f95932eff821205070a1b22a (patch) | |
tree | ae1c27ef2a62db04e4964ec5b02bb40fb78e6c86 /builtin.c | |
parent | 20b32db5af37c322e3116cbc646b8b60974460e6 (diff) |
Add tojson and fromjson builtins
Diffstat (limited to 'builtin.c')
-rw-r--r-- | builtin.c | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -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}, |