Debian Codegraph @

texk/web2c/mfluadir/otfcc/include/dep/json-builder.h

raw
1
2 /* vim: set et ts=3 sw=3 sts=3 ft=c:
3 *
4 * Copyright (C) 2014 James McLaughlin. All rights reserved.
5 * https://github.com/udp/json-builder
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #ifndef _JSON_BUILDER_H
32 #define _JSON_BUILDER_H
33
34 /* Requires json.h from json-parser
35 * https://github.com/udp/json-parser
36 */
37 #include "json.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /* IMPORTANT NOTE: If you want to use json-builder functions with values
44 * allocated by json-parser as part of the parsing process, you must pass
45 * json_builder_extra as the value_extra setting in json_settings when
46 * parsing. Otherwise there will not be room for the extra state and
47 * json-builder WILL invoke undefined behaviour.
48 *
49 * Also note that unlike json-parser, json-builder does not currently support
50 * custom allocators (for no particular reason other than that it doesn't have
51 * any settings or global state.)
52 */
53 extern const size_t json_builder_extra;
54
55 /*** Arrays
56 ***
57 * Note that all of these length arguments are just a hint to allow for
58 * pre-allocation - passing 0 is fine.
59 */
60 json_value *json_array_new(size_t length);
61 json_value *json_array_push(json_value *array, json_value *);
62
63 /*** Objects
64 ***/
65 json_value *json_object_new(size_t length);
66
67 json_value *json_object_push(json_value *object, const json_char *name, json_value *);
68
69 /* Same as json_object_push, but doesn't call strlen() for you.
70 */
71 json_value *json_object_push_length(json_value *object, unsigned int name_length, const json_char *name, json_value *);
72
73 /* Same as json_object_push_length, but doesn't copy the name buffer before
74 * storing it in the value. Use this micro-optimisation at your own risk.
75 */
76 json_value *json_object_push_nocopy(json_value *object, unsigned int name_length, json_char *name, json_value *);
77
78 /* Merges all entries from objectB into objectA and frees objectB.
79 */
80 json_value *json_object_merge(json_value *objectA, json_value *objectB);
81
82 /* Sort the entries of an object based on the order in a prototype object.
83 * Helpful when reading JSON and writing it again to preserve user order.
84 */
85 void json_object_sort(json_value *object, json_value *proto);
86
87 /*** Strings
88 ***/
89 json_value *json_string_new(const json_char *);
90 json_value *json_string_new_length(unsigned int length, const json_char *);
91 json_value *json_string_new_nocopy(unsigned int length, json_char *);
92
93 /*** Everything else
94 ***/
95 json_value *json_integer_new(json_int_t);
96 json_value *json_double_new(double);
97 json_value *json_boolean_new(int);
98 json_value *json_null_new(void);
99
100 /*** Serializing
101 ***/
102 #define json_serialize_mode_multiline 0
103 #define json_serialize_mode_single_line 1
104 #define json_serialize_mode_packed 2
105
106 #define json_serialize_opt_CRLF (1 << 1)
107 #define json_serialize_opt_pack_brackets (1 << 2)
108 #define json_serialize_opt_no_space_after_comma (1 << 3)
109 #define json_serialize_opt_no_space_after_colon (1 << 4)
110 #define json_serialize_opt_use_tabs (1 << 5)
111
112 typedef struct json_serialize_opts {
113 int mode;
114 int opts;
115 int indent_size;
116
117 } json_serialize_opts;
118
119 /* Returns a length in characters that is at least large enough to hold the
120 * value in its serialized form, including a null terminator.
121 */
122 size_t json_measure(json_value *);
123 size_t json_measure_ex(json_value *, json_serialize_opts);
124
125 /* Serializes a JSON value into the buffer given (which must already be
126 * allocated with a length of at least json_measure(value, opts))
127 */
128 void json_serialize(json_char *buf, json_value *);
129 void json_serialize_ex(json_char *buf, json_value *, json_serialize_opts);
130
131 /*** Cleaning up
132 ***/
133 void json_builder_free(json_value *);
134
135 #ifdef __cplusplus
136 }
137 #endif
138
139 #endif