]> git.proxmox.com Git - mirror_ovs.git/blob - lib/json.h
Implement JSON parsing and serialization.
[mirror_ovs.git] / lib / json.h
1 /*
2 * Copyright (c) 2009 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef JSON_H
18 #define JSON_H 1
19
20 /* This is an implementation of JavaScript Object Notation (JSON) as specified
21 * by RFC 4627. It is intended to fully comply with RFC 4627, with the
22 * following known exceptions and clarifications:
23 *
24 * - Null bytes (\u0000) are not allowed in strings.
25 *
26 * - Only UTF-8 encoding is supported (RFC 4627 allows for other Unicode
27 * encodings).
28 *
29 * - Names within an object must be unique (RFC 4627 says that they
30 * "should" be unique).
31 */
32
33 #include "shash.h"
34
35 /* Type of a JSON value. */
36 enum json_type {
37 JSON_NULL, /* null */
38 JSON_FALSE, /* false */
39 JSON_TRUE, /* true */
40 JSON_OBJECT, /* {"a": b, "c": d, ...} */
41 JSON_ARRAY, /* [1, 2, 3, ...] */
42 JSON_INTEGER, /* 123. */
43 JSON_REAL, /* 123.456. */
44 JSON_STRING, /* "..." */
45 JSON_N_TYPES
46 };
47
48 const char *json_type_to_string(enum json_type);
49
50 /* A JSON array. */
51 struct json_array {
52 size_t n, n_allocated;
53 struct json **elems;
54 };
55
56 /* A JSON value. */
57 struct json {
58 enum json_type type;
59 union {
60 struct shash *object; /* Contains "struct json *"s. */
61 struct json_array array;
62 long long int integer;
63 double real;
64 char *string;
65 } u;
66 };
67
68 struct json *json_null_create(void);
69 struct json *json_boolean_create(bool);
70 struct json *json_string_create(const char *);
71 struct json *json_string_create_nocopy(char *);
72 struct json *json_integer_create(long long int);
73 struct json *json_real_create(double);
74
75 struct json *json_array_create_empty(void);
76 void json_array_add(struct json *, struct json *element);
77 void json_array_trim(struct json *);
78 struct json *json_array_create(struct json **, size_t n);
79 struct json *json_array_create_2(struct json *, struct json *);
80 struct json *json_array_create_3(struct json *, struct json *, struct json *);
81
82 struct json *json_object_create(void);
83 void json_object_put(struct json *, const char *name, struct json *value);
84 void json_object_put_string(struct json *,
85 const char *name, const char *value);
86
87 const char *json_string(const struct json *);
88 struct json_array *json_array(const struct json *);
89 struct shash *json_object(const struct json *);
90 bool json_boolean(const struct json *);
91 double json_real(const struct json *);
92 int64_t json_integer(const struct json *);
93
94 struct json *json_clone(const struct json *);
95 void json_destroy(struct json *);
96
97 size_t json_hash(const struct json *, size_t basis);
98 bool json_equal(const struct json *, const struct json *);
99 \f
100 /* Parsing JSON. */
101 enum {
102 JSPF_TRAILER = 1 << 0 /* Check for garbage following input. */
103 };
104
105 struct json_parser *json_parser_create(int flags);
106 size_t json_parser_feed(struct json_parser *, const char *, size_t);
107 bool json_parser_is_done(const struct json_parser *);
108 struct json *json_parser_finish(struct json_parser *);
109 void json_parser_abort(struct json_parser *);
110
111 struct json *json_from_string(const char *string);
112 struct json *json_from_file(const char *file_name);
113 \f
114 /* Serializing JSON. */
115
116 enum {
117 JSSF_PRETTY = 1 << 0, /* Multiple lines with indentation, if true. */
118 JSSF_SORT = 1 << 1 /* Object members in sorted order, if true. */
119 };
120 char *json_to_string(const struct json *, int flags);
121
122 #endif /* json.h */