json.h (3535B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2021 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 struct json_iter; 24 25 /* 26 * Identical to JSMN types 27 */ 28 enum json_type { 29 JSON_UNDEFINED = 0, 30 JSON_OBJECT = 1 << 0, 31 JSON_ARRAY = 1 << 1, 32 JSON_STRING = 1 << 2, 33 JSON_PRIMITIVE = 1 << 3, 34 }; 35 36 enum json_flag { 37 JSON_FLAG_MANDATORY = 1, 38 JSON_FLAG_OPTIONAL = 2, 39 }; 40 41 struct json_iter { 42 struct json_contents *contents; 43 int start; 44 int count; 45 int current; 46 }; 47 48 #define JSON_MANDATORY(key, type, out) \ 49 (type), (key), (out), JSON_FLAG_MANDATORY 50 51 #define JSON_OPTIONAL(key, type, out) \ 52 (type), (key), (out), JSON_FLAG_OPTIONAL 53 54 #define json_iter_is_valid(iter) ((iter)->start != -1) 55 56 struct json_contents *json_contents_new(const char *json, size_t json_len); 57 void json_contents_free(struct json_contents *c); 58 59 void json_iter_init(struct json_iter *iter, struct json_contents *c); 60 61 /* 62 * Parse an arbitrary number of key/value pairs from a JSON iterator. Initially 63 * a new JSON contents object should be created with json_contents_new() and, 64 * when done, freed with json_contents_free. 65 * 66 * Iterators can be initialized with the json_contents object. Nested object 67 * iterators are also parsed with this function. 68 * 69 * Arguments should be specified using JSON_MANDATORY or JSON_OPTIONAL: 70 * 71 * r = json_iter_parse(iter, JSON_MANDATORY("mykey", JSON_STRING, &strvalue), 72 * JSON_OPTIONAL("optkey", JSON_STRING, &optvalue), 73 * JSON_UNDEFINED); 74 * 75 * String values should be of type char ** and must be freed 76 * Object values should be of type struct json_iter * 77 * Primitive types (numbers, booleans, null) should be of type 78 * struct json_iter *. This is to allow the caller to distinguish 79 * between the actual value type after parsing using a getter for 80 * the expected type (get_uint/get_int/get_boolean etc.). In 81 * addition this lets the caller use JSON_OPTIONAL and check post 82 * json_iter_parse if the iterator is valid (json_iter_is_valid). 83 * 84 * No other types are supported at this time, and json_iter_parse will fail if 85 * other types are encountered. 86 * 87 * JSON_OPTIONAL string values will point to NULL if not found 88 * JSON_OPTIONAL objects/primitives can be checked with json_iter_is_valid. 89 */ 90 bool json_iter_parse(struct json_iter *iter, enum json_type type, ...); 91 92 bool json_iter_get_int(struct json_iter *iter, int *i); 93 bool json_iter_get_uint(struct json_iter *iter, unsigned int *i); 94 bool json_iter_get_boolean(struct json_iter *iter, bool *b); 95 bool json_iter_get_null(struct json_iter *iter); 96 bool json_iter_get_container(struct json_iter *iter, 97 struct json_iter *container); 98 bool json_iter_get_string(struct json_iter *iter, char **s); 99 100 enum json_type json_iter_get_type(struct json_iter *iter); 101 bool json_iter_next(struct json_iter *iter);