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