Last active
December 27, 2021 20:48
-
-
Save opsJson/66a5950ce5c54f3898e83143aab32fbe to your computer and use it in GitHub Desktop.
Easily creates json, just by passing a list of strings, numbers, jsons, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <stdarg.h> | |
| size_t counter(char *str) { | |
| size_t commas = 0; | |
| int string = 0; | |
| while (*str) { | |
| //escape commas inside string; | |
| if (*str == '"' && *(str - 1) != '\\') string = !string; | |
| //count commas | |
| if (string == 0) | |
| if (*str == ',') commas++; | |
| str++; | |
| } | |
| return commas+1; | |
| } | |
| #define STR(X) #X | |
| char JSON_CMD_FLAG = 0; | |
| char JSON_STRING_FLAG = 0; | |
| void json_set_cmd_flag(int flag) { | |
| JSON_CMD_FLAG = flag; | |
| } | |
| void json_set_string_flag(int flag) { | |
| JSON_STRING_FLAG = flag; | |
| } | |
| #define json(...) __json__(__FILE__, __LINE__, counter(#__VA_ARGS__), __VA_ARGS__) | |
| char* __json__(char *file, int line, size_t count, ...) { | |
| //variables | |
| char *json; | |
| size_t i, length; | |
| va_list args; | |
| va_start(args, count); | |
| //check for pairs | |
| if (count % 2 != 0) { | |
| fprintf(stderr, "Number of keys and values unmatched at %s:%i", file, line); | |
| return 0; | |
| } | |
| //get length | |
| for (i=0, length=0; i<count; i++) { | |
| length += strlen(va_arg(args, char*)); | |
| } | |
| //allocate | |
| json = (char*)malloc((sizeof(char) * length) //length | |
| + (count * 10) //enough for \"\" \"\" , : at worst case | |
| + 2 //for { } | |
| + 1); //for null terminator | |
| if (json == NULL) { | |
| fprintf(stderr, "Couldn't allocate json at %s:%i", file, line); | |
| return 0; | |
| } | |
| //rewind args | |
| va_end(args); | |
| va_start(args, count); | |
| //make json | |
| strcpy(json, "{"); | |
| for (i=0; i<count/2; i++) { | |
| if (i > 0) strcat(json, ","); | |
| //key | |
| (JSON_CMD_FLAG) ? (strcat(json, "\\\"")) : (strcat(json, "\"")); | |
| strcat(json, va_arg(args, char*)); | |
| (JSON_CMD_FLAG) ? (strcat(json, "\\\"")) : (strcat(json, "\"")); | |
| //value | |
| strcat(json, ":"); | |
| (JSON_CMD_FLAG) ? (strcat(json, "\\\"")) : (0); | |
| (JSON_STRING_FLAG) ? (strcat(json, "\"")) : (0); | |
| strcat(json, va_arg(args, char*)); | |
| (JSON_STRING_FLAG) ? (strcat(json, "\"")) : (0); | |
| (JSON_CMD_FLAG) ? (strcat(json, "\\\"")) : (0); | |
| } | |
| strcat(json, "}"); | |
| return json; | |
| } | |
| /*/////////////////////////////////// | |
| Testing: | |
| ///////////////////////////////////*/ | |
| int main() { | |
| //set 1 for "cmd escaped" json | |
| json_set_cmd_flag(0); | |
| //set 1 for string values | |
| json_set_string_flag(1); | |
| char *j = json("string", "Hello World", "number", "123"); | |
| printf("%s\n", j); | |
| //free | |
| free(j); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment