Issue
I'm writing a kernel module that needs to parse json strings, and I was wondering if the linux kernel provides any API to do it, the way that I'm handling it now is writing a port of JSMN to the linux kernel but I think that this isn't an elegant way to do it, so there is any way more elegant to do it?
Solution
Linux Kernel doesn't support JSON format natively.
This is an Open Source project that I'm developing for supporting JSON on Linux Kernel: https://github.com/emalele1688/JsonOnKernel
Library interface is documented into the kjson.h
file. You can just link the library code into your project and start to use it. Here some example;
struct kjstring_t to_parse;
struct kjson_container *ctn;
struct kjson_object_t *obj;
char *str = "{\"accepted_1\": {\"codes\": []}, \"accepted_2\": [{\"codes\": []}, {\"enabled\": 1}]}"; // A simple JSON
kjstring_new_string_buffer(&to_parse, str, strlen(str));
ctn = kjson_parse(&to_parse); // This will start to parse the JSON
obj = kjson_lookup_object(ctn, "enabled");
int enabled = kjson_as_integer(obj); // is 1
You can find more example inside the repository. Currently the library is under development by myself, and the repository is open for every pull request.
Answered By - Emanuele Santini Answer Checked By - Timothy Miller (WPSolving Admin)