Search Results
6/11/2025, 6:37:20 AM
I'm using a JSON library in C (yajl) to parse some GLTF (not important), and the library operates by using a callback every time it processes a token.
static int gltf_double(void *userdata, double val)
{
// ...
return 1;
}
static const yajl_callbacks gltf_callbacks = {
.yajl_null = gltf_null,
.yajl_boolean = gltf_boolean,
.yajl_integer = gltf_integer,
.yajl_double = gltf_double,
.yajl_number = NULL,
.yajl_string = gltf_string,
.yajl_start_map = gltf_start_map,
.yajl_map_key = gltf_map_key,
.yajl_end_map = gltf_end_map,
.yajl_start_array = gltf_start_array,
.yajl_end_array = gltf_end_array,
};
yajl_handle yajl = yajl_alloc(&gltf_callbacks, NULL, &state);
I was starting to write a somewhat bloated state machine to handle all of the keys I was expecting, but then it occurred to me, why don't I just fuck with the callback table itself instead?
If I encounter the "scene" key, I change the integer callback to a function that writes the value somewhere (and the others to error out) then restore the table to what it was before.
Is there anything wrong with doing it this way? Is this how people actually use these kinds of libraries?
static int gltf_double(void *userdata, double val)
{
// ...
return 1;
}
static const yajl_callbacks gltf_callbacks = {
.yajl_null = gltf_null,
.yajl_boolean = gltf_boolean,
.yajl_integer = gltf_integer,
.yajl_double = gltf_double,
.yajl_number = NULL,
.yajl_string = gltf_string,
.yajl_start_map = gltf_start_map,
.yajl_map_key = gltf_map_key,
.yajl_end_map = gltf_end_map,
.yajl_start_array = gltf_start_array,
.yajl_end_array = gltf_end_array,
};
yajl_handle yajl = yajl_alloc(&gltf_callbacks, NULL, &state);
I was starting to write a somewhat bloated state machine to handle all of the keys I was expecting, but then it occurred to me, why don't I just fuck with the callback table itself instead?
If I encounter the "scene" key, I change the integer callback to a function that writes the value somewhere (and the others to error out) then restore the table to what it was before.
Is there anything wrong with doing it this way? Is this how people actually use these kinds of libraries?
Page 1