59#include <json-c/json.h>
90 enum json_type t = json_object_get_type(j);
94 case json_type_boolean:
95 return make_bool(json_object_get_boolean(j));
96 case json_type_double:
99 return make_int((int64_t)json_object_get_int64(j));
100 case json_type_string:
102 case json_type_array: {
103 size_t n = json_object_array_length(j);
109 for (
size_t i = 0; i < n; ++i) {
110 json_object *item = json_object_array_get_idx(j, (
int)i);
114 for (
size_t i = 0; i < n; ++i)
119 case json_type_object: {
121 json_object_object_foreach(j, key, val) {
162 return json_object_new_null();
164 return json_object_new_boolean(v->
i ? 1 : 0);
166 return json_object_new_int64(v->
i);
168 return json_object_new_double(v->
d);
170 return json_object_new_string(v->
s ? v->
s :
"");
172 json_object *arr = json_object_new_array();
174 for (
int i = 0; i < n; ++i) {
180 json_object_array_add(arr, json_object_new_null());
186 json_object *obj = json_object_new_object();
190 for (
int i = 0; i < kn; ++i) {
199 json_object_object_add(obj, k.
s, json_object_new_null());
209 return json_object_new_string(
"<unsupported>");
static json_object * fun_to_json(const Value *v)
Convert a Fun Value into a newly allocated json-c object tree.
static Value json_to_fun(json_object *j)
Convert a json-c object tree into a Fun Value.
int map_set(Value *vm, const char *key, Value v)
Insert or replace a key in the map.
Value make_map_empty(void)
Construct a new empty map Value.
int map_get_copy(const Value *vm, const char *key, Value *out)
Look up a key and copy the stored value into out.
Value map_keys_array(const Value *vm)
Return all map keys as an array of strings.
Tagged union representing a Fun value.
Value make_bool(int v)
Construct a boolean Value.
Value make_nil(void)
Construct a nil Value.
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
int array_length(const Value *v)
Get the element count of an array Value.
void free_value(Value v)
Free dynamic storage owned by a Value.
int array_get_copy(const Value *v, int index, Value *out)
Copy an array element into out.
Value make_float(double v)
Construct a Value representing a double-precision float.
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Value make_array_from_values(const Value *vals, int count)
Create an array Value by copying items from an input span.