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 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/ 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) 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; char *jsonTemp; lastString = (char *) malloc(sizeof(char) * strlen("") ); // memmove(lastString, " { \"key1\" : \"data1\" } \0", strlen(" { \"key1\" : \"data1\" } \0") ); // strcat(lastString, "\0"); // strcat(lastString, " { \"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.23 sec, absolute running time: 0.18 sec, cpu time: 0.01 sec, memory peak: 5 Mb, absolute service time: 0,55 sec
edit mode
|
history
|
discussion
output length is 46 output is {"key2":"data2","key3":"data3","key4":"data4"}