Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
json string formatter
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <stdio.h> // printf #include <string.h> // strlen, strcat, strncat, memmove #include <stdlib.h> // malloc #include <ctype.h> // isspace extern char *addStr(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6); extern void removeWhiteSpace(char *str); extern char * str2JsonFormat(char *prevStr, char *key, char *valueStr); /* ============================================================================================= ======== concatenate multiple strings ========== ============================================================================================= This will compute the total size of 6 input strings, catenate them together then return a new string External Library functions: strlen, strcat, malloc Usage: new_string = addStr(s1,s2,s3,s4,s5) Example output: "s1s2s3s4s5s6" */ char *addStr(char *s1, char *s2, char *s3, char *s4, char *s5, char *s6) { char *out; // catenated return string int outSize = 0; // size of return string // need to calculate space needed for catenated return string // Add length of all input parameters outSize = strlen(s1) + strlen(s2) + strlen(s3) + strlen(s4) + strlen(s5)+ strlen(s6); // need to malloc a cozy memory location out = (char *) malloc(sizeof(char) * outSize); // concatenate new string strcat(out, s1); strcat(out, s2); strcat(out, s3); strcat(out, s4); strcat(out, s5); strcat(out, s6); return out; } /* ============================================================================================= ======== Remove whitespace from string ========== ============================================================================================= reference: https://www.geeksforgeeks.org/remove-spaces-from-a-given-string/ another version to remove extra spaces leaving only one reference: https://www.geeksforgeeks.org/remove-extra-spaces-string/?ref=lbp checks for balanced pairs of brackets, braces, and parentheses reference https://www.geeksforgeeks.org/check-for-balanced-parentheses-in-an-expression/?ref=leftbar-rightbar This will remove all white space characters (space, tab, CR, LF) from string. It modifies the 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' Enhancement 1) make a copy the initial string to a temp variable using memmove 2) now remove whitespace from temp variable 3) return the temp variable 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) Limitations: This will ONLY create a single object with string type key / value properties. Its all I need for this application. 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 // remove all white space characters, if any (space, tab, CR, LF) from prevStr removeWhiteSpace(prevStr); // 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 a cozy memory location out = (char *) malloc(sizeof(char) * outSize); // if prevStr already has a property remove closing brace and add comma, otherwise start new strcat(out, "{"); // start new JSON string if (strlen(prevStr) >= 9 ) { out[0] = 0; // nullify out string strncat(out, prevStr, strlen(prevStr)-1 ); // delete the '}' (closing brace) strcat(out, ","); // and add ',' (comma) } out = addStr(out, "\"", key, "\":\"", valueStr, "\"}"); // concatenate JSON string return out; // return pointer to the new JSON formatted string } int main() { char *lastString; char *jsonTemp; lastString = (char *) malloc(sizeof(char) * 100); memmove(lastString, " { \"key1\" : \"data1\" }\0", strlen(" { \"key1\" : \"data1\" }\0") ); jsonTemp = str2JsonFormat(lastString, "key2", "data2"); lastString = (char *) malloc(sizeof(char) * strlen(jsonTemp) ); memmove(lastString, jsonTemp, strlen(jsonTemp) ); jsonTemp = str2JsonFormat(lastString, "key3", "data3"); lastString = (char *) malloc(sizeof(char) * strlen(jsonTemp) ); memmove(lastString, jsonTemp, strlen(jsonTemp) ); jsonTemp = str2JsonFormat(lastString, "key4", "data4"); lastString = (char *) malloc(sizeof(char) * strlen(jsonTemp) ); memmove(lastString, jsonTemp, strlen(jsonTemp) ); printf(" output length is %li \r\n", strlen(lastString) ); printf(" output is %s \r\n", lastString); return 0; }
gcc
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.12 sec, absolute running time: 0.15 sec, cpu time: 0.01 sec, memory peak: 5 Mb, absolute service time: 0,4 sec
edit mode
|
history
|
discussion
output length is 60 output is {"key1":"data1,"key2":"data2","key3":"data3","key4":"data4"}