]>
Commit | Line | Data |
---|---|---|
f38b84ea | 1 | /* |
a11fdef1 | 2 | * Copyright (c) 2009, 2010 Nicira, Inc. |
f38b84ea BP |
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 | ||
0225f3a0 BP |
35 | #ifdef __cplusplus |
36 | extern "C" { | |
37 | #endif | |
38 | ||
36d802ae BP |
39 | struct ds; |
40 | ||
f38b84ea BP |
41 | /* Type of a JSON value. */ |
42 | enum json_type { | |
43 | JSON_NULL, /* null */ | |
44 | JSON_FALSE, /* false */ | |
45 | JSON_TRUE, /* true */ | |
46 | JSON_OBJECT, /* {"a": b, "c": d, ...} */ | |
47 | JSON_ARRAY, /* [1, 2, 3, ...] */ | |
48 | JSON_INTEGER, /* 123. */ | |
49 | JSON_REAL, /* 123.456. */ | |
50 | JSON_STRING, /* "..." */ | |
51 | JSON_N_TYPES | |
52 | }; | |
53 | ||
54 | const char *json_type_to_string(enum json_type); | |
55 | ||
56 | /* A JSON array. */ | |
57 | struct json_array { | |
58 | size_t n, n_allocated; | |
59 | struct json **elems; | |
60 | }; | |
61 | ||
62 | /* A JSON value. */ | |
63 | struct json { | |
64 | enum json_type type; | |
65 | union { | |
66 | struct shash *object; /* Contains "struct json *"s. */ | |
67 | struct json_array array; | |
68 | long long int integer; | |
69 | double real; | |
70 | char *string; | |
71 | } u; | |
72 | }; | |
73 | ||
74 | struct json *json_null_create(void); | |
75 | struct json *json_boolean_create(bool); | |
76 | struct json *json_string_create(const char *); | |
77 | struct json *json_string_create_nocopy(char *); | |
78 | struct json *json_integer_create(long long int); | |
79 | struct json *json_real_create(double); | |
80 | ||
81 | struct json *json_array_create_empty(void); | |
82 | void json_array_add(struct json *, struct json *element); | |
83 | void json_array_trim(struct json *); | |
84 | struct json *json_array_create(struct json **, size_t n); | |
f6f8c3ba | 85 | struct json *json_array_create_1(struct json *); |
f38b84ea BP |
86 | struct json *json_array_create_2(struct json *, struct json *); |
87 | struct json *json_array_create_3(struct json *, struct json *, struct json *); | |
88 | ||
89 | struct json *json_object_create(void); | |
90 | void json_object_put(struct json *, const char *name, struct json *value); | |
91 | void json_object_put_string(struct json *, | |
92 | const char *name, const char *value); | |
93 | ||
94 | const char *json_string(const struct json *); | |
95 | struct json_array *json_array(const struct json *); | |
96 | struct shash *json_object(const struct json *); | |
97 | bool json_boolean(const struct json *); | |
98 | double json_real(const struct json *); | |
99 | int64_t json_integer(const struct json *); | |
100 | ||
101 | struct json *json_clone(const struct json *); | |
102 | void json_destroy(struct json *); | |
103 | ||
104 | size_t json_hash(const struct json *, size_t basis); | |
105 | bool json_equal(const struct json *, const struct json *); | |
106 | \f | |
107 | /* Parsing JSON. */ | |
108 | enum { | |
109 | JSPF_TRAILER = 1 << 0 /* Check for garbage following input. */ | |
110 | }; | |
111 | ||
112 | struct json_parser *json_parser_create(int flags); | |
113 | size_t json_parser_feed(struct json_parser *, const char *, size_t); | |
114 | bool json_parser_is_done(const struct json_parser *); | |
115 | struct json *json_parser_finish(struct json_parser *); | |
116 | void json_parser_abort(struct json_parser *); | |
117 | ||
118 | struct json *json_from_string(const char *string); | |
119 | struct json *json_from_file(const char *file_name); | |
5562d6f5 | 120 | struct json *json_from_stream(FILE *stream); |
f38b84ea BP |
121 | \f |
122 | /* Serializing JSON. */ | |
123 | ||
124 | enum { | |
125 | JSSF_PRETTY = 1 << 0, /* Multiple lines with indentation, if true. */ | |
126 | JSSF_SORT = 1 << 1 /* Object members in sorted order, if true. */ | |
127 | }; | |
128 | char *json_to_string(const struct json *, int flags); | |
36d802ae | 129 | void json_to_ds(const struct json *, int flags, struct ds *); |
7d23a63a BP |
130 | \f |
131 | /* JSON string formatting operations. */ | |
132 | ||
133 | bool json_string_unescape(const char *in, size_t in_len, char **outp); | |
f38b84ea | 134 | |
0225f3a0 BP |
135 | #ifdef __cplusplus |
136 | } | |
137 | #endif | |
138 | ||
f38b84ea | 139 | #endif /* json.h */ |