3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-12-23 06:02:37 +01:00
iwd/src/json.h
James Prestwood abfd749335 json: introduce JSON module
This is a minimal wrapper around jsmn.h to make things a bit easier
for iterating through a JSON object.

To use, first parse the JSON and create a contents object using
json_contents_new(). This object can then be used to initialize a
json_iter object using json_iter_init().

The json_iter object can then be parsed with json_iter_parse by
passing in JSON_MANDATORY/JSON_OPTIONAL arguments. Currently only
JSON_STRING and JSON_OBJECT types are supported. Any JSON_MANDATORY
values that are not found will result in an error.

If a JSON_OPTIONAL string is not found, the pointer will be NULL.
If a JSON_OPTIONAL object is not found, this iterator will be
initialized but 'start' will be -1. This can be checked with a
convenience macro json_object_not_found();
2021-12-10 17:33:47 -06:00

84 lines
2.6 KiB
C

/*
*
* Wireless daemon for Linux
*
* Copyright (C) 2021 Intel Corporation. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
struct json_iter;
/*
* Identical to JSMN types
*/
enum json_type {
JSON_UNDEFINED = 0,
JSON_OBJECT = 1,
JSON_ARRAY = 2,
JSON_STRING = 3,
JSON_PRIMITIVE = 4
};
enum json_flag {
JSON_FLAG_MANDATORY = 1,
JSON_FLAG_OPTIONAL = 2,
};
struct json_iter {
struct json_contents *contents;
int start;
int count;
};
#define JSON_MANDATORY(key, type, out) \
(type), (key), (out), JSON_FLAG_MANDATORY
#define JSON_OPTIONAL(key, type, out) \
(type), (key), (out), JSON_FLAG_OPTIONAL
#define json_iter_is_valid(iter) ((iter)->start != -1)
struct json_contents *json_contents_new(const char *json, size_t json_len);
void json_contents_free(struct json_contents *c);
void json_iter_init(struct json_iter *iter, struct json_contents *c);
/*
* Parse an arbitrary number of key/value pairs from a JSON iterator. Initially
* a new JSON contents object should be created with json_contents_new() and,
* when done, freed with json_contents_free.
*
* Iterators can be initialized with the json_contents object. Nested object
* iterators are also parsed with this function.
*
* Arguments should be specified using JSON_MANDATORY or JSON_OPTIONAL:
*
* r = json_iter_parse(iter, JSON_MANDATORY("mykey", JSON_STRING, &strvalue),
* JSON_OPTIONAL("optkey", JSON_STRING, &optvalue),
* JSON_UNDEFINED);
*
* String values should be of type char ** and must be freed
* Object values should be of type struct json_iter *
*
* No other types are supported at this time, and json_iter_parse will fail if
* other types are encountered.
*
* JSON_OPTIONAL string values will point to NULL if not found
* JSON_OPTIONAL objects can be checked with json_object_not_found.
*/
bool json_iter_parse(struct json_iter *iter, enum json_type type, ...);