CToon C++ 0.6.1
A C++ TOON/JSON serialization library
Loading...
Searching...
No Matches
ctoon.hpp
Go to the documentation of this file.
1
29#pragma once
30
31#include "ctoon.h"
32
33#include <cstddef>
34#include <cstdint>
35#include <cstring>
36#include <cstdio>
37#include <exception>
38#include <stdexcept>
39#include <string>
40
41/* -----------------------------------------------------------------------
42 * Portability helpers
43 * -------------------------------------------------------------------- */
44
46#if __cplusplus >= 201103L
47# define CTOON_NOEXCEPT noexcept
48#else
49# define CTOON_NOEXCEPT throw()
50#endif
51
52#if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
53# include <string_view>
54 namespace ctoon { using string_view = std::string_view; }
55#else
56 namespace ctoon {
57 class string_view {
58 public:
59 string_view() CTOON_NOEXCEPT : d_(""), n_(0) {}
60 string_view(const char *p, std::size_t n) CTOON_NOEXCEPT
61 : d_(p ? p : ""), n_(p ? n : 0) {}
62 string_view(const char *p) CTOON_NOEXCEPT
63 : d_(p ? p : ""), n_(p ? std::strlen(p) : 0) {}
64 string_view(const std::string &s) CTOON_NOEXCEPT
65 : d_(s.data()), n_(s.size()) {}
66 const char *data() const CTOON_NOEXCEPT { return d_; }
67 std::size_t size() const CTOON_NOEXCEPT { return n_; }
68 bool empty() const CTOON_NOEXCEPT { return n_ == 0; }
69 std::string str() const { return std::string(d_, n_); }
70 bool operator==(const string_view &o) const CTOON_NOEXCEPT {
71 return n_ == o.n_ && std::memcmp(d_, o.d_, n_) == 0;
72 }
73 bool operator!=(const string_view &o) const CTOON_NOEXCEPT { return !(*this == o); }
74 private:
75 const char *d_;
76 std::size_t n_;
77 };
78 } // namespace ctoon
79#endif
81
82namespace ctoon {
83
84/* -----------------------------------------------------------------------
85 * Version
86 * -------------------------------------------------------------------- */
87
88class version {
89public:
90 static unsigned int major() CTOON_NOEXCEPT { return CTOON_VERSION_MAJOR; }
91 static unsigned int minor() CTOON_NOEXCEPT { return CTOON_VERSION_MINOR; }
92 static unsigned int patch() CTOON_NOEXCEPT { return CTOON_VERSION_PATCH; }
93 static unsigned int hex() CTOON_NOEXCEPT { return CTOON_VERSION_HEX; }
94 static string_view string() CTOON_NOEXCEPT { return CTOON_VERSION_STRING; }
95private:
96 version();
97};
98
99/* -----------------------------------------------------------------------
100 * Exception hierarchy
101 * -------------------------------------------------------------------- */
102
103class error : public std::exception {
104public:
105 explicit error(const std::string &msg) CTOON_NOEXCEPT : msg_(msg) {}
106 virtual const char *what() const CTOON_NOEXCEPT { return msg_.c_str(); }
107 virtual ~error() CTOON_NOEXCEPT {}
108protected:
109 std::string msg_;
110};
111
112class parse_error : public error {
113public:
114 ctoon_read_code code;
115 std::size_t pos;
116 explicit parse_error(const ctoon_read_err &e)
117 : error(build_msg(e)), code(e.code), pos(e.pos) {}
118 virtual ~parse_error() CTOON_NOEXCEPT {}
119private:
120 static std::string build_msg(const ctoon_read_err &e) {
121 std::string m(e.msg ? e.msg : "parse error");
122 char buf[32]; std::sprintf(buf, " (pos %zu)", e.pos);
123 return m + buf;
124 }
125};
126
127class write_error : public error {
128public:
129 ctoon_write_code code;
130 explicit write_error(const ctoon_write_err &e)
131 : error(e.msg ? e.msg : "write error"), code(e.code) {}
132 virtual ~write_error() CTOON_NOEXCEPT {}
133};
134
135/* -----------------------------------------------------------------------
136 * write_result
137 * -------------------------------------------------------------------- */
138
140public:
141 write_result() CTOON_NOEXCEPT : ptr_(NULL), len_(0) {}
142 write_result(char *p, std::size_t n) CTOON_NOEXCEPT : ptr_(p), len_(n) {}
143 ~write_result() { std::free(ptr_); }
144 write_result(write_result &&o) CTOON_NOEXCEPT
145 : ptr_(o.ptr_), len_(o.len_) { o.ptr_ = NULL; o.len_ = 0; }
146 write_result &operator=(write_result &&o) CTOON_NOEXCEPT {
147 if (this != &o) { std::free(ptr_); ptr_ = o.ptr_; len_ = o.len_; o.ptr_ = NULL; o.len_ = 0; }
148 return *this;
149 }
150 const char *c_str() const CTOON_NOEXCEPT { return ptr_ ? ptr_ : ""; }
151 std::size_t size() const CTOON_NOEXCEPT { return len_; }
152 bool empty() const CTOON_NOEXCEPT { return len_ == 0; }
153 string_view view() const CTOON_NOEXCEPT { return string_view(ptr_ ? ptr_ : "", len_); }
154 std::string str() const { return std::string(ptr_ ? ptr_ : "", len_); }
155 char *ptr_;
156 std::size_t len_;
157};
158
159/* -----------------------------------------------------------------------
160 * delimiter – shadows ctoon_delimiter
161 * -------------------------------------------------------------------- */
162
163enum class delimiter : uint32_t {
164 COMMA = CTOON_DELIMITER_COMMA,
165 TAB = CTOON_DELIMITER_TAB,
166 PIPE = CTOON_DELIMITER_PIPE,
167};
168
169/* -----------------------------------------------------------------------
170 * write_flag – shadows ctoon_write_flag
171 *
172 * Use these directly wherever ctoon_write_flag was used.
173 * Implicit cast to ctoon_write_flag is provided via to_c().
174 * -------------------------------------------------------------------- */
175
176enum class write_flag : uint32_t {
177 NOFLAG = static_cast<uint32_t>(CTOON_WRITE_NOFLAG),
178 ESCAPE_UNICODE = static_cast<uint32_t>(CTOON_WRITE_ESCAPE_UNICODE),
179 ESCAPE_SLASHES = static_cast<uint32_t>(CTOON_WRITE_ESCAPE_SLASHES),
180 ALLOW_INF_AND_NAN = static_cast<uint32_t>(CTOON_WRITE_ALLOW_INF_AND_NAN),
181 INF_AND_NAN_AS_NULL = static_cast<uint32_t>(CTOON_WRITE_INF_AND_NAN_AS_NULL),
182 ALLOW_INVALID_UNICODE = static_cast<uint32_t>(CTOON_WRITE_ALLOW_INVALID_UNICODE),
183 LENGTH_MARKER = static_cast<uint32_t>(CTOON_WRITE_LENGTH_MARKER),
184 NEWLINE_AT_END = static_cast<uint32_t>(CTOON_WRITE_NEWLINE_AT_END),
185};
186
187inline write_flag operator|(write_flag l, write_flag r) CTOON_NOEXCEPT {
188 return static_cast<write_flag>(static_cast<uint32_t>(l) | static_cast<uint32_t>(r));
189}
190inline write_flag &operator|=(write_flag &l, write_flag r) CTOON_NOEXCEPT {
191 l = l | r; return l;
192}
194inline ctoon_write_flag to_c(write_flag f) CTOON_NOEXCEPT {
195 return static_cast<ctoon_write_flag>(static_cast<uint32_t>(f));
196}
197
198/* -----------------------------------------------------------------------
199 * read_flag – shadows ctoon_read_flag
200 * -------------------------------------------------------------------- */
201
202enum class read_flag : uint32_t {
203 NOFLAG = static_cast<uint32_t>(CTOON_READ_NOFLAG),
204 INSITU = static_cast<uint32_t>(CTOON_READ_INSITU),
205 STOP_WHEN_DONE = static_cast<uint32_t>(CTOON_READ_STOP_WHEN_DONE),
206 ALLOW_TRAILING_COMMAS = static_cast<uint32_t>(CTOON_READ_ALLOW_TRAILING_COMMAS),
207 ALLOW_COMMENTS = static_cast<uint32_t>(CTOON_READ_ALLOW_COMMENTS),
208 ALLOW_INF_AND_NAN = static_cast<uint32_t>(CTOON_READ_ALLOW_INF_AND_NAN),
209 NUMBER_AS_RAW = static_cast<uint32_t>(CTOON_READ_NUMBER_AS_RAW),
210 ALLOW_INVALID_UNICODE = static_cast<uint32_t>(CTOON_READ_ALLOW_INVALID_UNICODE),
211 BIGNUM_AS_RAW = static_cast<uint32_t>(CTOON_READ_BIGNUM_AS_RAW),
212 ALLOW_BOM = static_cast<uint32_t>(CTOON_READ_ALLOW_BOM),
213 ALLOW_EXT_NUMBER = static_cast<uint32_t>(CTOON_READ_ALLOW_EXT_NUMBER),
214 ALLOW_EXT_ESCAPE = static_cast<uint32_t>(CTOON_READ_ALLOW_EXT_ESCAPE),
215 ALLOW_EXT_WHITESPACE = static_cast<uint32_t>(CTOON_READ_ALLOW_EXT_WHITESPACE),
216 ALLOW_SINGLE_QUOTED_STR = static_cast<uint32_t>(CTOON_READ_ALLOW_SINGLE_QUOTED_STR),
217 ALLOW_UNQUOTED_KEY = static_cast<uint32_t>(CTOON_READ_ALLOW_UNQUOTED_KEY),
218};
219
220inline read_flag operator|(read_flag l, read_flag r) CTOON_NOEXCEPT {
221 return static_cast<read_flag>(static_cast<uint32_t>(l) | static_cast<uint32_t>(r));
222}
223inline read_flag &operator|=(read_flag &l, read_flag r) CTOON_NOEXCEPT {
224 l = l | r; return l;
225}
227inline ctoon_read_flag to_c(read_flag f) CTOON_NOEXCEPT {
228 return static_cast<ctoon_read_flag>(static_cast<uint32_t>(f));
229}
230
231/* -----------------------------------------------------------------------
232 * write_options – builder pattern
233 * -------------------------------------------------------------------- */
234
236public:
237 write_options() CTOON_NOEXCEPT
238 : flag_(write_flag::NOFLAG), delimiter_(delimiter::COMMA), indent_(2) {}
239
240 write_options &with_flag(write_flag f) CTOON_NOEXCEPT { flag_ = f; return *this; }
241 write_options &with_delimiter(delimiter d) CTOON_NOEXCEPT { delimiter_ = d; return *this; }
242 write_options &with_indent(int i) CTOON_NOEXCEPT { indent_ = i; return *this; }
243
244 write_flag flag() const CTOON_NOEXCEPT { return flag_; }
245 delimiter get_delimiter() const CTOON_NOEXCEPT { return delimiter_; }
246 int indent() const CTOON_NOEXCEPT { return indent_; }
247
248 ctoon_write_options to_c() const CTOON_NOEXCEPT {
249 ctoon_write_options o;
250 o.flag = ctoon::to_c(flag_);
251 o.delimiter = static_cast<ctoon_delimiter>(delimiter_);
252 o.indent = indent_;
253 return o;
254 }
255private:
256 write_flag flag_;
257 delimiter delimiter_;
258 int indent_;
259};
260
261/* -----------------------------------------------------------------------
262 * Forward declarations
263 * -------------------------------------------------------------------- */
264
265class value;
266class mut_value;
267class document;
268class mut_document;
269
270/* -----------------------------------------------------------------------
271 * value – non-owning immutable view over ctoon_val*
272 * -------------------------------------------------------------------- */
273
274class value {
275public:
276 value() CTOON_NOEXCEPT : v_(NULL) {}
277 explicit value(ctoon_val *v) CTOON_NOEXCEPT : v_(v) {}
278
279 ctoon_val *raw() const CTOON_NOEXCEPT { return v_; }
280 bool valid() const CTOON_NOEXCEPT { return v_ != NULL; }
281 operator bool() const CTOON_NOEXCEPT { return valid(); }
282
283 /* --- type predicates --- */
284 bool is_null() const CTOON_NOEXCEPT { return v_ && ctoon_is_null(v_); }
285 bool is_true() const CTOON_NOEXCEPT { return v_ && ctoon_is_true(v_); }
286 bool is_false() const CTOON_NOEXCEPT { return v_ && ctoon_is_false(v_); }
287 bool is_bool() const CTOON_NOEXCEPT { return v_ && ctoon_is_bool(v_); }
288 bool is_uint() const CTOON_NOEXCEPT { return v_ && ctoon_is_uint(v_); }
289 bool is_sint() const CTOON_NOEXCEPT { return v_ && ctoon_is_sint(v_); }
290 bool is_int() const CTOON_NOEXCEPT { return v_ && ctoon_is_int(v_); }
291 bool is_real() const CTOON_NOEXCEPT { return v_ && ctoon_is_real(v_); }
292 bool is_num() const CTOON_NOEXCEPT { return v_ && ctoon_is_num(v_); }
293 bool is_str() const CTOON_NOEXCEPT { return v_ && ctoon_is_str(v_); }
294 bool is_arr() const CTOON_NOEXCEPT { return v_ && ctoon_is_arr(v_); }
295 bool is_obj() const CTOON_NOEXCEPT { return v_ && ctoon_is_obj(v_); }
296
297 /* --- scalar getters --- */
298 bool get_bool() const CTOON_NOEXCEPT { return v_ && ctoon_get_bool(v_); }
299 std::uint64_t get_uint() const CTOON_NOEXCEPT { return v_ ? ctoon_get_uint(v_) : 0; }
300 std::int64_t get_sint() const CTOON_NOEXCEPT { return v_ ? ctoon_get_sint(v_) : 0; }
301 double get_real() const CTOON_NOEXCEPT { return v_ ? ctoon_get_real(v_) : 0.0; }
302 double get_num() const CTOON_NOEXCEPT { return v_ ? ctoon_get_num(v_) : 0.0; }
303 std::size_t get_len() const CTOON_NOEXCEPT { return v_ ? ctoon_get_len(v_) : 0; }
304 string_view get_str() const CTOON_NOEXCEPT {
305 if (!v_) return string_view();
306 const char *s = ctoon_get_str(v_);
307 return s ? string_view(s, ctoon_get_len(v_)) : string_view();
308 }
309
310 /* --- array access --- */
311 std::size_t arr_size() const CTOON_NOEXCEPT { return v_ ? ctoon_arr_size(v_) : 0; }
312 value arr_get(std::size_t i) const CTOON_NOEXCEPT { return value(v_ ? ctoon_arr_get(v_, i) : NULL); }
313 value arr_first() const CTOON_NOEXCEPT { return value(v_ ? ctoon_arr_get_first(v_) : NULL); }
314 value arr_last() const CTOON_NOEXCEPT { return value(v_ ? ctoon_arr_get_last(v_) : NULL); }
315 value operator[](std::size_t i) const CTOON_NOEXCEPT { return arr_get(i); }
316
317 /* --- object access --- */
318 std::size_t obj_size() const CTOON_NOEXCEPT { return v_ ? ctoon_obj_size(v_) : 0; }
319 value obj_get(const char *key) const CTOON_NOEXCEPT { return value(v_ ? ctoon_obj_get(v_, key) : NULL); }
320 value obj_get(const string_view &key) const CTOON_NOEXCEPT {
321 return value(v_ ? ctoon_obj_getn(v_, key.data(), key.size()) : NULL);
322 }
323 value operator[](const char *key) const CTOON_NOEXCEPT { return obj_get(key); }
324 value operator[](const string_view &key) const CTOON_NOEXCEPT { return obj_get(key); }
325
326 /* --- equality --- */
327 bool equals(const value &rhs) const CTOON_NOEXCEPT { return v_ && rhs.v_ && ctoon_equals(v_, rhs.v_); }
328 bool equals(const char *s) const CTOON_NOEXCEPT { return v_ && ctoon_equals_str(v_, s); }
329 bool equals(const string_view &s) const CTOON_NOEXCEPT { return v_ && ctoon_equals_strn(v_, s.data(), s.size()); }
330
331 /* --- pointer path --- */
332 value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
333 return value(v_ ? ctoon_ptr_getn(v_, ptr.data(), ptr.size()) : NULL);
334 }
335
336 /* --- serialisation --- */
338 ctoon_write_options co = opts.to_c();
339 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
340 std::size_t len = 0;
341 char *raw = ctoon_val_write_opts(v_, &co, NULL, &len, &err);
342 if (!raw) throw write_error(err);
343 return write_result(raw, len);
344 }
345
346 /* --- iterators --- */
348 class arr_iterator {
349 public:
350 explicit arr_iterator(ctoon_val *arr, bool end = false) : cur_(NULL) {
351 if (!arr || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
352 ctoon_arr_iter_init(arr, &iter_);
353 cur_ = ctoon_arr_iter_next(&iter_);
354 }
355 value operator*() const CTOON_NOEXCEPT { return value(cur_); }
356 arr_iterator &operator++() CTOON_NOEXCEPT { cur_ = ctoon_arr_iter_next(&iter_); return *this; }
357 bool operator!=(const arr_iterator &o) const CTOON_NOEXCEPT { return cur_ != o.cur_; }
358 private:
359 ctoon_arr_iter iter_;
360 ctoon_val *cur_;
361 };
362 struct arr_range {
363 ctoon_val *arr;
364 arr_iterator begin() const { return arr_iterator(arr); }
365 arr_iterator end() const { return arr_iterator(arr, true); }
366 };
367 arr_range arr_items() const CTOON_NOEXCEPT { arr_range r; r.arr = v_; return r; }
368
369 struct kv_pair {
370 ctoon_val *key_raw, *val_raw;
371 value key() const { return value(key_raw); }
372 value val() const { return value(val_raw); }
373 };
374 class obj_iterator {
375 public:
376 explicit obj_iterator(ctoon_val *obj, bool end = false) : key_(NULL) {
377 if (!obj || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
378 ctoon_obj_iter_init(obj, &iter_);
379 key_ = ctoon_obj_iter_next(&iter_);
380 }
381 kv_pair operator*() const CTOON_NOEXCEPT {
382 kv_pair kv; kv.key_raw = key_; kv.val_raw = ctoon_obj_iter_get_val(key_); return kv;
383 }
384 obj_iterator &operator++() CTOON_NOEXCEPT { key_ = ctoon_obj_iter_next(&iter_); return *this; }
385 bool operator!=(const obj_iterator &o) const CTOON_NOEXCEPT { return key_ != o.key_; }
386 private:
387 ctoon_obj_iter iter_;
388 ctoon_val *key_;
389 };
390 struct obj_range {
391 ctoon_val *obj;
392 obj_iterator begin() const { return obj_iterator(obj); }
393 obj_iterator end() const { return obj_iterator(obj, true); }
394 };
395 obj_range obj_items() const CTOON_NOEXCEPT { obj_range r; r.obj = v_; return r; }
397
398protected:
399 ctoon_val *v_;
400};
401
402/* -----------------------------------------------------------------------
403 * mut_value – non-owning mutable view over ctoon_mut_val*
404 * -------------------------------------------------------------------- */
405
407public:
408 mut_value() CTOON_NOEXCEPT : v_(NULL) {}
409 explicit mut_value(ctoon_mut_val *v) CTOON_NOEXCEPT : v_(v) {}
410
411 ctoon_mut_val *raw() const CTOON_NOEXCEPT { return v_; }
412 bool valid() const CTOON_NOEXCEPT { return v_ != NULL; }
413 operator bool() const CTOON_NOEXCEPT { return valid(); }
414
415 /* --- type predicates --- */
416 bool is_null() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_null(v_); }
417 bool is_true() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_true(v_); }
418 bool is_false() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_false(v_); }
419 bool is_bool() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_bool(v_); }
420 bool is_uint() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_uint(v_); }
421 bool is_sint() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_sint(v_); }
422 bool is_int() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_int(v_); }
423 bool is_real() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_real(v_); }
424 bool is_num() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_num(v_); }
425 bool is_str() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_str(v_); }
426 bool is_arr() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_arr(v_); }
427 bool is_obj() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_obj(v_); }
428
429 /* --- scalar getters --- */
430 bool get_bool() const CTOON_NOEXCEPT { return v_ && ctoon_mut_get_bool(v_); }
431 std::uint64_t get_uint() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_uint(v_) : 0; }
432 std::int64_t get_sint() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_sint(v_) : 0; }
433 double get_real() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_real(v_) : 0.0; }
434 double get_num() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_num(v_) : 0.0; }
435 std::size_t get_len() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_len(v_) : 0; }
436 string_view get_str() const CTOON_NOEXCEPT {
437 if (!v_) return string_view();
438 const char *s = ctoon_mut_get_str(v_);
439 return s ? string_view(s, ctoon_mut_get_len(v_)) : string_view();
440 }
441
442 /* --- array access --- */
443 std::size_t arr_size() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_arr_size(v_) : 0; }
444 mut_value arr_get(std::size_t i) const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_arr_get(v_, i) : NULL); }
445 mut_value arr_first() const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_arr_get_first(v_) : NULL); }
446 mut_value arr_last() const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_arr_get_last(v_) : NULL); }
447 mut_value operator[](std::size_t i) const CTOON_NOEXCEPT { return arr_get(i); }
448
449 /* --- object access --- */
450 std::size_t obj_size() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_obj_size(v_) : 0; }
451 mut_value obj_get(const char *key) const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_obj_get(v_, key) : NULL); }
452 mut_value obj_get(const string_view &key) const CTOON_NOEXCEPT {
453 return mut_value(v_ ? ctoon_mut_obj_getn(v_, key.data(), key.size()) : NULL);
454 }
455 mut_value operator[](const char *key) const CTOON_NOEXCEPT { return obj_get(key); }
456 mut_value operator[](const string_view &key) const CTOON_NOEXCEPT { return obj_get(key); }
457
458 /* --- equality --- */
459 bool equals(const mut_value &rhs) const CTOON_NOEXCEPT { return v_ && rhs.v_ && ctoon_mut_equals(v_, rhs.v_); }
460 bool equals(const char *s) const CTOON_NOEXCEPT { return v_ && ctoon_mut_equals_str(v_, s); }
461 bool equals(const string_view &s) const CTOON_NOEXCEPT { return v_ && ctoon_mut_equals_strn(v_, s.data(), s.size()); }
462
463 /* --- pointer --- */
464 mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
465 return mut_value(v_ ? ctoon_mut_ptr_getn(v_, ptr.data(), ptr.size()) : NULL);
466 }
467
468 /* --- array mutation --- */
469 bool arr_append(mut_value item) const CTOON_NOEXCEPT { return v_ && item.v_ && ctoon_mut_arr_append(v_, item.v_); }
470 bool arr_prepend(mut_value item) const CTOON_NOEXCEPT { return v_ && item.v_ && ctoon_mut_arr_prepend(v_, item.v_); }
471 bool arr_clear() const CTOON_NOEXCEPT { return v_ && ctoon_mut_arr_clear(v_); }
472 mut_value arr_remove(std::size_t i) const CTOON_NOEXCEPT {
473 return mut_value(v_ ? ctoon_mut_arr_remove(v_, i) : NULL);
474 }
475
476 /* --- object mutation --- */
477 bool obj_add(mut_value key, mut_value val) const CTOON_NOEXCEPT {
478 return v_ && key.v_ && val.v_ && ctoon_mut_obj_add(v_, key.v_, val.v_);
479 }
480 bool obj_put(mut_value key, mut_value val) const CTOON_NOEXCEPT {
481 return v_ && key.v_ && val.v_ && ctoon_mut_obj_put(v_, key.v_, val.v_);
482 }
483 bool obj_clear() const CTOON_NOEXCEPT { return v_ && ctoon_mut_obj_clear(v_); }
484 mut_value obj_remove(const string_view &key) const CTOON_NOEXCEPT {
485 return mut_value(v_ ? ctoon_mut_obj_remove_keyn(v_, key.data(), key.size()) : NULL);
486 }
487
488 /* --- iterators --- */
490 class arr_iterator {
491 public:
492 explicit arr_iterator(ctoon_mut_val *arr, bool end = false) : cur_(NULL) {
493 if (!arr || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
494 ctoon_mut_arr_iter_init(arr, &iter_);
495 cur_ = ctoon_mut_arr_iter_next(&iter_);
496 }
497 mut_value operator*() const CTOON_NOEXCEPT { return mut_value(cur_); }
498 arr_iterator &operator++() CTOON_NOEXCEPT { cur_ = ctoon_mut_arr_iter_next(&iter_); return *this; }
499 bool operator!=(const arr_iterator &o) const CTOON_NOEXCEPT { return cur_ != o.cur_; }
500 private:
501 ctoon_mut_arr_iter iter_;
502 ctoon_mut_val *cur_;
503 };
504 struct arr_range {
505 ctoon_mut_val *arr;
506 arr_iterator begin() const { return arr_iterator(arr); }
507 arr_iterator end() const { return arr_iterator(arr, true); }
508 };
509 arr_range arr_items() const CTOON_NOEXCEPT { arr_range r; r.arr = v_; return r; }
510
511 struct kv_pair {
512 ctoon_mut_val *key_raw, *val_raw;
513 mut_value key() const { return mut_value(key_raw); }
514 mut_value val() const { return mut_value(val_raw); }
515 };
516 class obj_iterator {
517 public:
518 explicit obj_iterator(ctoon_mut_val *obj, bool end = false) : key_(NULL) {
519 if (!obj || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
520 ctoon_mut_obj_iter_init(obj, &iter_);
521 key_ = ctoon_mut_obj_iter_next(&iter_);
522 }
523 kv_pair operator*() const CTOON_NOEXCEPT {
524 kv_pair kv; kv.key_raw = key_; kv.val_raw = ctoon_mut_obj_iter_get_val(key_); return kv;
525 }
526 obj_iterator &operator++() CTOON_NOEXCEPT { key_ = ctoon_mut_obj_iter_next(&iter_); return *this; }
527 bool operator!=(const obj_iterator &o) const CTOON_NOEXCEPT { return key_ != o.key_; }
528 private:
529 ctoon_mut_obj_iter iter_;
530 ctoon_mut_val *key_;
531 };
532 struct obj_range {
533 ctoon_mut_val *obj;
534 obj_iterator begin() const { return obj_iterator(obj); }
535 obj_iterator end() const { return obj_iterator(obj, true); }
536 };
537 obj_range obj_items() const CTOON_NOEXCEPT { obj_range r; r.obj = v_; return r; }
539
540protected:
541 ctoon_mut_val *v_;
542};
543
544/* -----------------------------------------------------------------------
545 * document – RAII owner of ctoon_doc*
546 * -------------------------------------------------------------------- */
547
548class document {
549public:
550 document() CTOON_NOEXCEPT : doc_(NULL) {}
551 explicit document(ctoon_doc *doc) CTOON_NOEXCEPT : doc_(doc) {}
552 ~document() { ctoon_doc_free(doc_); }
553
554 document(document &&o) CTOON_NOEXCEPT : doc_(o.doc_) { o.doc_ = NULL; }
555 document &operator=(document &&o) CTOON_NOEXCEPT {
556 if (this != &o) { ctoon_doc_free(doc_); doc_ = o.doc_; o.doc_ = NULL; }
557 return *this;
558 }
559
560 /* --- parse factories --- */
561 static document parse(const char *toon, std::size_t len,
563 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
564 ctoon_doc *d = ctoon_read_opts(const_cast<char *>(toon), len, to_c(flags), NULL, &err);
565 if (!d) throw parse_error(err);
566 return document(d);
567 }
568 static document parse(const char *toon, read_flag flags = read_flag::NOFLAG) {
569 return parse(toon, std::strlen(toon), flags);
570 }
571 static document parse(const std::string &toon, read_flag flags = read_flag::NOFLAG) {
572 return parse(toon.data(), toon.size(), flags);
573 }
574 static document parse(const string_view &toon, read_flag flags = read_flag::NOFLAG) {
575 return parse(toon.data(), toon.size(), flags);
576 }
577 /* Same as parse(), but with a custom indentSize (spec §12) — the
578 * number of spaces that count as one indentation level (default 2). */
579 static document parse_indent(const char *toon, std::size_t len, int indent_size,
581 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
582 ctoon_doc *d = ctoon_read_opts_indent(const_cast<char *>(toon), len,
583 to_c(flags), indent_size, NULL, &err);
584 if (!d) throw parse_error(err);
585 return document(d);
586 }
587 static document parse_indent(const std::string &toon, int indent_size,
589 return parse_indent(toon.data(), toon.size(), indent_size, flags);
590 }
591 static document parse_indent(const string_view &toon, int indent_size,
593 return parse_indent(toon.data(), toon.size(), indent_size, flags);
594 }
595 static document parse_file(const char *path, read_flag flags = read_flag::NOFLAG) {
596 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
597 ctoon_doc *d = ctoon_read_file(path, to_c(flags), NULL, &err);
598 if (!d) throw parse_error(err);
599 return document(d);
600 }
601
602#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
603 static document from_json(const char *json, std::size_t len,
605 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
606 ctoon_doc *d = ctoon_read_json(const_cast<char *>(json), len, to_c(flags), NULL, &err);
607 if (!d) throw parse_error(err);
608 return document(d);
609 }
610 static document from_json(const char *json, read_flag flags = read_flag::NOFLAG) {
611 return from_json(json, std::strlen(json), flags);
612 }
613 static document from_json(const std::string &json, read_flag flags = read_flag::NOFLAG) {
614 return from_json(json.data(), json.size(), flags);
615 }
616 static document from_json(const string_view &json, read_flag flags = read_flag::NOFLAG) {
617 return from_json(json.data(), json.size(), flags);
618 }
619 static document from_json_file(const char *path, read_flag flags = read_flag::NOFLAG) {
620 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
621 ctoon_doc *d = ctoon_read_json_file(path, to_c(flags), NULL, &err);
622 if (!d) throw parse_error(err);
623 return document(d);
624 }
625#endif /* CTOON_ENABLE_JSON */
626
627 /* --- access --- */
628 bool valid() const CTOON_NOEXCEPT { return doc_ != NULL; }
629 operator bool() const CTOON_NOEXCEPT { return valid(); }
630 value root() const CTOON_NOEXCEPT { return value(ctoon_doc_get_root(doc_)); }
631 value operator*() const CTOON_NOEXCEPT { return root(); }
632 ctoon_doc *raw() const CTOON_NOEXCEPT { return doc_; }
633 std::size_t read_size() const CTOON_NOEXCEPT { return ctoon_doc_get_read_size(doc_); }
634 std::size_t val_count() const CTOON_NOEXCEPT { return ctoon_doc_get_val_count(doc_); }
635
636 value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
637 return value(ctoon_doc_ptr_getn(doc_, ptr.data(), ptr.size()));
638 }
639
640 /* --- TOON serialisation --- */
642 ctoon_write_options co = opts.to_c();
643 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
644 std::size_t len = 0;
645 char *raw = ctoon_write_opts(doc_, &co, NULL, &len, &err);
646 if (!raw) throw write_error(err);
647 return write_result(raw, len);
648 }
649 void write_file(const char *path,
650 const write_options &opts = write_options()) const {
651 ctoon_write_options co = opts.to_c();
652 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
653 if (!ctoon_write_file(path, doc_, &co, NULL, &err)) throw write_error(err);
654 }
655
656 /* --- JSON serialisation --- */
657#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
658 write_result to_json(int indent = 2,
659 write_flag flags = write_flag::NOFLAG) const {
660 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
661 std::size_t len = 0;
662 char *raw = ctoon_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
663 if (!raw) throw write_error(err);
664 return write_result(raw, len);
665 }
666 void to_json_file(const char *path, int indent = 2,
667 write_flag flags = write_flag::NOFLAG) const {
668 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
669 std::size_t len = 0;
670 char *raw = ctoon_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
671 if (!raw) throw write_error(err);
672 FILE *fp = std::fopen(path, "wb");
673 if (!fp) { std::free(raw); throw write_error(err); }
674 std::fwrite(raw, 1, len, fp);
675 std::fclose(fp);
676 std::free(raw);
677 }
678#endif /* CTOON_ENABLE_JSON */
679
680 /* --- convert to mutable --- */
681 mut_document mut_copy() const; /* defined after mut_document */
682
683 ctoon_doc *release() CTOON_NOEXCEPT { ctoon_doc *d = doc_; doc_ = NULL; return d; }
684
685 ctoon_doc *doc_;
686};
687
688/* -----------------------------------------------------------------------
689 * mut_document – RAII owner of ctoon_mut_doc*
690 * -------------------------------------------------------------------- */
691
693public:
694 mut_document() CTOON_NOEXCEPT : doc_(NULL) {}
695 explicit mut_document(ctoon_mut_doc *doc) CTOON_NOEXCEPT : doc_(doc) {}
696 ~mut_document() { ctoon_mut_doc_free(doc_); }
697
698 mut_document(mut_document &&o) CTOON_NOEXCEPT : doc_(o.doc_) { o.doc_ = NULL; }
699 mut_document &operator=(mut_document &&o) CTOON_NOEXCEPT {
700 if (this != &o) { ctoon_mut_doc_free(doc_); doc_ = o.doc_; o.doc_ = NULL; }
701 return *this;
702 }
703
705 ctoon_mut_doc *d = ctoon_mut_doc_new(NULL);
706 if (!d) throw std::bad_alloc();
707 return mut_document(d);
708 }
709
710 /* --- access --- */
711 bool valid() const CTOON_NOEXCEPT { return doc_ != NULL; }
712 operator bool() const CTOON_NOEXCEPT { return valid(); }
713 mut_value root() const CTOON_NOEXCEPT { return mut_value(ctoon_mut_doc_get_root(doc_)); }
714 mut_value operator*() const CTOON_NOEXCEPT { return root(); }
715 void set_root(mut_value v) CTOON_NOEXCEPT { ctoon_mut_doc_set_root(doc_, v.raw()); }
716 ctoon_mut_doc *raw() const CTOON_NOEXCEPT { return doc_; }
717
718 mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
719 return mut_value(ctoon_mut_doc_ptr_getn(doc_, ptr.data(), ptr.size()));
720 }
721
722 /* --- value factories --- */
723 mut_value make_null() CTOON_NOEXCEPT { return mut_value(ctoon_mut_null(doc_)); }
724 mut_value make_true() CTOON_NOEXCEPT { return mut_value(ctoon_mut_true(doc_)); }
725 mut_value make_false() CTOON_NOEXCEPT { return mut_value(ctoon_mut_false(doc_)); }
726 mut_value make_bool(bool v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_bool(doc_, v)); }
727 mut_value make_uint(std::uint64_t v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_uint(doc_, v)); }
728 mut_value make_sint(std::int64_t v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_sint(doc_, v)); }
729 mut_value make_real(double v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_real(doc_, v)); }
730 mut_value make_arr() CTOON_NOEXCEPT { return mut_value(ctoon_mut_arr(doc_)); }
731 mut_value make_obj() CTOON_NOEXCEPT { return mut_value(ctoon_mut_obj(doc_)); }
732 mut_value make_str(const char *s) CTOON_NOEXCEPT { return mut_value(ctoon_mut_strcpy(doc_, s)); }
733 mut_value make_str(const std::string &s) CTOON_NOEXCEPT {
734 return mut_value(ctoon_mut_strncpy(doc_, s.data(), s.size()));
735 }
736 mut_value make_str(const string_view &s) CTOON_NOEXCEPT {
737 return mut_value(ctoon_mut_strncpy(doc_, s.data(), s.size()));
738 }
739
740 /* --- TOON serialisation --- */
742 ctoon_write_options co = opts.to_c();
743 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
744 std::size_t len = 0;
745 char *raw = ctoon_mut_write_opts(doc_, &co, NULL, &len, &err);
746 if (!raw) throw write_error(err);
747 return write_result(raw, len);
748 }
749 void write_file(const char *path,
750 const write_options &opts = write_options()) const {
751 ctoon_write_options co = opts.to_c();
752 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
753 if (!ctoon_mut_write_file(path, doc_, &co, NULL, &err)) throw write_error(err);
754 }
755
756 /* --- JSON serialisation --- */
757#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
758 write_result to_json(int indent = 2,
759 write_flag flags = write_flag::NOFLAG) const {
760 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
761 std::size_t len = 0;
762 char *raw = ctoon_mut_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
763 if (!raw) throw write_error(err);
764 return write_result(raw, len);
765 }
766 void to_json_file(const char *path, int indent = 2,
767 write_flag flags = write_flag::NOFLAG) const {
768 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
769 std::size_t len = 0;
770 char *raw = ctoon_mut_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
771 if (!raw) throw write_error(err);
772 FILE *fp = std::fopen(path, "wb");
773 if (!fp) { std::free(raw); throw write_error(err); }
774 std::fwrite(raw, 1, len, fp);
775 std::fclose(fp);
776 std::free(raw);
777 }
778#endif /* CTOON_ENABLE_JSON */
779
780 ctoon_mut_doc *release() CTOON_NOEXCEPT { ctoon_mut_doc *d = doc_; doc_ = NULL; return d; }
781
782 ctoon_mut_doc *doc_;
783};
784
785/* --- deferred --- */
787 ctoon_mut_doc *d = ctoon_doc_mut_copy(doc_, NULL);
788 if (!d) throw std::bad_alloc();
789 return mut_document(d);
790}
791
792/* -----------------------------------------------------------------------
793 * Free-function API (thin wrappers, prefer member functions)
794 * -------------------------------------------------------------------- */
795
796inline document parse(const char *toon, read_flag flags = read_flag::NOFLAG) {
797 return document::parse(toon, flags);
798}
799inline document parse(const std::string &toon, read_flag flags = read_flag::NOFLAG) {
800 return document::parse(toon, flags);
801}
802inline document parse_file(const char *path, read_flag flags = read_flag::NOFLAG) {
803 return document::parse_file(path, flags);
804}
806inline write_result to_string(const document &d, const write_options &o = write_options()) { return d.to_string(o); }
807inline write_result to_string(const mut_document &d, const write_options &o = write_options()) { return d.to_string(o); }
808
809#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
810inline document from_json(const char *json, std::size_t len, read_flag flags = read_flag::NOFLAG) {
811 return document::from_json(json, len, flags);
812}
813inline document from_json(const char *json, read_flag flags = read_flag::NOFLAG) {
814 return document::from_json(json, flags);
815}
816inline document from_json(const std::string &json, read_flag flags = read_flag::NOFLAG) {
817 return document::from_json(json, flags);
818}
819inline document from_json_file(const char *path, read_flag flags = read_flag::NOFLAG) {
820 return document::from_json_file(path, flags);
821}
822inline write_result to_json(const document &d, int indent = 2, write_flag flags = write_flag::NOFLAG) {
823 return d.to_json(indent, flags);
824}
825inline write_result to_json(const mut_document &d, int indent = 2, write_flag flags = write_flag::NOFLAG) {
826 return d.to_json(indent, flags);
827}
828#endif /* CTOON_ENABLE_JSON */
829
830} // namespace ctoon
static document parse_indent(const char *toon, std::size_t len, int indent_size, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:579
document & operator=(document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:555
static document parse_indent(const string_view &toon, int indent_size, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:591
document(document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:554
ctoon_doc * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:632
void write_file(const char *path, const write_options &opts=write_options()) const
Definition ctoon.hpp:649
document(ctoon_doc *doc) CTOON_NOEXCEPT
Definition ctoon.hpp:551
std::size_t val_count() const CTOON_NOEXCEPT
Definition ctoon.hpp:634
ctoon_doc * doc_
Definition ctoon.hpp:685
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:628
static document parse(const string_view &toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:574
static document parse_indent(const std::string &toon, int indent_size, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:587
std::size_t read_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:633
value operator*() const CTOON_NOEXCEPT
Definition ctoon.hpp:631
value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:636
static document parse(const std::string &toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:571
ctoon_doc * release() CTOON_NOEXCEPT
Definition ctoon.hpp:683
static document parse(const char *toon, std::size_t len, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:561
document() CTOON_NOEXCEPT
Definition ctoon.hpp:550
write_result to_string(const write_options &opts=write_options()) const
Definition ctoon.hpp:641
mut_document mut_copy() const
Definition ctoon.hpp:786
static document parse_file(const char *path, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:595
static document parse(const char *toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:568
value root() const CTOON_NOEXCEPT
Definition ctoon.hpp:630
virtual const char * what() const CTOON_NOEXCEPT
Definition ctoon.hpp:106
std::string msg_
Definition ctoon.hpp:109
error(const std::string &msg) CTOON_NOEXCEPT
Definition ctoon.hpp:105
virtual ~error() CTOON_NOEXCEPT
Definition ctoon.hpp:107
mut_value make_uint(std::uint64_t v) CTOON_NOEXCEPT
Definition ctoon.hpp:727
mut_value make_false() CTOON_NOEXCEPT
Definition ctoon.hpp:725
mut_value make_arr() CTOON_NOEXCEPT
Definition ctoon.hpp:730
mut_value make_sint(std::int64_t v) CTOON_NOEXCEPT
Definition ctoon.hpp:728
void set_root(mut_value v) CTOON_NOEXCEPT
Definition ctoon.hpp:715
void write_file(const char *path, const write_options &opts=write_options()) const
Definition ctoon.hpp:749
mut_value make_bool(unsigned char v) CTOON_NOEXCEPT
Definition ctoon.hpp:726
mut_value make_str(const std::string &s) CTOON_NOEXCEPT
Definition ctoon.hpp:733
mut_document & operator=(mut_document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:699
mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:718
mut_value make_obj() CTOON_NOEXCEPT
Definition ctoon.hpp:731
write_result to_string(const write_options &opts=write_options()) const
Definition ctoon.hpp:741
mut_document() CTOON_NOEXCEPT
Definition ctoon.hpp:694
mut_document(ctoon_mut_doc *doc) CTOON_NOEXCEPT
Definition ctoon.hpp:695
ctoon_mut_doc * doc_
Definition ctoon.hpp:782
mut_value make_str(const string_view &s) CTOON_NOEXCEPT
Definition ctoon.hpp:736
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:711
mut_value root() const CTOON_NOEXCEPT
Definition ctoon.hpp:713
ctoon_mut_doc * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:716
mut_value make_real(double v) CTOON_NOEXCEPT
Definition ctoon.hpp:729
mut_value make_null() CTOON_NOEXCEPT
Definition ctoon.hpp:723
static mut_document create()
Definition ctoon.hpp:704
mut_document(mut_document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:698
mut_value make_str(const char *s) CTOON_NOEXCEPT
Definition ctoon.hpp:732
mut_value make_true() CTOON_NOEXCEPT
Definition ctoon.hpp:724
mut_value operator*() const CTOON_NOEXCEPT
Definition ctoon.hpp:714
ctoon_mut_doc * release() CTOON_NOEXCEPT
Definition ctoon.hpp:780
unsigned char equals(const mut_value &rhs) const CTOON_NOEXCEPT
Definition ctoon.hpp:459
mut_value obj_get(const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:452
mut_value arr_get(std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:444
ctoon_mut_val * v_
Definition ctoon.hpp:541
unsigned char obj_clear() const CTOON_NOEXCEPT
Definition ctoon.hpp:483
mut_value() CTOON_NOEXCEPT
Definition ctoon.hpp:408
unsigned char arr_append(mut_value item) const CTOON_NOEXCEPT
Definition ctoon.hpp:469
unsigned char is_obj() const CTOON_NOEXCEPT
Definition ctoon.hpp:427
std::int64_t get_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:432
mut_value arr_first() const CTOON_NOEXCEPT
Definition ctoon.hpp:445
unsigned char arr_prepend(mut_value item) const CTOON_NOEXCEPT
Definition ctoon.hpp:470
unsigned char is_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:421
unsigned char is_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:419
std::size_t arr_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:443
unsigned char get_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:430
unsigned char is_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:424
mut_value(ctoon_mut_val *v) CTOON_NOEXCEPT
Definition ctoon.hpp:409
mut_value operator[](const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:456
unsigned char is_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:423
unsigned char is_null() const CTOON_NOEXCEPT
Definition ctoon.hpp:416
double get_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:434
mut_value obj_get(const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:451
unsigned char arr_clear() const CTOON_NOEXCEPT
Definition ctoon.hpp:471
mut_value operator[](const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:455
std::size_t obj_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:450
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:412
unsigned char is_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:425
unsigned char equals(const string_view &s) const CTOON_NOEXCEPT
Definition ctoon.hpp:461
mut_value operator[](std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:447
unsigned char obj_put(mut_value key, mut_value val) const CTOON_NOEXCEPT
Definition ctoon.hpp:480
unsigned char is_int() const CTOON_NOEXCEPT
Definition ctoon.hpp:422
mut_value arr_last() const CTOON_NOEXCEPT
Definition ctoon.hpp:446
mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:464
std::size_t get_len() const CTOON_NOEXCEPT
Definition ctoon.hpp:435
double get_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:433
unsigned char is_arr() const CTOON_NOEXCEPT
Definition ctoon.hpp:426
unsigned char equals(const char *s) const CTOON_NOEXCEPT
Definition ctoon.hpp:460
unsigned char is_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:420
std::uint64_t get_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:431
unsigned char obj_add(mut_value key, mut_value val) const CTOON_NOEXCEPT
Definition ctoon.hpp:477
unsigned char is_false() const CTOON_NOEXCEPT
Definition ctoon.hpp:418
unsigned char is_true() const CTOON_NOEXCEPT
Definition ctoon.hpp:417
ctoon_mut_val * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:411
mut_value obj_remove(const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:484
mut_value arr_remove(std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:472
string_view get_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:436
parse_error(const ctoon_read_err &e)
Definition ctoon.hpp:116
std::size_t pos
Definition ctoon.hpp:115
ctoon_read_code code
Definition ctoon.hpp:114
virtual ~parse_error() CTOON_NOEXCEPT
Definition ctoon.hpp:118
value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:332
unsigned char equals(const string_view &s) const CTOON_NOEXCEPT
Definition ctoon.hpp:329
unsigned char is_int() const CTOON_NOEXCEPT
Definition ctoon.hpp:290
double get_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:302
std::size_t obj_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:318
std::int64_t get_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:300
unsigned char is_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:291
unsigned char is_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:292
unsigned char equals(const char *s) const CTOON_NOEXCEPT
Definition ctoon.hpp:328
value operator[](std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:315
unsigned char equals(const value &rhs) const CTOON_NOEXCEPT
Definition ctoon.hpp:327
unsigned char is_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:289
value() CTOON_NOEXCEPT
Definition ctoon.hpp:276
unsigned char get_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:298
std::size_t arr_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:311
value arr_first() const CTOON_NOEXCEPT
Definition ctoon.hpp:313
value operator[](const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:324
string_view get_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:304
value(ctoon_val *v) CTOON_NOEXCEPT
Definition ctoon.hpp:277
std::size_t get_len() const CTOON_NOEXCEPT
Definition ctoon.hpp:303
unsigned char is_true() const CTOON_NOEXCEPT
Definition ctoon.hpp:285
unsigned char is_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:293
unsigned char is_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:287
write_result to_string(const write_options &opts=write_options()) const
Definition ctoon.hpp:337
value obj_get(const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:319
unsigned char is_arr() const CTOON_NOEXCEPT
Definition ctoon.hpp:294
value arr_last() const CTOON_NOEXCEPT
Definition ctoon.hpp:314
value arr_get(std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:312
ctoon_val * v_
Definition ctoon.hpp:399
std::uint64_t get_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:299
value operator[](const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:323
unsigned char is_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:288
ctoon_val * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:279
value obj_get(const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:320
double get_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:301
unsigned char is_false() const CTOON_NOEXCEPT
Definition ctoon.hpp:286
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:280
unsigned char is_obj() const CTOON_NOEXCEPT
Definition ctoon.hpp:295
unsigned char is_null() const CTOON_NOEXCEPT
Definition ctoon.hpp:284
static string_view string() CTOON_NOEXCEPT
Definition ctoon.hpp:94
static unsigned int patch() CTOON_NOEXCEPT
Definition ctoon.hpp:92
static unsigned int minor() CTOON_NOEXCEPT
Definition ctoon.hpp:91
static unsigned int hex() CTOON_NOEXCEPT
Definition ctoon.hpp:93
static unsigned int major() CTOON_NOEXCEPT
Definition ctoon.hpp:90
virtual ~write_error() CTOON_NOEXCEPT
Definition ctoon.hpp:132
write_error(const ctoon_write_err &e)
Definition ctoon.hpp:130
ctoon_write_code code
Definition ctoon.hpp:129
write_options & with_indent(int i) CTOON_NOEXCEPT
Definition ctoon.hpp:242
int indent() const CTOON_NOEXCEPT
Definition ctoon.hpp:246
ctoon_write_options to_c() const CTOON_NOEXCEPT
Definition ctoon.hpp:248
write_flag flag() const CTOON_NOEXCEPT
Definition ctoon.hpp:244
write_options & with_delimiter(delimiter d) CTOON_NOEXCEPT
Definition ctoon.hpp:241
write_options & with_flag(write_flag f) CTOON_NOEXCEPT
Definition ctoon.hpp:240
delimiter get_delimiter() const CTOON_NOEXCEPT
Definition ctoon.hpp:245
write_options() CTOON_NOEXCEPT
Definition ctoon.hpp:237
write_result(write_result &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:144
std::size_t len_
Definition ctoon.hpp:156
write_result(char *p, std::size_t n) CTOON_NOEXCEPT
Definition ctoon.hpp:142
std::string str() const
Definition ctoon.hpp:154
string_view view() const CTOON_NOEXCEPT
Definition ctoon.hpp:153
const char * c_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:150
std::size_t size() const CTOON_NOEXCEPT
Definition ctoon.hpp:151
write_result & operator=(write_result &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:146
write_result() CTOON_NOEXCEPT
Definition ctoon.hpp:141
unsigned char empty() const CTOON_NOEXCEPT
Definition ctoon.hpp:152
write_flag
Definition ctoon.hpp:176
delimiter
Definition ctoon.hpp:163
ctoon_write_flag to_c(write_flag f) CTOON_NOEXCEPT
Convert to the C type for passing to C API.
Definition ctoon.hpp:194
write_flag operator|(write_flag l, write_flag r) CTOON_NOEXCEPT
Definition ctoon.hpp:187
read_flag
Definition ctoon.hpp:202
mut_document make_document()
Definition ctoon.hpp:805
write_result to_string(const document &d, const write_options &o=write_options())
Definition ctoon.hpp:806
write_flag & operator|=(write_flag &l, write_flag r) CTOON_NOEXCEPT
Definition ctoon.hpp:190
document parse_file(const char *path, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:802
document parse(const char *toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:796