CToon C++ 0.9.0
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 * TOON spec (the toon-format/spec release this build targets)
101 * -------------------------------------------------------------------- */
102
103class spec {
104public:
106 class tag {
107 public:
108 static unsigned int major() CTOON_NOEXCEPT { return CTOON_SPEC_TAG_MAJOR; }
109 static unsigned int minor() CTOON_NOEXCEPT { return CTOON_SPEC_TAG_MINOR; }
110 static unsigned int patch() CTOON_NOEXCEPT { return CTOON_SPEC_TAG_PATCH; }
111 static unsigned int hex() CTOON_NOEXCEPT { return CTOON_SPEC_TAG_HEX; }
112 static string_view string() CTOON_NOEXCEPT { return CTOON_SPEC_TAG_STRING; }
113 private:
114 tag();
115 };
116
118 class version {
119 public:
120 static string_view string() CTOON_NOEXCEPT { return CTOON_SPEC_VERSION_STRING; }
121 private:
122 version();
123 };
124
126 class date {
127 public:
128 static string_view string() CTOON_NOEXCEPT { return CTOON_SPEC_DATE_STRING; }
129 private:
130 date();
131 };
132private:
133 spec();
134};
135
136/* -----------------------------------------------------------------------
137 * Exception hierarchy
138 * -------------------------------------------------------------------- */
139
140class error : public std::exception {
141public:
142 explicit error(const std::string &msg) CTOON_NOEXCEPT : msg_(msg) {}
143 virtual const char *what() const CTOON_NOEXCEPT { return msg_.c_str(); }
144 virtual ~error() CTOON_NOEXCEPT {}
145protected:
146 std::string msg_;
147};
148
149class parse_error : public error {
150public:
151 ctoon_read_code code;
152 std::size_t pos;
153 explicit parse_error(const ctoon_read_err &e)
154 : error(build_msg(e)), code(e.code), pos(e.pos) {}
155 virtual ~parse_error() CTOON_NOEXCEPT {}
156private:
157 static std::string build_msg(const ctoon_read_err &e) {
158 std::string m(e.msg ? e.msg : "parse error");
159 char buf[32]; std::sprintf(buf, " (pos %zu)", e.pos);
160 return m + buf;
161 }
162};
163
164class write_error : public error {
165public:
166 ctoon_write_code code;
167 explicit write_error(const ctoon_write_err &e)
168 : error(e.msg ? e.msg : "write error"), code(e.code) {}
169 virtual ~write_error() CTOON_NOEXCEPT {}
170};
171
172/* -----------------------------------------------------------------------
173 * write_result
174 * -------------------------------------------------------------------- */
175
177public:
178 write_result() CTOON_NOEXCEPT : ptr_(NULL), len_(0) {}
179 write_result(char *p, std::size_t n) CTOON_NOEXCEPT : ptr_(p), len_(n) {}
180 ~write_result() { std::free(ptr_); }
181 write_result(write_result &&o) CTOON_NOEXCEPT
182 : ptr_(o.ptr_), len_(o.len_) { o.ptr_ = NULL; o.len_ = 0; }
183 write_result &operator=(write_result &&o) CTOON_NOEXCEPT {
184 if (this != &o) { std::free(ptr_); ptr_ = o.ptr_; len_ = o.len_; o.ptr_ = NULL; o.len_ = 0; }
185 return *this;
186 }
187 const char *c_str() const CTOON_NOEXCEPT { return ptr_ ? ptr_ : ""; }
188 std::size_t size() const CTOON_NOEXCEPT { return len_; }
189 bool empty() const CTOON_NOEXCEPT { return len_ == 0; }
190 string_view view() const CTOON_NOEXCEPT { return string_view(ptr_ ? ptr_ : "", len_); }
191 std::string str() const { return std::string(ptr_ ? ptr_ : "", len_); }
192 char *ptr_;
193 std::size_t len_;
194};
195
196/* -----------------------------------------------------------------------
197 * delimiter – shadows ctoon_delimiter
198 * -------------------------------------------------------------------- */
199
200enum class delimiter : uint32_t {
201 COMMA = CTOON_DELIMITER_COMMA,
202 TAB = CTOON_DELIMITER_TAB,
203 PIPE = CTOON_DELIMITER_PIPE,
204};
205
206/* -----------------------------------------------------------------------
207 * write_flag – shadows ctoon_write_flag
208 *
209 * Use these directly wherever ctoon_write_flag was used.
210 * Implicit cast to ctoon_write_flag is provided via to_c().
211 * -------------------------------------------------------------------- */
212
213enum class write_flag : uint32_t {
214 NOFLAG = static_cast<uint32_t>(CTOON_WRITE_NOFLAG),
215 ESCAPE_UNICODE = static_cast<uint32_t>(CTOON_WRITE_ESCAPE_UNICODE),
216 ESCAPE_SLASHES = static_cast<uint32_t>(CTOON_WRITE_ESCAPE_SLASHES),
217 ALLOW_INF_AND_NAN = static_cast<uint32_t>(CTOON_WRITE_ALLOW_INF_AND_NAN),
218 INF_AND_NAN_AS_NULL = static_cast<uint32_t>(CTOON_WRITE_INF_AND_NAN_AS_NULL),
219 ALLOW_INVALID_UNICODE = static_cast<uint32_t>(CTOON_WRITE_ALLOW_INVALID_UNICODE),
220 LENGTH_MARKER = static_cast<uint32_t>(CTOON_WRITE_LENGTH_MARKER),
221 NEWLINE_AT_END = static_cast<uint32_t>(CTOON_WRITE_NEWLINE_AT_END),
222};
223
224inline write_flag operator|(write_flag l, write_flag r) CTOON_NOEXCEPT {
225 return static_cast<write_flag>(static_cast<uint32_t>(l) | static_cast<uint32_t>(r));
226}
227inline write_flag &operator|=(write_flag &l, write_flag r) CTOON_NOEXCEPT {
228 l = l | r; return l;
229}
231inline ctoon_write_flag to_c(write_flag f) CTOON_NOEXCEPT {
232 return static_cast<ctoon_write_flag>(static_cast<uint32_t>(f));
233}
234
235/* -----------------------------------------------------------------------
236 * read_flag – shadows ctoon_read_flag
237 * -------------------------------------------------------------------- */
238
239enum class read_flag : uint32_t {
240 NOFLAG = static_cast<uint32_t>(CTOON_READ_NOFLAG),
241 INSITU = static_cast<uint32_t>(CTOON_READ_INSITU),
242 STOP_WHEN_DONE = static_cast<uint32_t>(CTOON_READ_STOP_WHEN_DONE),
243 ALLOW_TRAILING_COMMAS = static_cast<uint32_t>(CTOON_READ_ALLOW_TRAILING_COMMAS),
244 ALLOW_COMMENTS = static_cast<uint32_t>(CTOON_READ_ALLOW_COMMENTS),
245 ALLOW_INF_AND_NAN = static_cast<uint32_t>(CTOON_READ_ALLOW_INF_AND_NAN),
246 NUMBER_AS_RAW = static_cast<uint32_t>(CTOON_READ_NUMBER_AS_RAW),
247 ALLOW_INVALID_UNICODE = static_cast<uint32_t>(CTOON_READ_ALLOW_INVALID_UNICODE),
248 BIGNUM_AS_RAW = static_cast<uint32_t>(CTOON_READ_BIGNUM_AS_RAW),
249 ALLOW_BOM = static_cast<uint32_t>(CTOON_READ_ALLOW_BOM),
250 ALLOW_EXT_NUMBER = static_cast<uint32_t>(CTOON_READ_ALLOW_EXT_NUMBER),
251 ALLOW_EXT_ESCAPE = static_cast<uint32_t>(CTOON_READ_ALLOW_EXT_ESCAPE),
252 ALLOW_EXT_WHITESPACE = static_cast<uint32_t>(CTOON_READ_ALLOW_EXT_WHITESPACE),
253 ALLOW_SINGLE_QUOTED_STR = static_cast<uint32_t>(CTOON_READ_ALLOW_SINGLE_QUOTED_STR),
254 ALLOW_UNQUOTED_KEY = static_cast<uint32_t>(CTOON_READ_ALLOW_UNQUOTED_KEY),
255 NON_STRICT = static_cast<uint32_t>(CTOON_READ_NON_STRICT),
256};
257
258inline read_flag operator|(read_flag l, read_flag r) CTOON_NOEXCEPT {
259 return static_cast<read_flag>(static_cast<uint32_t>(l) | static_cast<uint32_t>(r));
260}
261inline read_flag &operator|=(read_flag &l, read_flag r) CTOON_NOEXCEPT {
262 l = l | r; return l;
263}
265inline ctoon_read_flag to_c(read_flag f) CTOON_NOEXCEPT {
266 return static_cast<ctoon_read_flag>(static_cast<uint32_t>(f));
267}
268
269/* -----------------------------------------------------------------------
270 * write_options – builder pattern
271 * -------------------------------------------------------------------- */
272
274public:
275 write_options() CTOON_NOEXCEPT
276 : flag_(write_flag::NOFLAG), delimiter_(delimiter::COMMA), indent_(2) {}
277
278 write_options &with_flag(write_flag f) CTOON_NOEXCEPT { flag_ = f; return *this; }
279 write_options &with_delimiter(delimiter d) CTOON_NOEXCEPT { delimiter_ = d; return *this; }
280 write_options &with_indent(int i) CTOON_NOEXCEPT { indent_ = i; return *this; }
281
282 write_flag flag() const CTOON_NOEXCEPT { return flag_; }
283 delimiter get_delimiter() const CTOON_NOEXCEPT { return delimiter_; }
284 int indent() const CTOON_NOEXCEPT { return indent_; }
285
286 ctoon_write_options to_c() const CTOON_NOEXCEPT {
287 ctoon_write_options o;
288 o.flag = ctoon::to_c(flag_);
289 o.delimiter = static_cast<ctoon_delimiter>(delimiter_);
290 o.indent = indent_;
291 return o;
292 }
293private:
294 write_flag flag_;
295 delimiter delimiter_;
296 int indent_;
297};
298
299/* -----------------------------------------------------------------------
300 * Forward declarations
301 * -------------------------------------------------------------------- */
302
303class value;
304class mut_value;
305class document;
306class mut_document;
307
308/* -----------------------------------------------------------------------
309 * value – non-owning immutable view over ctoon_val*
310 * -------------------------------------------------------------------- */
311
312class value {
313public:
314 value() CTOON_NOEXCEPT : v_(NULL) {}
315 explicit value(ctoon_val *v) CTOON_NOEXCEPT : v_(v) {}
316
317 ctoon_val *raw() const CTOON_NOEXCEPT { return v_; }
318 bool valid() const CTOON_NOEXCEPT { return v_ != NULL; }
319 operator bool() const CTOON_NOEXCEPT { return valid(); }
320
321 /* --- type predicates --- */
322 bool is_null() const CTOON_NOEXCEPT { return v_ && ctoon_is_null(v_); }
323 bool is_true() const CTOON_NOEXCEPT { return v_ && ctoon_is_true(v_); }
324 bool is_false() const CTOON_NOEXCEPT { return v_ && ctoon_is_false(v_); }
325 bool is_bool() const CTOON_NOEXCEPT { return v_ && ctoon_is_bool(v_); }
326 bool is_uint() const CTOON_NOEXCEPT { return v_ && ctoon_is_uint(v_); }
327 bool is_sint() const CTOON_NOEXCEPT { return v_ && ctoon_is_sint(v_); }
328 bool is_int() const CTOON_NOEXCEPT { return v_ && ctoon_is_int(v_); }
329 bool is_real() const CTOON_NOEXCEPT { return v_ && ctoon_is_real(v_); }
330 bool is_num() const CTOON_NOEXCEPT { return v_ && ctoon_is_num(v_); }
331 bool is_str() const CTOON_NOEXCEPT { return v_ && ctoon_is_str(v_); }
332 bool is_arr() const CTOON_NOEXCEPT { return v_ && ctoon_is_arr(v_); }
333 bool is_obj() const CTOON_NOEXCEPT { return v_ && ctoon_is_obj(v_); }
334
335 /* --- scalar getters --- */
336 bool get_bool() const CTOON_NOEXCEPT { return v_ && ctoon_get_bool(v_); }
337 std::uint64_t get_uint() const CTOON_NOEXCEPT { return v_ ? ctoon_get_uint(v_) : 0; }
338 std::int64_t get_sint() const CTOON_NOEXCEPT { return v_ ? ctoon_get_sint(v_) : 0; }
339 double get_real() const CTOON_NOEXCEPT { return v_ ? ctoon_get_real(v_) : 0.0; }
340 double get_num() const CTOON_NOEXCEPT { return v_ ? ctoon_get_num(v_) : 0.0; }
341 std::size_t get_len() const CTOON_NOEXCEPT { return v_ ? ctoon_get_len(v_) : 0; }
342 string_view get_str() const CTOON_NOEXCEPT {
343 if (!v_) return string_view();
344 const char *s = ctoon_get_str(v_);
345 return s ? string_view(s, ctoon_get_len(v_)) : string_view();
346 }
347
348 /* --- array access --- */
349 std::size_t arr_size() const CTOON_NOEXCEPT { return v_ ? ctoon_arr_size(v_) : 0; }
350 value arr_get(std::size_t i) const CTOON_NOEXCEPT { return value(v_ ? ctoon_arr_get(v_, i) : NULL); }
351 value arr_first() const CTOON_NOEXCEPT { return value(v_ ? ctoon_arr_get_first(v_) : NULL); }
352 value arr_last() const CTOON_NOEXCEPT { return value(v_ ? ctoon_arr_get_last(v_) : NULL); }
353 value operator[](std::size_t i) const CTOON_NOEXCEPT { return arr_get(i); }
354
355 /* --- object access --- */
356 std::size_t obj_size() const CTOON_NOEXCEPT { return v_ ? ctoon_obj_size(v_) : 0; }
357 value obj_get(const char *key) const CTOON_NOEXCEPT { return value(v_ ? ctoon_obj_get(v_, key) : NULL); }
358 value obj_get(const string_view &key) const CTOON_NOEXCEPT {
359 return value(v_ ? ctoon_obj_getn(v_, key.data(), key.size()) : NULL);
360 }
361 value operator[](const char *key) const CTOON_NOEXCEPT { return obj_get(key); }
362 value operator[](const string_view &key) const CTOON_NOEXCEPT { return obj_get(key); }
363
364 /* --- equality --- */
365 bool equals(const value &rhs) const CTOON_NOEXCEPT { return v_ && rhs.v_ && ctoon_equals(v_, rhs.v_); }
366 bool equals(const char *s) const CTOON_NOEXCEPT { return v_ && ctoon_equals_str(v_, s); }
367 bool equals(const string_view &s) const CTOON_NOEXCEPT { return v_ && ctoon_equals_strn(v_, s.data(), s.size()); }
368
369 /* --- pointer path --- */
370 value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
371 return value(v_ ? ctoon_ptr_getn(v_, ptr.data(), ptr.size()) : NULL);
372 }
373
374 /* --- serialisation --- */
376 ctoon_write_options co = opts.to_c();
377 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
378 std::size_t len = 0;
379 char *raw = ctoon_val_write_opts(v_, &co, NULL, &len, &err);
380 if (!raw) throw write_error(err);
381 return write_result(raw, len);
382 }
383
384 /* --- iterators --- */
386 class arr_iterator {
387 public:
388 explicit arr_iterator(ctoon_val *arr, bool end = false) : cur_(NULL) {
389 if (!arr || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
390 ctoon_arr_iter_init(arr, &iter_);
391 cur_ = ctoon_arr_iter_next(&iter_);
392 }
393 value operator*() const CTOON_NOEXCEPT { return value(cur_); }
394 arr_iterator &operator++() CTOON_NOEXCEPT { cur_ = ctoon_arr_iter_next(&iter_); return *this; }
395 bool operator!=(const arr_iterator &o) const CTOON_NOEXCEPT { return cur_ != o.cur_; }
396 private:
397 ctoon_arr_iter iter_;
398 ctoon_val *cur_;
399 };
400 struct arr_range {
401 ctoon_val *arr;
402 arr_iterator begin() const { return arr_iterator(arr); }
403 arr_iterator end() const { return arr_iterator(arr, true); }
404 };
405 arr_range arr_items() const CTOON_NOEXCEPT { arr_range r; r.arr = v_; return r; }
406
407 struct kv_pair {
408 ctoon_val *key_raw, *val_raw;
409 value key() const { return value(key_raw); }
410 value val() const { return value(val_raw); }
411 };
412 class obj_iterator {
413 public:
414 explicit obj_iterator(ctoon_val *obj, bool end = false) : key_(NULL) {
415 if (!obj || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
416 ctoon_obj_iter_init(obj, &iter_);
417 key_ = ctoon_obj_iter_next(&iter_);
418 }
419 kv_pair operator*() const CTOON_NOEXCEPT {
420 kv_pair kv; kv.key_raw = key_; kv.val_raw = ctoon_obj_iter_get_val(key_); return kv;
421 }
422 obj_iterator &operator++() CTOON_NOEXCEPT { key_ = ctoon_obj_iter_next(&iter_); return *this; }
423 bool operator!=(const obj_iterator &o) const CTOON_NOEXCEPT { return key_ != o.key_; }
424 private:
425 ctoon_obj_iter iter_;
426 ctoon_val *key_;
427 };
428 struct obj_range {
429 ctoon_val *obj;
430 obj_iterator begin() const { return obj_iterator(obj); }
431 obj_iterator end() const { return obj_iterator(obj, true); }
432 };
433 obj_range obj_items() const CTOON_NOEXCEPT { obj_range r; r.obj = v_; return r; }
435
436protected:
437 ctoon_val *v_;
438};
439
440/* -----------------------------------------------------------------------
441 * mut_value – non-owning mutable view over ctoon_mut_val*
442 * -------------------------------------------------------------------- */
443
445public:
446 mut_value() CTOON_NOEXCEPT : v_(NULL) {}
447 explicit mut_value(ctoon_mut_val *v) CTOON_NOEXCEPT : v_(v) {}
448
449 ctoon_mut_val *raw() const CTOON_NOEXCEPT { return v_; }
450 bool valid() const CTOON_NOEXCEPT { return v_ != NULL; }
451 operator bool() const CTOON_NOEXCEPT { return valid(); }
452
453 /* --- type predicates --- */
454 bool is_null() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_null(v_); }
455 bool is_true() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_true(v_); }
456 bool is_false() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_false(v_); }
457 bool is_bool() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_bool(v_); }
458 bool is_uint() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_uint(v_); }
459 bool is_sint() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_sint(v_); }
460 bool is_int() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_int(v_); }
461 bool is_real() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_real(v_); }
462 bool is_num() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_num(v_); }
463 bool is_str() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_str(v_); }
464 bool is_arr() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_arr(v_); }
465 bool is_obj() const CTOON_NOEXCEPT { return v_ && ctoon_mut_is_obj(v_); }
466
467 /* --- scalar getters --- */
468 bool get_bool() const CTOON_NOEXCEPT { return v_ && ctoon_mut_get_bool(v_); }
469 std::uint64_t get_uint() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_uint(v_) : 0; }
470 std::int64_t get_sint() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_sint(v_) : 0; }
471 double get_real() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_real(v_) : 0.0; }
472 double get_num() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_num(v_) : 0.0; }
473 std::size_t get_len() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_get_len(v_) : 0; }
474 string_view get_str() const CTOON_NOEXCEPT {
475 if (!v_) return string_view();
476 const char *s = ctoon_mut_get_str(v_);
477 return s ? string_view(s, ctoon_mut_get_len(v_)) : string_view();
478 }
479
480 /* --- array access --- */
481 std::size_t arr_size() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_arr_size(v_) : 0; }
482 mut_value arr_get(std::size_t i) const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_arr_get(v_, i) : NULL); }
483 mut_value arr_first() const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_arr_get_first(v_) : NULL); }
484 mut_value arr_last() const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_arr_get_last(v_) : NULL); }
485 mut_value operator[](std::size_t i) const CTOON_NOEXCEPT { return arr_get(i); }
486
487 /* --- object access --- */
488 std::size_t obj_size() const CTOON_NOEXCEPT { return v_ ? ctoon_mut_obj_size(v_) : 0; }
489 mut_value obj_get(const char *key) const CTOON_NOEXCEPT { return mut_value(v_ ? ctoon_mut_obj_get(v_, key) : NULL); }
490 mut_value obj_get(const string_view &key) const CTOON_NOEXCEPT {
491 return mut_value(v_ ? ctoon_mut_obj_getn(v_, key.data(), key.size()) : NULL);
492 }
493 mut_value operator[](const char *key) const CTOON_NOEXCEPT { return obj_get(key); }
494 mut_value operator[](const string_view &key) const CTOON_NOEXCEPT { return obj_get(key); }
495
496 /* --- equality --- */
497 bool equals(const mut_value &rhs) const CTOON_NOEXCEPT { return v_ && rhs.v_ && ctoon_mut_equals(v_, rhs.v_); }
498 bool equals(const char *s) const CTOON_NOEXCEPT { return v_ && ctoon_mut_equals_str(v_, s); }
499 bool equals(const string_view &s) const CTOON_NOEXCEPT { return v_ && ctoon_mut_equals_strn(v_, s.data(), s.size()); }
500
501 /* --- pointer --- */
502 mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
503 return mut_value(v_ ? ctoon_mut_ptr_getn(v_, ptr.data(), ptr.size()) : NULL);
504 }
505
506 /* --- array mutation --- */
507 bool arr_append(mut_value item) const CTOON_NOEXCEPT { return v_ && item.v_ && ctoon_mut_arr_append(v_, item.v_); }
508 bool arr_prepend(mut_value item) const CTOON_NOEXCEPT { return v_ && item.v_ && ctoon_mut_arr_prepend(v_, item.v_); }
509 bool arr_clear() const CTOON_NOEXCEPT { return v_ && ctoon_mut_arr_clear(v_); }
510 mut_value arr_remove(std::size_t i) const CTOON_NOEXCEPT {
511 return mut_value(v_ ? ctoon_mut_arr_remove(v_, i) : NULL);
512 }
513
514 /* --- object mutation --- */
515 bool obj_add(mut_value key, mut_value val) const CTOON_NOEXCEPT {
516 return v_ && key.v_ && val.v_ && ctoon_mut_obj_add(v_, key.v_, val.v_);
517 }
518 bool obj_put(mut_value key, mut_value val) const CTOON_NOEXCEPT {
519 return v_ && key.v_ && val.v_ && ctoon_mut_obj_put(v_, key.v_, val.v_);
520 }
521 bool obj_clear() const CTOON_NOEXCEPT { return v_ && ctoon_mut_obj_clear(v_); }
522 mut_value obj_remove(const string_view &key) const CTOON_NOEXCEPT {
523 return mut_value(v_ ? ctoon_mut_obj_remove_keyn(v_, key.data(), key.size()) : NULL);
524 }
525
526 /* --- iterators --- */
528 class arr_iterator {
529 public:
530 explicit arr_iterator(ctoon_mut_val *arr, bool end = false) : cur_(NULL) {
531 if (!arr || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
532 ctoon_mut_arr_iter_init(arr, &iter_);
533 cur_ = ctoon_mut_arr_iter_next(&iter_);
534 }
535 mut_value operator*() const CTOON_NOEXCEPT { return mut_value(cur_); }
536 arr_iterator &operator++() CTOON_NOEXCEPT { cur_ = ctoon_mut_arr_iter_next(&iter_); return *this; }
537 bool operator!=(const arr_iterator &o) const CTOON_NOEXCEPT { return cur_ != o.cur_; }
538 private:
539 ctoon_mut_arr_iter iter_;
540 ctoon_mut_val *cur_;
541 };
542 struct arr_range {
543 ctoon_mut_val *arr;
544 arr_iterator begin() const { return arr_iterator(arr); }
545 arr_iterator end() const { return arr_iterator(arr, true); }
546 };
547 arr_range arr_items() const CTOON_NOEXCEPT { arr_range r; r.arr = v_; return r; }
548
549 struct kv_pair {
550 ctoon_mut_val *key_raw, *val_raw;
551 mut_value key() const { return mut_value(key_raw); }
552 mut_value val() const { return mut_value(val_raw); }
553 };
554 class obj_iterator {
555 public:
556 explicit obj_iterator(ctoon_mut_val *obj, bool end = false) : key_(NULL) {
557 if (!obj || end) { std::memset(&iter_, 0, sizeof(iter_)); return; }
558 ctoon_mut_obj_iter_init(obj, &iter_);
559 key_ = ctoon_mut_obj_iter_next(&iter_);
560 }
561 kv_pair operator*() const CTOON_NOEXCEPT {
562 kv_pair kv; kv.key_raw = key_; kv.val_raw = ctoon_mut_obj_iter_get_val(key_); return kv;
563 }
564 obj_iterator &operator++() CTOON_NOEXCEPT { key_ = ctoon_mut_obj_iter_next(&iter_); return *this; }
565 bool operator!=(const obj_iterator &o) const CTOON_NOEXCEPT { return key_ != o.key_; }
566 private:
567 ctoon_mut_obj_iter iter_;
568 ctoon_mut_val *key_;
569 };
570 struct obj_range {
571 ctoon_mut_val *obj;
572 obj_iterator begin() const { return obj_iterator(obj); }
573 obj_iterator end() const { return obj_iterator(obj, true); }
574 };
575 obj_range obj_items() const CTOON_NOEXCEPT { obj_range r; r.obj = v_; return r; }
577
578protected:
579 ctoon_mut_val *v_;
580};
581
582/* -----------------------------------------------------------------------
583 * document – RAII owner of ctoon_doc*
584 * -------------------------------------------------------------------- */
585
586class document {
587public:
588 document() CTOON_NOEXCEPT : doc_(NULL) {}
589 explicit document(ctoon_doc *doc) CTOON_NOEXCEPT : doc_(doc) {}
590 ~document() { ctoon_doc_free(doc_); }
591
592 document(document &&o) CTOON_NOEXCEPT : doc_(o.doc_) { o.doc_ = NULL; }
593 document &operator=(document &&o) CTOON_NOEXCEPT {
594 if (this != &o) { ctoon_doc_free(doc_); doc_ = o.doc_; o.doc_ = NULL; }
595 return *this;
596 }
597
598 /* --- parse factories --- */
599 static document parse(const char *toon, std::size_t len,
601 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
602 ctoon_doc *d = ctoon_read_opts(const_cast<char *>(toon), len, to_c(flags), NULL, &err);
603 if (!d) throw parse_error(err);
604 return document(d);
605 }
606 static document parse(const char *toon, read_flag flags = read_flag::NOFLAG) {
607 return parse(toon, std::strlen(toon), flags);
608 }
609 static document parse(const std::string &toon, read_flag flags = read_flag::NOFLAG) {
610 return parse(toon.data(), toon.size(), flags);
611 }
612 static document parse(const string_view &toon, read_flag flags = read_flag::NOFLAG) {
613 return parse(toon.data(), toon.size(), flags);
614 }
615 /* Same as parse(), but with a custom indentSize (spec §12) - the
616 * number of spaces that count as one indentation level (default 2). */
617 static document parse_indent(const char *toon, std::size_t len, int indent_size,
619 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
620 ctoon_doc *d = ctoon_read_opts_indent(const_cast<char *>(toon), len,
621 to_c(flags), indent_size, NULL, &err);
622 if (!d) throw parse_error(err);
623 return document(d);
624 }
625 static document parse_indent(const std::string &toon, int indent_size,
627 return parse_indent(toon.data(), toon.size(), indent_size, flags);
628 }
629 static document parse_indent(const string_view &toon, int indent_size,
631 return parse_indent(toon.data(), toon.size(), indent_size, flags);
632 }
633 static document parse_file(const char *path, read_flag flags = read_flag::NOFLAG) {
634 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
635 ctoon_doc *d = ctoon_read_file(path, to_c(flags), NULL, &err);
636 if (!d) throw parse_error(err);
637 return document(d);
638 }
639
640#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
641 static document from_json(const char *json, std::size_t len,
643 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
644 ctoon_doc *d = ctoon_read_json(const_cast<char *>(json), len, to_c(flags), NULL, &err);
645 if (!d) throw parse_error(err);
646 return document(d);
647 }
648 static document from_json(const char *json, read_flag flags = read_flag::NOFLAG) {
649 return from_json(json, std::strlen(json), flags);
650 }
651 static document from_json(const std::string &json, read_flag flags = read_flag::NOFLAG) {
652 return from_json(json.data(), json.size(), flags);
653 }
654 static document from_json(const string_view &json, read_flag flags = read_flag::NOFLAG) {
655 return from_json(json.data(), json.size(), flags);
656 }
657 static document from_json_file(const char *path, read_flag flags = read_flag::NOFLAG) {
658 ctoon_read_err err; std::memset(&err, 0, sizeof(err));
659 ctoon_doc *d = ctoon_read_json_file(path, to_c(flags), NULL, &err);
660 if (!d) throw parse_error(err);
661 return document(d);
662 }
663#endif /* CTOON_ENABLE_JSON */
664
665 /* --- access --- */
666 bool valid() const CTOON_NOEXCEPT { return doc_ != NULL; }
667 operator bool() const CTOON_NOEXCEPT { return valid(); }
668 value root() const CTOON_NOEXCEPT { return value(ctoon_doc_get_root(doc_)); }
669 value operator*() const CTOON_NOEXCEPT { return root(); }
670 ctoon_doc *raw() const CTOON_NOEXCEPT { return doc_; }
671 std::size_t read_size() const CTOON_NOEXCEPT { return ctoon_doc_get_read_size(doc_); }
672 std::size_t val_count() const CTOON_NOEXCEPT { return ctoon_doc_get_val_count(doc_); }
673
674 value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
675 return value(ctoon_doc_ptr_getn(doc_, ptr.data(), ptr.size()));
676 }
677
678 /* --- TOON serialisation --- */
680 ctoon_write_options co = opts.to_c();
681 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
682 std::size_t len = 0;
683 char *raw = ctoon_write_opts(doc_, &co, NULL, &len, &err);
684 if (!raw) throw write_error(err);
685 return write_result(raw, len);
686 }
687 void write_file(const char *path,
688 const write_options &opts = write_options()) const {
689 ctoon_write_options co = opts.to_c();
690 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
691 if (!ctoon_write_file(path, doc_, &co, NULL, &err)) throw write_error(err);
692 }
693
694 /* --- JSON serialisation --- */
695#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
696 write_result to_json(int indent = 2,
697 write_flag flags = write_flag::NOFLAG) const {
698 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
699 std::size_t len = 0;
700 char *raw = ctoon_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
701 if (!raw) throw write_error(err);
702 return write_result(raw, len);
703 }
704 void to_json_file(const char *path, int indent = 2,
705 write_flag flags = write_flag::NOFLAG) const {
706 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
707 std::size_t len = 0;
708 char *raw = ctoon_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
709 if (!raw) throw write_error(err);
710 FILE *fp = std::fopen(path, "wb");
711 if (!fp) { std::free(raw); throw write_error(err); }
712 std::fwrite(raw, 1, len, fp);
713 std::fclose(fp);
714 std::free(raw);
715 }
716#endif /* CTOON_ENABLE_JSON */
717
718 /* --- convert to mutable --- */
719 mut_document mut_copy() const; /* defined after mut_document */
720
721 ctoon_doc *release() CTOON_NOEXCEPT { ctoon_doc *d = doc_; doc_ = NULL; return d; }
722
723 ctoon_doc *doc_;
724};
725
726/* -----------------------------------------------------------------------
727 * mut_document – RAII owner of ctoon_mut_doc*
728 * -------------------------------------------------------------------- */
729
731public:
732 mut_document() CTOON_NOEXCEPT : doc_(NULL) {}
733 explicit mut_document(ctoon_mut_doc *doc) CTOON_NOEXCEPT : doc_(doc) {}
734 ~mut_document() { ctoon_mut_doc_free(doc_); }
735
736 mut_document(mut_document &&o) CTOON_NOEXCEPT : doc_(o.doc_) { o.doc_ = NULL; }
737 mut_document &operator=(mut_document &&o) CTOON_NOEXCEPT {
738 if (this != &o) { ctoon_mut_doc_free(doc_); doc_ = o.doc_; o.doc_ = NULL; }
739 return *this;
740 }
741
743 ctoon_mut_doc *d = ctoon_mut_doc_new(NULL);
744 if (!d) throw std::bad_alloc();
745 return mut_document(d);
746 }
747
748 /* --- access --- */
749 bool valid() const CTOON_NOEXCEPT { return doc_ != NULL; }
750 operator bool() const CTOON_NOEXCEPT { return valid(); }
751 mut_value root() const CTOON_NOEXCEPT { return mut_value(ctoon_mut_doc_get_root(doc_)); }
752 mut_value operator*() const CTOON_NOEXCEPT { return root(); }
753 void set_root(mut_value v) CTOON_NOEXCEPT { ctoon_mut_doc_set_root(doc_, v.raw()); }
754 ctoon_mut_doc *raw() const CTOON_NOEXCEPT { return doc_; }
755
756 mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT {
757 return mut_value(ctoon_mut_doc_ptr_getn(doc_, ptr.data(), ptr.size()));
758 }
759
760 /* --- value factories --- */
761 mut_value make_null() CTOON_NOEXCEPT { return mut_value(ctoon_mut_null(doc_)); }
762 mut_value make_true() CTOON_NOEXCEPT { return mut_value(ctoon_mut_true(doc_)); }
763 mut_value make_false() CTOON_NOEXCEPT { return mut_value(ctoon_mut_false(doc_)); }
764 mut_value make_bool(bool v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_bool(doc_, v)); }
765 mut_value make_uint(std::uint64_t v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_uint(doc_, v)); }
766 mut_value make_sint(std::int64_t v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_sint(doc_, v)); }
767 mut_value make_real(double v) CTOON_NOEXCEPT { return mut_value(ctoon_mut_real(doc_, v)); }
768 mut_value make_arr() CTOON_NOEXCEPT { return mut_value(ctoon_mut_arr(doc_)); }
769 mut_value make_obj() CTOON_NOEXCEPT { return mut_value(ctoon_mut_obj(doc_)); }
770 mut_value make_str(const char *s) CTOON_NOEXCEPT { return mut_value(ctoon_mut_strcpy(doc_, s)); }
771 mut_value make_str(const std::string &s) CTOON_NOEXCEPT {
772 return mut_value(ctoon_mut_strncpy(doc_, s.data(), s.size()));
773 }
774 mut_value make_str(const string_view &s) CTOON_NOEXCEPT {
775 return mut_value(ctoon_mut_strncpy(doc_, s.data(), s.size()));
776 }
777
778 /* --- TOON serialisation --- */
780 ctoon_write_options co = opts.to_c();
781 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
782 std::size_t len = 0;
783 char *raw = ctoon_mut_write_opts(doc_, &co, NULL, &len, &err);
784 if (!raw) throw write_error(err);
785 return write_result(raw, len);
786 }
787 void write_file(const char *path,
788 const write_options &opts = write_options()) const {
789 ctoon_write_options co = opts.to_c();
790 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
791 if (!ctoon_mut_write_file(path, doc_, &co, NULL, &err)) throw write_error(err);
792 }
793
794 /* --- JSON serialisation --- */
795#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
796 write_result to_json(int indent = 2,
797 write_flag flags = write_flag::NOFLAG) const {
798 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
799 std::size_t len = 0;
800 char *raw = ctoon_mut_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
801 if (!raw) throw write_error(err);
802 return write_result(raw, len);
803 }
804 void to_json_file(const char *path, int indent = 2,
805 write_flag flags = write_flag::NOFLAG) const {
806 ctoon_write_err err; std::memset(&err, 0, sizeof(err));
807 std::size_t len = 0;
808 char *raw = ctoon_mut_doc_to_json(doc_, indent, to_c(flags), NULL, &len, &err);
809 if (!raw) throw write_error(err);
810 FILE *fp = std::fopen(path, "wb");
811 if (!fp) { std::free(raw); throw write_error(err); }
812 std::fwrite(raw, 1, len, fp);
813 std::fclose(fp);
814 std::free(raw);
815 }
816#endif /* CTOON_ENABLE_JSON */
817
818 ctoon_mut_doc *release() CTOON_NOEXCEPT { ctoon_mut_doc *d = doc_; doc_ = NULL; return d; }
819
820 ctoon_mut_doc *doc_;
821};
822
823/* --- deferred --- */
825 ctoon_mut_doc *d = ctoon_doc_mut_copy(doc_, NULL);
826 if (!d) throw std::bad_alloc();
827 return mut_document(d);
828}
829
830/* -----------------------------------------------------------------------
831 * Free-function API (thin wrappers, prefer member functions)
832 * -------------------------------------------------------------------- */
833
834inline document parse(const char *toon, read_flag flags = read_flag::NOFLAG) {
835 return document::parse(toon, flags);
836}
837inline document parse(const std::string &toon, read_flag flags = read_flag::NOFLAG) {
838 return document::parse(toon, flags);
839}
840inline document parse_file(const char *path, read_flag flags = read_flag::NOFLAG) {
841 return document::parse_file(path, flags);
842}
844inline write_result to_string(const document &d, const write_options &o = write_options()) { return d.to_string(o); }
845inline write_result to_string(const mut_document &d, const write_options &o = write_options()) { return d.to_string(o); }
846
847#if defined(CTOON_ENABLE_JSON) && CTOON_ENABLE_JSON
848inline document from_json(const char *json, std::size_t len, read_flag flags = read_flag::NOFLAG) {
849 return document::from_json(json, len, flags);
850}
851inline document from_json(const char *json, read_flag flags = read_flag::NOFLAG) {
852 return document::from_json(json, flags);
853}
854inline document from_json(const std::string &json, read_flag flags = read_flag::NOFLAG) {
855 return document::from_json(json, flags);
856}
857inline document from_json_file(const char *path, read_flag flags = read_flag::NOFLAG) {
858 return document::from_json_file(path, flags);
859}
860inline write_result to_json(const document &d, int indent = 2, write_flag flags = write_flag::NOFLAG) {
861 return d.to_json(indent, flags);
862}
863inline write_result to_json(const mut_document &d, int indent = 2, write_flag flags = write_flag::NOFLAG) {
864 return d.to_json(indent, flags);
865}
866
867/* -----------------------------------------------------------------------
868 * Direct text <-> text conversion
869 *
870 * One call into the C core. No ctoon::document is created and no value
871 * tree is exposed: JSON text goes in, TOON text comes out (and back).
872 * -------------------------------------------------------------------- */
873
875inline write_result json_to_toon(const char *json, std::size_t len,
876 const write_options &opts = write_options(),
878 ctoon_read_err rerr; std::memset(&rerr, 0, sizeof(rerr));
879 ctoon_write_err werr; std::memset(&werr, 0, sizeof(werr));
880 ctoon_write_options co = opts.to_c();
881 std::size_t n = 0;
882 char *raw = ctoon_json_to_toon_opts(json, len, to_c(flags), &co, NULL, &n, &rerr, &werr);
883 if (!raw) {
884 if (rerr.code) throw parse_error(rerr);
885 throw write_error(werr);
886 }
887 return write_result(raw, n);
888}
889inline write_result json_to_toon(const char *json,
890 const write_options &opts = write_options(),
892 return json_to_toon(json, std::strlen(json), opts, flags);
893}
894inline write_result json_to_toon(const std::string &json,
895 const write_options &opts = write_options(),
897 return json_to_toon(json.data(), json.size(), opts, flags);
898}
899inline write_result json_to_toon(const string_view &json,
900 const write_options &opts = write_options(),
902 return json_to_toon(json.data(), json.size(), opts, flags);
903}
904
906inline void json_to_toon_file(const char *json_path, const char *toon_path,
907 const write_options &opts = write_options(),
909 ctoon_read_err rerr; std::memset(&rerr, 0, sizeof(rerr));
910 ctoon_write_err werr; std::memset(&werr, 0, sizeof(werr));
911 ctoon_write_options co = opts.to_c();
912 if (!ctoon_json_to_toon_file_opts(json_path, toon_path, to_c(flags), &co, NULL, &rerr, &werr)) {
913 if (rerr.code) throw parse_error(rerr);
914 throw write_error(werr);
915 }
916}
917
919inline write_result toon_to_json(const char *toon, std::size_t len, int indent = 0,
921 read_flag rflags = read_flag::NOFLAG) {
922 ctoon_read_err rerr; std::memset(&rerr, 0, sizeof(rerr));
923 ctoon_write_err werr; std::memset(&werr, 0, sizeof(werr));
924 std::size_t n = 0;
925 char *raw = ctoon_toon_to_json_opts(toon, len, to_c(rflags), indent, to_c(wflags),
926 NULL, &n, &rerr, &werr);
927 if (!raw) {
928 if (rerr.code) throw parse_error(rerr);
929 throw write_error(werr);
930 }
931 return write_result(raw, n);
932}
933inline write_result toon_to_json(const char *toon, int indent = 0,
935 read_flag rflags = read_flag::NOFLAG) {
936 return toon_to_json(toon, std::strlen(toon), indent, wflags, rflags);
937}
938inline write_result toon_to_json(const std::string &toon, int indent = 0,
940 read_flag rflags = read_flag::NOFLAG) {
941 return toon_to_json(toon.data(), toon.size(), indent, wflags, rflags);
942}
943inline write_result toon_to_json(const string_view &toon, int indent = 0,
945 read_flag rflags = read_flag::NOFLAG) {
946 return toon_to_json(toon.data(), toon.size(), indent, wflags, rflags);
947}
948
950inline void toon_to_json_file(const char *toon_path, const char *json_path, int indent = 0,
952 read_flag rflags = read_flag::NOFLAG) {
953 ctoon_read_err rerr; std::memset(&rerr, 0, sizeof(rerr));
954 ctoon_write_err werr; std::memset(&werr, 0, sizeof(werr));
955 if (!ctoon_toon_to_json_file_opts(toon_path, json_path, to_c(rflags), indent,
956 to_c(wflags), NULL, &rerr, &werr)) {
957 if (rerr.code) throw parse_error(rerr);
958 throw write_error(werr);
959 }
960}
961#endif /* CTOON_ENABLE_JSON */
962
963} // 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:617
static document from_json(const std::string &json, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:651
document & operator=(document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:593
static document parse_indent(const string_view &toon, int indent_size, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:629
write_result to_json(int indent=2, write_flag flags=write_flag::NOFLAG) const
Definition ctoon.hpp:696
document(document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:592
ctoon_doc * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:670
void write_file(const char *path, const write_options &opts=write_options()) const
Definition ctoon.hpp:687
document(ctoon_doc *doc) CTOON_NOEXCEPT
Definition ctoon.hpp:589
static document from_json(const char *json, std::size_t len, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:641
static document from_json(const string_view &json, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:654
std::size_t val_count() const CTOON_NOEXCEPT
Definition ctoon.hpp:672
ctoon_doc * doc_
Definition ctoon.hpp:723
static document from_json(const char *json, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:648
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:666
static document parse(const string_view &toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:612
static document parse_indent(const std::string &toon, int indent_size, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:625
std::size_t read_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:671
value operator*() const CTOON_NOEXCEPT
Definition ctoon.hpp:669
value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:674
static document parse(const std::string &toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:609
ctoon_doc * release() CTOON_NOEXCEPT
Definition ctoon.hpp:721
static document parse(const char *toon, std::size_t len, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:599
static document from_json_file(const char *path, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:657
document() CTOON_NOEXCEPT
Definition ctoon.hpp:588
write_result to_string(const write_options &opts=write_options()) const
Definition ctoon.hpp:679
mut_document mut_copy() const
Definition ctoon.hpp:824
static document parse_file(const char *path, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:633
static document parse(const char *toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:606
value root() const CTOON_NOEXCEPT
Definition ctoon.hpp:668
void to_json_file(const char *path, int indent=2, write_flag flags=write_flag::NOFLAG) const
Definition ctoon.hpp:704
virtual const char * what() const CTOON_NOEXCEPT
Definition ctoon.hpp:143
std::string msg_
Definition ctoon.hpp:146
error(const std::string &msg) CTOON_NOEXCEPT
Definition ctoon.hpp:142
virtual ~error() CTOON_NOEXCEPT
Definition ctoon.hpp:144
mut_value make_uint(std::uint64_t v) CTOON_NOEXCEPT
Definition ctoon.hpp:765
mut_value make_false() CTOON_NOEXCEPT
Definition ctoon.hpp:763
mut_value make_arr() CTOON_NOEXCEPT
Definition ctoon.hpp:768
mut_value make_sint(std::int64_t v) CTOON_NOEXCEPT
Definition ctoon.hpp:766
void set_root(mut_value v) CTOON_NOEXCEPT
Definition ctoon.hpp:753
void write_file(const char *path, const write_options &opts=write_options()) const
Definition ctoon.hpp:787
mut_value make_bool(unsigned char v) CTOON_NOEXCEPT
Definition ctoon.hpp:764
mut_value make_str(const std::string &s) CTOON_NOEXCEPT
Definition ctoon.hpp:771
mut_document & operator=(mut_document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:737
mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:756
write_result to_json(int indent=2, write_flag flags=write_flag::NOFLAG) const
Definition ctoon.hpp:796
mut_value make_obj() CTOON_NOEXCEPT
Definition ctoon.hpp:769
write_result to_string(const write_options &opts=write_options()) const
Definition ctoon.hpp:779
mut_document() CTOON_NOEXCEPT
Definition ctoon.hpp:732
mut_document(ctoon_mut_doc *doc) CTOON_NOEXCEPT
Definition ctoon.hpp:733
ctoon_mut_doc * doc_
Definition ctoon.hpp:820
mut_value make_str(const string_view &s) CTOON_NOEXCEPT
Definition ctoon.hpp:774
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:749
mut_value root() const CTOON_NOEXCEPT
Definition ctoon.hpp:751
ctoon_mut_doc * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:754
mut_value make_real(double v) CTOON_NOEXCEPT
Definition ctoon.hpp:767
mut_value make_null() CTOON_NOEXCEPT
Definition ctoon.hpp:761
void to_json_file(const char *path, int indent=2, write_flag flags=write_flag::NOFLAG) const
Definition ctoon.hpp:804
static mut_document create()
Definition ctoon.hpp:742
mut_document(mut_document &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:736
mut_value make_str(const char *s) CTOON_NOEXCEPT
Definition ctoon.hpp:770
mut_value make_true() CTOON_NOEXCEPT
Definition ctoon.hpp:762
mut_value operator*() const CTOON_NOEXCEPT
Definition ctoon.hpp:752
ctoon_mut_doc * release() CTOON_NOEXCEPT
Definition ctoon.hpp:818
unsigned char equals(const mut_value &rhs) const CTOON_NOEXCEPT
Definition ctoon.hpp:497
mut_value obj_get(const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:490
mut_value arr_get(std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:482
ctoon_mut_val * v_
Definition ctoon.hpp:579
unsigned char obj_clear() const CTOON_NOEXCEPT
Definition ctoon.hpp:521
mut_value() CTOON_NOEXCEPT
Definition ctoon.hpp:446
unsigned char arr_append(mut_value item) const CTOON_NOEXCEPT
Definition ctoon.hpp:507
unsigned char is_obj() const CTOON_NOEXCEPT
Definition ctoon.hpp:465
std::int64_t get_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:470
mut_value arr_first() const CTOON_NOEXCEPT
Definition ctoon.hpp:483
unsigned char arr_prepend(mut_value item) const CTOON_NOEXCEPT
Definition ctoon.hpp:508
unsigned char is_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:459
unsigned char is_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:457
std::size_t arr_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:481
unsigned char get_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:468
unsigned char is_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:462
mut_value(ctoon_mut_val *v) CTOON_NOEXCEPT
Definition ctoon.hpp:447
mut_value operator[](const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:494
unsigned char is_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:461
unsigned char is_null() const CTOON_NOEXCEPT
Definition ctoon.hpp:454
double get_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:472
mut_value obj_get(const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:489
unsigned char arr_clear() const CTOON_NOEXCEPT
Definition ctoon.hpp:509
mut_value operator[](const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:493
std::size_t obj_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:488
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:450
unsigned char is_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:463
unsigned char equals(const string_view &s) const CTOON_NOEXCEPT
Definition ctoon.hpp:499
mut_value operator[](std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:485
unsigned char obj_put(mut_value key, mut_value val) const CTOON_NOEXCEPT
Definition ctoon.hpp:518
unsigned char is_int() const CTOON_NOEXCEPT
Definition ctoon.hpp:460
mut_value arr_last() const CTOON_NOEXCEPT
Definition ctoon.hpp:484
mut_value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:502
std::size_t get_len() const CTOON_NOEXCEPT
Definition ctoon.hpp:473
double get_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:471
unsigned char is_arr() const CTOON_NOEXCEPT
Definition ctoon.hpp:464
unsigned char equals(const char *s) const CTOON_NOEXCEPT
Definition ctoon.hpp:498
unsigned char is_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:458
std::uint64_t get_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:469
unsigned char obj_add(mut_value key, mut_value val) const CTOON_NOEXCEPT
Definition ctoon.hpp:515
unsigned char is_false() const CTOON_NOEXCEPT
Definition ctoon.hpp:456
unsigned char is_true() const CTOON_NOEXCEPT
Definition ctoon.hpp:455
ctoon_mut_val * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:449
mut_value obj_remove(const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:522
mut_value arr_remove(std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:510
string_view get_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:474
parse_error(const ctoon_read_err &e)
Definition ctoon.hpp:153
std::size_t pos
Definition ctoon.hpp:152
ctoon_read_code code
Definition ctoon.hpp:151
virtual ~parse_error() CTOON_NOEXCEPT
Definition ctoon.hpp:155
static string_view string() CTOON_NOEXCEPT
Definition ctoon.hpp:128
static unsigned int patch() CTOON_NOEXCEPT
Definition ctoon.hpp:110
static unsigned int major() CTOON_NOEXCEPT
Definition ctoon.hpp:108
static unsigned int minor() CTOON_NOEXCEPT
Definition ctoon.hpp:109
static string_view string() CTOON_NOEXCEPT
Definition ctoon.hpp:112
static unsigned int hex() CTOON_NOEXCEPT
Definition ctoon.hpp:111
static string_view string() CTOON_NOEXCEPT
Definition ctoon.hpp:120
value get_pointer(const string_view &ptr) const CTOON_NOEXCEPT
Definition ctoon.hpp:370
unsigned char equals(const string_view &s) const CTOON_NOEXCEPT
Definition ctoon.hpp:367
unsigned char is_int() const CTOON_NOEXCEPT
Definition ctoon.hpp:328
double get_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:340
std::size_t obj_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:356
std::int64_t get_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:338
unsigned char is_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:329
unsigned char is_num() const CTOON_NOEXCEPT
Definition ctoon.hpp:330
unsigned char equals(const char *s) const CTOON_NOEXCEPT
Definition ctoon.hpp:366
value operator[](std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:353
unsigned char equals(const value &rhs) const CTOON_NOEXCEPT
Definition ctoon.hpp:365
unsigned char is_sint() const CTOON_NOEXCEPT
Definition ctoon.hpp:327
value() CTOON_NOEXCEPT
Definition ctoon.hpp:314
unsigned char get_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:336
std::size_t arr_size() const CTOON_NOEXCEPT
Definition ctoon.hpp:349
value arr_first() const CTOON_NOEXCEPT
Definition ctoon.hpp:351
value operator[](const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:362
string_view get_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:342
value(ctoon_val *v) CTOON_NOEXCEPT
Definition ctoon.hpp:315
std::size_t get_len() const CTOON_NOEXCEPT
Definition ctoon.hpp:341
unsigned char is_true() const CTOON_NOEXCEPT
Definition ctoon.hpp:323
unsigned char is_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:331
unsigned char is_bool() const CTOON_NOEXCEPT
Definition ctoon.hpp:325
write_result to_string(const write_options &opts=write_options()) const
Definition ctoon.hpp:375
value obj_get(const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:357
unsigned char is_arr() const CTOON_NOEXCEPT
Definition ctoon.hpp:332
value arr_last() const CTOON_NOEXCEPT
Definition ctoon.hpp:352
value arr_get(std::size_t i) const CTOON_NOEXCEPT
Definition ctoon.hpp:350
ctoon_val * v_
Definition ctoon.hpp:437
std::uint64_t get_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:337
value operator[](const char *key) const CTOON_NOEXCEPT
Definition ctoon.hpp:361
unsigned char is_uint() const CTOON_NOEXCEPT
Definition ctoon.hpp:326
ctoon_val * raw() const CTOON_NOEXCEPT
Definition ctoon.hpp:317
value obj_get(const string_view &key) const CTOON_NOEXCEPT
Definition ctoon.hpp:358
double get_real() const CTOON_NOEXCEPT
Definition ctoon.hpp:339
unsigned char is_false() const CTOON_NOEXCEPT
Definition ctoon.hpp:324
unsigned char valid() const CTOON_NOEXCEPT
Definition ctoon.hpp:318
unsigned char is_obj() const CTOON_NOEXCEPT
Definition ctoon.hpp:333
unsigned char is_null() const CTOON_NOEXCEPT
Definition ctoon.hpp:322
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:169
write_error(const ctoon_write_err &e)
Definition ctoon.hpp:167
ctoon_write_code code
Definition ctoon.hpp:166
write_options & with_indent(int i) CTOON_NOEXCEPT
Definition ctoon.hpp:280
int indent() const CTOON_NOEXCEPT
Definition ctoon.hpp:284
ctoon_write_options to_c() const CTOON_NOEXCEPT
Definition ctoon.hpp:286
write_flag flag() const CTOON_NOEXCEPT
Definition ctoon.hpp:282
write_options & with_delimiter(delimiter d) CTOON_NOEXCEPT
Definition ctoon.hpp:279
write_options & with_flag(write_flag f) CTOON_NOEXCEPT
Definition ctoon.hpp:278
delimiter get_delimiter() const CTOON_NOEXCEPT
Definition ctoon.hpp:283
write_options() CTOON_NOEXCEPT
Definition ctoon.hpp:275
write_result(write_result &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:181
std::size_t len_
Definition ctoon.hpp:193
write_result(char *p, std::size_t n) CTOON_NOEXCEPT
Definition ctoon.hpp:179
std::string str() const
Definition ctoon.hpp:191
string_view view() const CTOON_NOEXCEPT
Definition ctoon.hpp:190
const char * c_str() const CTOON_NOEXCEPT
Definition ctoon.hpp:187
std::size_t size() const CTOON_NOEXCEPT
Definition ctoon.hpp:188
write_result & operator=(write_result &&o) CTOON_NOEXCEPT
Definition ctoon.hpp:183
write_result() CTOON_NOEXCEPT
Definition ctoon.hpp:178
unsigned char empty() const CTOON_NOEXCEPT
Definition ctoon.hpp:189
document from_json_file(const char *path, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:857
write_flag
Definition ctoon.hpp:213
delimiter
Definition ctoon.hpp:200
write_result to_json(const document &d, int indent=2, write_flag flags=write_flag::NOFLAG)
Definition ctoon.hpp:860
ctoon_write_flag to_c(write_flag f) CTOON_NOEXCEPT
Convert to the C type for passing to C API.
Definition ctoon.hpp:231
document from_json(const char *json, std::size_t len, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:848
write_result toon_to_json(const char *toon, std::size_t len, int indent=0, write_flag wflags=write_flag::NOFLAG, read_flag rflags=read_flag::NOFLAG)
TOON text -> JSON text (compact by default; pass indent > 0 to pretty-print).
Definition ctoon.hpp:919
write_result json_to_toon(const char *json, std::size_t len, const write_options &opts=write_options(), read_flag flags=read_flag::NOFLAG)
JSON text -> TOON text.
Definition ctoon.hpp:875
write_flag operator|(write_flag l, write_flag r) CTOON_NOEXCEPT
Definition ctoon.hpp:224
read_flag
Definition ctoon.hpp:239
mut_document make_document()
Definition ctoon.hpp:843
write_result to_string(const document &d, const write_options &o=write_options())
Definition ctoon.hpp:844
void json_to_toon_file(const char *json_path, const char *toon_path, const write_options &opts=write_options(), read_flag flags=read_flag::NOFLAG)
JSON file -> TOON file.
Definition ctoon.hpp:906
void toon_to_json_file(const char *toon_path, const char *json_path, int indent=0, write_flag wflags=write_flag::NOFLAG, read_flag rflags=read_flag::NOFLAG)
TOON file -> JSON file.
Definition ctoon.hpp:950
write_flag & operator|=(write_flag &l, write_flag r) CTOON_NOEXCEPT
Definition ctoon.hpp:227
document parse_file(const char *path, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:840
document parse(const char *toon, read_flag flags=read_flag::NOFLAG)
Definition ctoon.hpp:834