]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/json.h
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / basic / json.h
CommitLineData
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
29enum {
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
44typedef 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
55union json_value {
56 bool boolean;
57 double real;
58 intmax_t integer;
59};
60
ed967b12 61typedef 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
71int json_variant_new(JsonVariant **ret, JsonVariantType type);
dde8bb32
LP
72JsonVariant *json_variant_unref(JsonVariant *v);
73
ed967b12 74DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
dde8bb32 75#define _cleanup_json_variant_unref_ _cleanup_(json_variant_unrefp)
ed967b12 76
dde8bb32
LP
77char *json_variant_string(JsonVariant *v);
78bool json_variant_bool(JsonVariant *v);
79intmax_t json_variant_integer(JsonVariant *v);
80double json_variant_real(JsonVariant *v);
ed967b12 81
dde8bb32
LP
82JsonVariant *json_variant_element(JsonVariant *v, unsigned index);
83JsonVariant *json_variant_value(JsonVariant *v, const char *key);
ed967b12 84
e7eebcfc
LP
85#define JSON_VALUE_NULL ((union json_value) {})
86
87int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);
dde8bb32 88
ed967b12
PO
89int json_parse(const char *string, JsonVariant **rv);
90int json_parse_measure(const char *string, size_t *size);