Skip to content

Instantly share code, notes, and snippets.

@doylecnn
Created September 10, 2015 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doylecnn/3d355dc16ed6e864c4ee to your computer and use it in GitHub Desktop.
Save doylecnn/3d355dc16ed6e864c4ee to your computer and use it in GitHub Desktop.
cjosn test
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "cJSON.h"
char* parseUint32(uint32_t num){
char b[30];
int len = sprintf(b, "%u", num);
char *p = (char*)malloc(sizeof(char)*len);
strcpy(p, b);
return p;
}
char* pack_json(int argc, const char** argv){
cJSON* root = cJSON_CreateObject();
cJSON* params = cJSON_CreateStringArray(argv, argc);
cJSON_AddItemToObject(root, "params", params);
char* result = cJSON_Print(root);
cJSON_Delete(root);
return result;
}
void printJson(int argc, const char** argv){
char* jsonStr = pack_json(argc, argv);
printf("%s\n", jsonStr);
free(jsonStr);
}
void test(){
const char** params = malloc(3*sizeof(char*));
char** p = (char**)params;
*p = parseUint32(1);
p++;
*p = parseUint32(2);
p++;
*p = parseUint32(3);
printJson(3,params);
p = (char**)params;
for(int i=0; i< 3; i++,p++){
free(*p);
}
free(params);
}
int main(int argc, char** argv){
for(int i=0;i<100;i++){
test();
}
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment