|
Project Information
Links
|
vjson is very simple and very fast JSON parser. pros:
InstallationClone repository and add this files to you project:
Test projectUnder windows:
UsingInclude "json.h" and just call json_parse(). Example: char *errorPos = 0; char *errorDesc = 0; int errorLine = 0; block_allocator allocator(1 << 10); // 1 KB per block json_value *root = json_parse(source, &errorPos, &errorDesc, &errorLine, &allocator); Warning: vjson is destructive parser, i.e. you source buffer will be modyfied. Return value is root json_value object or NULL if error occured. Parameters explanation:
Now you can access values by simple iteration over resulting document: for (json_value *it = root->first_child; it; it = it->next_sibling)
{
if (value->name)
{
printf("%s = ", value->name);
}
switch(value->type)
{
case JSON_NULL:
printf("null\n");
break;
case JSON_STRING:
printf("\"%s\"\n", value->string_value);
break;
case JSON_INT:
printf("%d\n", value->int_value);
break;
case JSON_FLOAT:
printf("%f\n", value->float_value);
break;
case JSON_BOOL:
printf(value->int_value ? "true\n" : "false\n");
break;
}
}Root node lifetime is equals to block allocator lifetime. For easy loading you maybe want write something like SampleDocumentClass using own file\logging functions. ModifyingYou can replace allocator. Just change json_parse() signature and json_alloc() Or create XML SAX like parsing by adding callback to json_parse() and call them instead of json_append() LicenseDistributed under the MIT license. Copyright (c) 2010, Ivan Vashchaev |