2021-12-10 23:33:26 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* 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,
|
2022-11-02 19:46:00 +01:00
|
|
|
JSON_OBJECT = 1 << 0,
|
|
|
|
JSON_ARRAY = 1 << 1,
|
|
|
|
JSON_STRING = 1 << 2,
|
|
|
|
JSON_PRIMITIVE = 1 << 3,
|
2021-12-10 23:33:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
enum json_flag {
|
|
|
|
JSON_FLAG_MANDATORY = 1,
|
|
|
|
JSON_FLAG_OPTIONAL = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct json_iter {
|
|
|
|
struct json_contents *contents;
|
|
|
|
int start;
|
|
|
|
int count;
|
2022-01-06 20:50:02 +01:00
|
|
|
int current;
|
2021-12-10 23:33:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#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),
|
2022-01-11 17:51:11 +01:00
|
|
|
* JSON_OPTIONAL("optkey", JSON_STRING, &optvalue),
|
|
|
|
* JSON_UNDEFINED);
|
2021-12-10 23:33:26 +01:00
|
|
|
*
|
|
|
|
* String values should be of type char ** and must be freed
|
|
|
|
* Object values should be of type struct json_iter *
|
2022-01-06 20:50:00 +01:00
|
|
|
* Primitive types (numbers, booleans, null) should be of type
|
2022-01-11 17:51:11 +01:00
|
|
|
* struct json_iter *. This is to allow the caller to distinguish
|
|
|
|
* between the actual value type after parsing using a getter for
|
|
|
|
* the expected type (get_uint/get_int/get_boolean etc.). In
|
|
|
|
* addition this lets the caller use JSON_OPTIONAL and check post
|
|
|
|
* json_iter_parse if the iterator is valid (json_iter_is_valid).
|
2021-12-10 23:33:26 +01:00
|
|
|
*
|
|
|
|
* 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
|
2022-01-06 20:50:00 +01:00
|
|
|
* JSON_OPTIONAL objects/primitives can be checked with json_object_is_valid.
|
2021-12-10 23:33:26 +01:00
|
|
|
*/
|
|
|
|
bool json_iter_parse(struct json_iter *iter, enum json_type type, ...);
|
2022-01-06 20:50:00 +01:00
|
|
|
|
|
|
|
bool json_iter_get_int(struct json_iter *iter, int *i);
|
|
|
|
bool json_iter_get_uint(struct json_iter *iter, unsigned int *i);
|
|
|
|
bool json_iter_get_boolean(struct json_iter *iter, bool *b);
|
|
|
|
bool json_iter_get_null(struct json_iter *iter);
|
2022-01-06 20:50:05 +01:00
|
|
|
bool json_iter_get_container(struct json_iter *iter,
|
|
|
|
struct json_iter *container);
|
2022-01-06 23:52:54 +01:00
|
|
|
bool json_iter_get_string(struct json_iter *iter, char **s);
|
2022-01-06 20:50:02 +01:00
|
|
|
|
|
|
|
enum json_type json_iter_get_type(struct json_iter *iter);
|
|
|
|
bool json_iter_next(struct json_iter *iter);
|