LibJSON

A JSON parser and printer
Download

LibJSON Ranking & Summary

Advertisement

  • Rating:
  • License:
  • LGPL v3
  • Price:
  • FREE
  • Publisher Name:
  • vincenthz

LibJSON Tags


LibJSON Description

A JSON parser and printer LibJSON is a small C library and small codebase that packs an efficient parser and a configurable printer. It is distributed under the LGPLv2 license, or at your option the LGPLv3 license.Using itParsingParsing is really easy, first you need to initialize a context with:int ret;json_parser parser;ret = json_parser_init(&parser, NULL, my_callback, my_callback_data);if (ret) { fprintf(stderr, "something wrong happened during init "); return ret;}my_callback and my_callback_data are the callback function with the data pointer that the parser is going to call after each parsing eventsRemember that the parser structure need to allocate some data, so when the parser context is not in used anymore, it need to be free:json_parser_free(&parser);Now this is the most important part, the actual feeding of data to the parser. The main function to use is json_parser_string; it takes a parser context, a string and a length, and optionally take a int pointer to be able to return the number of processed bytes which is only useful in case of error in the stream. The following example show how it's done when reading a json from a string in memory:char my_json_string[] = "{ "key": 123 }";ret = json_parser_string(&parser, my_json_string, strlen(my_json_string), NULL);if (ret) { /* error happened : print a message or something */ break;}The last part is the callback context, that is called as each parsing event. the following is the callback function parameters:int my_callback(void *userdata, int type, const char *data, uint32_t length)The callback is called with first the userdata. this is what has been registered at the parser init, as my_callback_data. It's useful to pass any data (as pointer to them) to the callback, such as a context, a handle, etc.Then the parsing event is represented by its type, and optionaly a valid data and length.here is a full callback function that just print some information:int my_callback(void *userdata, int type, const char *data, uint32_t length){ switch (type) { case JSON_OBJECT_BEGIN: case JSON_ARRAY_BEGIN: printf("entering structure "); break; case JSON_OBJECT_END: case JSON_ARRAY_END: printf("ending structure "); break; case JSON_KEY: case JSON_STRING: case JSON_INT: case JSON_FLOAT: printf("%*s ", length, data); break; case JSON_NULL: case JSON_TRUE: case JSON_FALSE: printf("constant "); }}PrintingPrinting is even easier than parsing. First you need a printing context, and a write callback function.The write callback function is in charge to putting the marshalled data (the string) where you want. For example you want to print to a file, to a socket, or to a memory buffer that grows.not complete... Here are some key features of "LibJSON": · Interruptible parser: get the JSON data to the parser any way you want; by appending char by char, or string chunks, the input reading is completely left to the caller. · No object model integrated: easy integration with any model by the means of a simple callback. · Small codebase: handcoded parser and efficient factorisation make the code smalls. · Full JSON support: tested through a small and precise testsuite. · No native conversion: callback only string of data and leave the actual representation of data to the caller · Supports putting limits on nesting level · Supports putting limits on data (string/int/float) size · Optionally support YAML/python and C comments. · Supports projects-specific allocation functions to integrate completely with projects · jsonlint utility provided with the library to verify, or reformat json stream. also useful as example on how to use the library. What's New in This Release: · some fixes in the install rules, add a pc file for pkgconfig


LibJSON Related Software