Skip to content

wonder-mice/json_put

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json_put

Zero-Footprint JSON Writer for C (and C++)

Example (using fixed preallocated array):

char json[128];
size_t json_len;
struct json_put jp;
struct json_put_buffer jp_buf;
json_put_static_buffer_init(&buf, json, sizeof(json));
json_put_init(&jp, &jp_buf, json_put_to_static_buffer);
/* write some json */
json_put_object_begin(&jp);
	json_put_name(&jp, "numbers");
	json_put_array_begin(&jp);
		json_put_string(&jp, "1");
		json_put_string(&jp, "2");
		json_put_string(&jp, "3");
	json_put_array_end(&jp);
	json_put_name(&jp, "key");
	json_put_string(&jp, "42");
json_put_object_end(&jp);
/* print generated string */
json_put_buffer_data(&jp_buf, &json_len);
fwrite(json, 1, json_len, stdout);
fprintf(stdout, "\n");

Example (using std::string from C++):

std::string json;
json_put jp;
json_put_init(&jp, &json, json_put_to_std_string);
// write some json
json_put_object_begin(&jp);
	json_put_name(&jp, "numbers");
	json_put_array_begin(&jp);
		json_put_string(&jp, "1");
		json_put_string(&jp, "2");
		json_put_string(&jp, "3");
	json_put_array_end(&jp);
	json_put_name(&jp, "key");
	json_put_string(&jp, "42");
json_put_object_end(&jp);
// print generated string
printf("%s\n", json.c_str());

Both examples will print:

{"numbers":["1","2","3"],"key":"42"}

About

Zero-Footprint JSON Writer for C (and C++)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published