]>
Commit | Line | Data |
---|---|---|
e7eebcfc LP |
1 | #pragma once |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2014 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
22 | #include <stdbool.h> | |
11c3a366 TA |
23 | #include <stddef.h> |
24 | #include <stdint.h> | |
71d35b6b | 25 | |
11c3a366 | 26 | #include "macro.h" |
ed967b12 | 27 | #include "util.h" |
e7eebcfc LP |
28 | |
29 | enum { | |
30 | JSON_END, | |
31 | JSON_COLON, | |
32 | JSON_COMMA, | |
33 | JSON_OBJECT_OPEN, | |
34 | JSON_OBJECT_CLOSE, | |
35 | JSON_ARRAY_OPEN, | |
36 | JSON_ARRAY_CLOSE, | |
37 | JSON_STRING, | |
38 | JSON_REAL, | |
39 | JSON_INTEGER, | |
40 | JSON_BOOLEAN, | |
41 | JSON_NULL, | |
42 | }; | |
43 | ||
ed967b12 PO |
44 | typedef enum { |
45 | JSON_VARIANT_CONTROL, | |
46 | JSON_VARIANT_STRING, | |
47 | JSON_VARIANT_INTEGER, | |
48 | JSON_VARIANT_BOOLEAN, | |
49 | JSON_VARIANT_REAL, | |
50 | JSON_VARIANT_ARRAY, | |
51 | JSON_VARIANT_OBJECT, | |
52 | JSON_VARIANT_NULL | |
53 | } JsonVariantType; | |
54 | ||
e7eebcfc LP |
55 | union json_value { |
56 | bool boolean; | |
57 | double real; | |
58 | intmax_t integer; | |
59 | }; | |
60 | ||
ed967b12 | 61 | typedef struct JsonVariant { |
dde8bb32 LP |
62 | JsonVariantType type; |
63 | size_t size; | |
ed967b12 PO |
64 | union { |
65 | char *string; | |
66 | struct JsonVariant *objects; | |
67 | union json_value value; | |
68 | }; | |
ed967b12 PO |
69 | } JsonVariant; |
70 | ||
71 | int json_variant_new(JsonVariant **ret, JsonVariantType type); | |
dde8bb32 LP |
72 | JsonVariant *json_variant_unref(JsonVariant *v); |
73 | ||
ed967b12 | 74 | DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref); |
dde8bb32 | 75 | #define _cleanup_json_variant_unref_ _cleanup_(json_variant_unrefp) |
ed967b12 | 76 | |
dde8bb32 LP |
77 | char *json_variant_string(JsonVariant *v); |
78 | bool json_variant_bool(JsonVariant *v); | |
79 | intmax_t json_variant_integer(JsonVariant *v); | |
80 | double json_variant_real(JsonVariant *v); | |
ed967b12 | 81 | |
dde8bb32 LP |
82 | JsonVariant *json_variant_element(JsonVariant *v, unsigned index); |
83 | JsonVariant *json_variant_value(JsonVariant *v, const char *key); | |
ed967b12 | 84 | |
e7eebcfc LP |
85 | #define JSON_VALUE_NULL ((union json_value) {}) |
86 | ||
87 | int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line); | |
dde8bb32 | 88 | |
ed967b12 PO |
89 | int json_parse(const char *string, JsonVariant **rv); |
90 | int json_parse_measure(const char *string, size_t *size); |