Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
json formatter
#include <stdio.h> // printf #include <string.h> // strlen, strcat, strncat, memmove #include <stdlib.h> // malloc #include <ctype.h> // isspace extern void removeWhiteSpace(char *str); extern char * str2JsonFormat(char *prevStr, char *key, char *valueStr); /* ============================================================================================= ======== Remove whitespace from string ========== ============================================================================================= reference: https://www.geeksforgeeks.org/remove-spaces-from-a-given-string/ 1) Initialize 'count' = 0 (Count of non-space character seen so far) 2) Iterate through all characters of given string, do following a) If current character is non-whitepace, then put this character at index 'count' and increment 'count' 3) Finally, write a NULL ('\0') at index 'count' External Library functions: isspace Usage: (string contains tabs, spaces and newlines) remove_spaces(" { "key1" : "data1" } "); Example output: {"key1":"data1"} */ void removeWhiteSpace(char *str) { int count = 0; // Count of non-space characters int i = 0; // iterate over each character in string // iterate over each character in string. for (i = 0; str[i]; i++) { // If current character is NOT a whitespace, then move it to index 'count++' if ( !isspace(str[i]) ) { str[count++] = str[i]; } } str[count] = '\0'; // add a null at the new end of string; } /* ============================================================================================= ======== JSON formatter ========== ============================================================================================= Takes three parameters 1) 'prevStr' string. If empty string, or an empty object creates a default new JSON formated string. Otherwise adds a ',' (comma) 2) 'key' property parameter 3) 'valueStr' string parameter. Can be any character, string, integer or float viewed as a string type. NOTE: This will NOT convert integers and floats to strings. It does not know how many decimal places to convert. This will need to be done as a separate step before passing it as a string parameter to this function. After checking validity of previous string and removing whitespace, it then wraps it in JSON ["key": "value"] string type format, appends it to the previous string and returns the new string. Each ["key": "value"] pair is separated by a "," (comma) External Library functions: strlen, strncat, strcat, malloc Usage: exampleOutput = str2JsonFormat(lastString, "name", "data"); Example1 output: (if lastString="" or lastString={} ) {"name":"data"} Example2 output: (if last string was NOT empty) {"name":"data","name":"data"} */ char * str2JsonFormat(char *prevStr, char *key, char *valueStr) { char *out; // formatted JSON return string int outSize = 0; // size of formatted JSON return string int overhead = 8; // extra characters for JSON formatting // char d[100] = {}; // int i, j; // need to calculate space needed for formatted return string // Add length of previous string input parameter // Add length key string input parameter // Add length valueStr string input parameter // Add overhead characters to reflect JSON formatting outSize = strlen(prevStr) + strlen(key) + strlen(valueStr) + overhead; // need to malloc memory location out = (char *) malloc(sizeof(char) * outSize); // remove all white space characters (space, tab, CR, LF) removeWhiteSpace(prevStr); // if previous string is empty or empty object create default string if ( (strlen(prevStr) == 0) | ( (prevStr[0] == '{') & (prevStr[1] == '}') ) ) { strcat(out, "{"); // add a "{" (left brace) } // otherwise append previous string to formatted return string with slight modifications else { strncat(out, prevStr, strlen(prevStr)-1 ); // delete the last '}' (right brace) strcat(out, ","); // and add ',' (comma) } // concatenate new key / value pair with JSON formatting strcat(out, "\""); // add a '"' (double quote) strcat(out, key); strcat(out, "\":\""); // complete the (double quote), add ':' (colon), start a new (double quote) strcat(out, valueStr); strcat(out, "\"}"); // add a '}' (right brace) // return pointer to the new JSON formatted string return out; } int main() { char lastString[100]; char * jsonTemp; // strcat(lastString, "\0"); strcat(lastString, " { \"key1\" : \"data1\" } \0"); jsonTemp = str2JsonFormat(lastString, "key2", "data2"); memmove(lastString, jsonTemp, sizeof(lastString) ); jsonTemp = str2JsonFormat(lastString, "key3", "data3"); memmove(lastString, jsonTemp, sizeof(lastString) ); jsonTemp = str2JsonFormat(lastString, "key4", "data4"); memmove(lastString, jsonTemp, sizeof(lastString) ); printf(" output length is %li \r\n", strlen(lastString) ); printf(" output is %s \r\n", lastString); return 0; }
run
|
edit
|
history
|
help
0
es collar
Coba
tom grey wolf
2.1.1 Prime numbers in different threads with thread ID
Shapes and such
Lab 6 mb
Max2
mypro
Comment.c
structure1