2 * Copyright (c) 2019 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
30 context_is_value(context c)
32 return c == root || c == array_value || c == object_value;
35 writer::writer(FILE *f)
50 writer::begin_value(int sep)
55 fprintf(_file, ", %c\n", sep);
60 if (_context == array_value || _context == object_key) {
61 fprintf(_file, "%*s", _depth * 2, "");
64 fprintf(_file, "%c\n", sep);
69 writer::advance(context c)
77 _context = array_value;
81 _context = object_key;
85 _context = object_value;
95 writer::key(const char *key)
97 assert(_context == object_key);
100 fprintf(_file, "\"%s\": ", key);
105 writer::object(std::function<void()> f)
107 context old = _context;
108 assert(context_is_value(old));
113 _context = object_key;
114 _needs_comma = false;
118 fprintf(_file, "\n%*s}", _depth * 2, "");
123 writer::object(const char *k, std::function<void()> f)
130 writer::array(std::function<void()> f)
132 context old = _context;
133 assert(context_is_value(old));
138 _context = array_value;
139 _needs_comma = false;
143 fprintf(_file, "\n%*s]", _depth * 2, "");
148 writer::array(const char *k, std::function<void()> f)
155 writer::boolean(bool value)
157 assert(context_is_value(_context));
159 fputs(value ? "true" : "false", _file);
164 writer::boolean(const char *k, bool value)
171 writer::number(uint64_t value)
173 assert(context_is_value(_context));
175 fprintf(_file, "%lld", value);
180 writer::number(const char *k, uint64_t value)
187 writer::string(const char *s)
189 assert(context_is_value(_context));
191 fprintf(_file, "\"%s\"", s);
196 writer::string(const char *k, const char *s)
203 writer::stringf(const char *fmt, ...)
207 assert(context_is_value(_context));
211 vfprintf(_file, fmt, ap);
218 writer::stringf(const char *k, const char *fmt, ...)
224 assert(context_is_value(_context));
228 vfprintf(_file, fmt, ap);