]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/json.h
Merge pull request #1828 from fbuihuu/set-property-on-inactive-unit
[thirdparty/systemd.git] / src / basic / json.h
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>
25
26 #include "util.h"
27
28 enum {
29 JSON_END,
30 JSON_COLON,
31 JSON_COMMA,
32 JSON_OBJECT_OPEN,
33 JSON_OBJECT_CLOSE,
34 JSON_ARRAY_OPEN,
35 JSON_ARRAY_CLOSE,
36 JSON_STRING,
37 JSON_REAL,
38 JSON_INTEGER,
39 JSON_BOOLEAN,
40 JSON_NULL,
41 };
42
43 typedef enum {
44 JSON_VARIANT_CONTROL,
45 JSON_VARIANT_STRING,
46 JSON_VARIANT_INTEGER,
47 JSON_VARIANT_BOOLEAN,
48 JSON_VARIANT_REAL,
49 JSON_VARIANT_ARRAY,
50 JSON_VARIANT_OBJECT,
51 JSON_VARIANT_NULL
52 } JsonVariantType;
53
54 union json_value {
55 bool boolean;
56 double real;
57 intmax_t integer;
58 };
59
60 typedef struct JsonVariant {
61 JsonVariantType type;
62 size_t size;
63 union {
64 char *string;
65 struct JsonVariant *objects;
66 union json_value value;
67 };
68 } JsonVariant;
69
70 int json_variant_new(JsonVariant **ret, JsonVariantType type);
71 JsonVariant *json_variant_unref(JsonVariant *v);
72
73 DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
74 #define _cleanup_json_variant_unref_ _cleanup_(json_variant_unrefp)
75
76 char *json_variant_string(JsonVariant *v);
77 bool json_variant_bool(JsonVariant *v);
78 intmax_t json_variant_integer(JsonVariant *v);
79 double json_variant_real(JsonVariant *v);
80
81 JsonVariant *json_variant_element(JsonVariant *v, unsigned index);
82 JsonVariant *json_variant_value(JsonVariant *v, const char *key);
83
84 #define JSON_VALUE_NULL ((union json_value) {})
85
86 int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);
87
88 int json_parse(const char *string, JsonVariant **rv);
89 int json_parse_measure(const char *string, size_t *size);