json_object* json_object_new_double_with_precision(double dfVal, int nCoordPrecision) { json_object* jso = json_object_new_double(dfVal); json_object_set_serializer(jso, OGR_json_double_with_precision_to_string, (void*)(size_t)nCoordPrecision, NULL ); return jso; }
json_object* json_object_new_gme_double(double dfVal) { json_object* pjoD = json_object_new_double(dfVal); json_object_set_serializer(pjoD, json_gme_double_to_string, NULL, NULL ); return pjoD; }
static struct json_object * get_rootfs_usage(void) { struct statfs s; if (statfs("/", &s)) return NULL; struct json_object *jso = json_object_new_double(1 - (double)s.f_bfree / s.f_blocks); json_object_set_serializer(jso, json_object_double_to_json_string, "%.4f", NULL); return jso; }
static void add_uptime(struct json_object *obj) { FILE *f = fopen("/proc/uptime", "r"); struct json_object* jso; if (!f) return; double uptime, idletime; if (fscanf(f, "%lf %lf", &uptime, &idletime) == 2) { jso = json_object_new_double(uptime); json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL); json_object_object_add(obj, "uptime", jso); jso = json_object_new_double(idletime); json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL); json_object_object_add(obj, "idletime", jso); } fclose(f); }
int main(int argc, char **argv) { json_object *my_object; MC_SET_DEBUG(1); printf("Test setting, then resetting a custom serializer:\n"); my_object = json_object_new_object(); json_object_object_add(my_object, "abc", json_object_new_int(12)); json_object_object_add(my_object, "foo", json_object_new_string("bar")); printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object)); struct myinfo userdata = { .value = 123 }; json_object_set_serializer(my_object, custom_serializer, &userdata, freeit); printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object)); printf("Next line of output should be from the custom freeit function:\n"); freeit_was_called = 0; json_object_set_serializer(my_object, NULL, NULL, NULL); assert(freeit_was_called); printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object)); json_object_put(my_object); // ============================================ my_object = json_object_new_object(); printf("Check that the custom serializer isn't free'd until the last json_object_put:\n"); json_object_set_serializer(my_object, custom_serializer, &userdata, freeit); json_object_get(my_object); json_object_put(my_object); printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object)); printf("Next line of output should be from the custom freeit function:\n"); freeit_was_called = 0; json_object_put(my_object); assert(freeit_was_called); return 0; }
static void add_loadavg(struct json_object *obj) { FILE *f = fopen("/proc/loadavg", "r"); if (!f) return; double loadavg; unsigned proc_running, proc_total; if (fscanf(f, "%lf %*f %*f %u/%u", &loadavg, &proc_running, &proc_total) == 3) { struct json_object *jso = json_object_new_double(loadavg); json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL); json_object_object_add(obj, "loadavg", jso); struct json_object *processes = json_object_new_object(); json_object_object_add(processes, "running", json_object_new_int(proc_running)); json_object_object_add(processes, "total", json_object_new_int(proc_total)); json_object_object_add(obj, "processes", processes); } fclose(f); }