New option -j / --json for the JSON output in the functions print_per_system_stats() and print_global_stats()#379
New option -j / --json for the JSON output in the functions print_per_system_stats() and print_global_stats()#379gsnw-sebast wants to merge 1 commit intoschweikert:developfrom
Conversation
|
Was put on draft because the test is still missing and I wanted to wait for feedback first |
|
Tested and confirmed working. Tested various ways, seems to all work the way we need it to. This binary is actually going into production today, with the hopes this will be included in main/master in the future. My C is a little rusty these days but I'm happy to help with this if I can. The only thing I can suggest is adding a \n at the end of the payload because it does this There's also the question of whether you bother pretty printing it or just leaving it condensed (less characters to decode, technically more efficient). Otherwise, perfect fit for us. |
|
We may have an issue here: fping --json -C 10 -p 1000 -t 2000 -q 8.8.8.8 1.1.1.1returns this: However when I try to parse it: Perhaps you should be returning: This parses as valid JSON and is basically handled the same way, foreach/for/etc over the count object. |
|
I've attached 3 diff's showing the changes I've made to produce a value JSON object. fping.c.diff.txt New output looks like this: |
|
Thanks a lot for this! Before we merge and before further work on the implementation is done, I think that there should be a design and discussion of the json schema. This was already a discussion topic in the previous PR that attempted to implement JSON support. Since this is going to be a sort of API, I want to get it right the first time. In particular what comes to mind:
Can you create maybe a (separate) PR with a markdown file that explains the JSON format? We can then discuss there. |
I have created a discussion #381 |
bde460c to
7523994
Compare
…_system_stats() and print_global_stats()
| #include <string.h> | ||
|
|
||
| char* json_int_to_string(int input) { | ||
| char *result = (char *)malloc(12 * sizeof(char)); |
There was a problem hiding this comment.
I do not see any free() for this, is this a memory leak?
There was a problem hiding this comment.
That is correct. In this case, a temporary variable would have to be used in fping.c, which then releases the memory again.
char *tmp = json_int_to_string(h->num_sent);
print_json_keyvalue("xmt", tmp, 0, 6);
free(tmp);
I will probably remove/change the function
| return; | ||
| } | ||
|
|
||
| strcpy(safe_key, json_key); |
There was a problem hiding this comment.
Could you explain why you copy the string? Could you not just give json_key to fprintf()?
| fprintf(stderr, " "); | ||
| } | ||
|
|
||
| fprintf(stderr, "\"%s\": \"%s\"", safe_key, safe_value); |
There was a problem hiding this comment.
Could you not just use json_key and json_value instead of copying them into temporary buffers and giving those to fprintf()?
|
I'd prefer JSON output from |
A JSON output can be generated using the
-j / --jsonoption.This can be extended with the
-soption.The
json.c/.hare intended to simplify use somewhat.On the one hand, it can be used to control the spaces for the output format and, on the other hand, the separators for the next data record.
The key value function only works with strings. If numbers are used, there is the function
json_int_to_string()