docs
Loading...
Searching...
No Matches
JSON.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
7#include <Ultralight/String.h>
8
9namespace ultralight {
10
11namespace detail {
12struct JSONData;
13}
14
15class JSONObject;
16class JSONArray;
17
18///
19/// JSON document or value for structured data.
20///
21/// You use JSON to work with structured data. You can parse a document from text or build one in
22/// code. Operations never throw exceptions, so missing keys or syntax errors won't crash your
23/// application.
24///
25/// Call JSON::Parse() to parse JSON text and check for syntax errors:
26///
27/// ```
28/// JSON prefs = JSON::Parse(json_text);
29/// if (!prefs)
30/// printf("line %u: %s\n", prefs.error_line(),
31/// prefs.error_message().utf8().data());
32/// ```
33///
34/// ## Reading Values
35///
36/// You can chain operator[] across keys or indices without checking each step. A missing key, an
37/// out-of-range index, or the wrong kind of value anywhere in the chain safely resolves to the
38/// fallback passed to Or().
39///
40/// Read nested values with fallback defaults:
41///
42/// ```
43/// void ApplyPrefs(const JSON& prefs) {
44/// double ui_scale = prefs["ui"]["scale"].Or(1.0); // 1.0 if missing
45/// String player_name = prefs["profile"]["name"].Or("anonymous");
46/// bool muted = prefs["audio"]["muted"].Or(false);
47/// }
48/// ```
49///
50/// The Or() method reads a value matching the fallback's type without converting between types
51/// (reading `"3"` with a fallback of `0` returns `0`). An integer fallback truncates a fractional
52/// number toward zero.
53///
54/// Always read values through a const JSON reference. Indexing a non-const document creates missing
55/// objects along the path automatically.
56///
57/// @note Testing a lookup in a conditional checks whether the key exists rather than its boolean
58/// value. A key holding `false` still evaluates to `true` in a conditional. Pass `false` to
59/// Or() to read a JSON boolean.
60///
61/// ## Iterating Objects and Arrays
62///
63/// Iterate over an object's members using a range-for loop over AsObject():
64///
65/// ```
66/// void ConnectAll(const JSON& prefs) {
67/// for (auto [server, address] : prefs["servers"].AsObject())
68/// Connect(server, address.Or(""));
69/// }
70/// ```
71///
72/// Call AsArray() to iterate over array elements (see JSONArray). See JSONObject for object
73/// iteration.
74///
75/// ## Building Documents
76///
77/// Start a new document by calling JSON::Object() or JSON::Array(). Indexing a non-const document
78/// with operator[] returns an assignable reference (see JSON::Ref). To build an array, assign
79/// JSON::Array() before indexing elements, or call Push() through AsArray() to append items.
80///
81/// Populate nested values and serialize the document to text:
82///
83/// ```
84/// JSON save = JSON::Object();
85/// save["player"]["name"] = "ada"; // creates "player" first
86/// save["player"]["level"] = 12;
87///
88/// save["unlocks"] = JSON::Array();
89/// save["unlocks"][0] = "tutorial_done";
90///
91/// JSON unlocks = save["unlocks"];
92/// unlocks.AsArray().Push("first_boss");
93///
94/// String saved_text = save.Stringify(2); // two spaces per level
95/// ```
96///
97/// ## Copying Documents
98///
99/// A copy shares the document, and a JSON assigned into a document shares its value as well. A
100/// change made through one copy shows up in every other copy and in any document the value was
101/// assigned into.
102///
103/// Copy a document to share it, or call Clone() to make an independent copy:
104///
105/// ```
106/// JSON shared = save; // the same document
107/// JSON copy = save.Clone(); // an independent copy
108/// ```
109///
110/// @warning Only one thread at a time may access a document. Because copies share the underlying
111/// data, accessing copies across threads requires your own synchronization.
112///
113/// @see JSON::Ref, JSONObject, JSONArray
114///
116 public:
117 ///
118 /// Create an empty, invalid JSON value.
119 ///
120 /// It has no error message.
121 ///
122 /// You can assign another JSON to it later.
123 ///
125
126 ///
127 /// Create a copy of another JSON.
128 ///
129 /// The copy shares the same document (see the class overview).
130 ///
131 /// @note Use Clone() for an independent copy.
132 ///
133 JSON(const JSON& other);
134
135 ///
136 /// Move constructor.
137 ///
138 /// The moved-from handle becomes empty and invalid.
139 ///
140 JSON(JSON&& other);
141
142 ///
143 /// Destructor.
144 ///
146
147 ///
148 /// Assignment operator.
149 ///
150 /// This JSON now shares `other`'s value.
151 ///
152 /// @note This doesn't write into the value this JSON referred to before-- use
153 /// `json[...] = value` instead.
154 ///
155 JSON& operator=(const JSON& other);
156
157 ///
158 /// Move assignment operator.
159 ///
160 JSON& operator=(JSON&& other);
161
162 ///
163 /// Parse JSON text.
164 ///
165 /// The top level accepts any JSON value (object, array, string, number, boolean, or null).
166 ///
167 /// @param text The JSON text to parse (UTF-8).
168 ///
169 /// @return Returns the parsed document. On failure, is_valid() returns false, and
170 /// error_message() and error_line() describe the first syntax error.
171 ///
172 /// @note Input nested deeper than 1000 levels or larger than 256 MB fails to parse.
173 ///
174 static JSON Parse(const String& text);
175
176 ///
177 /// Create a new empty object value.
178 ///
179 static JSON Object();
180
181 ///
182 /// Create a new empty array value.
183 ///
184 static JSON Array();
185
186 ///
187 /// Whether or not this JSON holds a value.
188 ///
189 /// This returns false for a failed parse, a default-constructed JSON, a moved-from JSON, or a
190 /// lookup that found nothing.
191 ///
192 bool is_valid() const;
193
194 ///
195 /// Whether or not this JSON holds a value (the same as is_valid()).
196 ///
197 /// @note This tests whether a value is present, not the JSON boolean value (see the class
198 /// overview).
199 ///
200 explicit operator bool() const { return is_valid(); }
201
202 ///
203 /// Get the parse error description ("" unless this is a failed Parse() result).
204 ///
206
207 ///
208 /// Get the 1-based line number of the parse error (0 unless this is a failed Parse()
209 /// result).
210 ///
211 unsigned error_line() const;
212
213 ///
214 /// Whether or not this value is an object.
215 ///
216 bool is_object() const;
217
218 ///
219 /// Whether or not this value is an array.
220 ///
221 bool is_array() const;
222
223 ///
224 /// Whether or not this value is a string.
225 ///
226 bool is_string() const;
227
228 ///
229 /// Whether or not this value is a number.
230 ///
231 bool is_number() const;
232
233 ///
234 /// Whether or not this value is a boolean.
235 ///
236 bool is_bool() const;
237
238 ///
239 /// Whether or not this value is null.
240 ///
241 bool is_null() const;
242
243 ///
244 /// Read an object member.
245 ///
246 /// You can index the result again without checking it first (see the class overview).
247 ///
248 /// @param key The member name.
249 ///
250 /// @return Returns a JSON sharing the member's value. A missing key, a non-object value, or an
251 /// invalid JSON returns an invalid JSON.
252 ///
253 JSON operator[](const String& key) const;
254
255 ///
256 /// Read an array element.
257 ///
258 /// @param index The element index.
259 ///
260 /// @return Returns a JSON sharing the element's value. An out-of-range index, a non-array value,
261 /// or an invalid JSON returns an invalid JSON.
262 ///
263 JSON operator[](size_t index) const;
264
265 class Ref;
266
267 ///
268 /// Get an assignable reference to an object member.
269 ///
270 /// Assigning through the reference writes the member.
271 ///
272 /// Chaining `[]` creates missing members as objects along the path.
273 ///
274 /// @param key The member name.
275 ///
276 /// @return Returns a reference to the member (see JSON::Ref).
277 ///
278 /// @note On a non-object value, writes through the reference do nothing.
279 ///
280 Ref operator[](const String& key);
281
282 ///
283 /// Get an assignable reference to an array element.
284 ///
285 /// Writing past the end pads the array with nulls, up to 65536 elements past the end.
286 ///
287 /// Writes further out are ignored.
288 ///
289 /// @param index The element index.
290 ///
291 /// @return Returns a reference to the element (see JSON::Ref).
292 ///
293 /// @note On a non-array value, writes through the reference do nothing.
294 ///
295 Ref operator[](size_t index);
296
297 ///
298 /// Read this value as a number.
299 ///
300 /// @param fallback The value to return when this value is not a number.
301 ///
302 /// @return Returns the number or `fallback`.
303 ///
304 double Or(double fallback) const;
305
306 ///
307 /// Read this value as a number, truncated toward zero to an integer.
308 ///
309 /// @param fallback The value to return when this value is not a number.
310 ///
311 /// @return Returns the truncated number or `fallback`. Numbers beyond the 64-bit range
312 /// clamp to the nearest representable integer. NaN reads as 0.
313 ///
314 int64_t Or(long long fallback) const;
315
316 ///
317 /// Same as Or(long long).
318 ///
319 int64_t Or(int fallback) const;
320
321 ///
322 /// Same as Or(long long).
323 ///
324 int64_t Or(unsigned fallback) const;
325
326 ///
327 /// Same as Or(long long).
328 ///
329 int64_t Or(long fallback) const;
330
331 ///
332 /// Same as Or(long long).
333 ///
334 int64_t Or(unsigned long fallback) const;
335
336 ///
337 /// Same as Or(long long).
338 ///
339 int64_t Or(unsigned long long fallback) const;
340
341 ///
342 /// Same as Or(double).
343 ///
344 double Or(float fallback) const;
345
346 ///
347 /// Read this value as a boolean.
348 ///
349 /// @param fallback The value to return when this value is not a boolean.
350 ///
351 /// @return Returns the boolean or `fallback`.
352 ///
353 bool Or(bool fallback) const;
354
355 ///
356 /// Read this value as a string.
357 ///
358 /// @param fallback The value to return when this value is not a string.
359 ///
360 /// @return Returns the string or `fallback`.
361 ///
362 String Or(const String& fallback) const;
363
364 ///
365 /// Same as Or(const String&).
366 ///
367 String Or(const char* fallback) const;
368
369 ///
370 /// An object member yielded by JSONObject iteration.
371 ///
372 /// Supports structured bindings (`for (auto [key, value] : json.AsObject())`).
373 ///
374 struct Member;
375
376 ///
377 /// Get a typed view of this value as an object (for iteration, Has(), and size()).
378 ///
379 /// @return Returns the view. A non-object value or an invalid JSON returns an empty view.
380 ///
382
383 ///
384 /// Get a typed view of this value as an array (for iteration, size(), and Push()).
385 ///
386 /// @return Returns the view. A non-array value or an invalid JSON returns an empty view.
387 ///
388 /// @note A const JSON doesn't protect an array from appends-- Push() still works through the
389 /// view.
390 ///
392
393 ///
394 /// Serialize this value to JSON text.
395 ///
396 /// @param indent The number of spaces per nesting level. Pass 0 for compact single-line
397 /// output.
398 ///
399 /// @return Returns the JSON text, or "" when this value is invalid.
400 ///
401 String Stringify(unsigned indent = 0) const;
402
403 ///
404 /// Deep-copy this value into an independent document.
405 ///
406 /// Plain copies share the document (see the class overview).
407 ///
408 /// @return Returns the copy, or an invalid JSON if this JSON is invalid.
409 ///
410 JSON Clone() const;
411
412 private:
413 friend class JSONObject;
414 friend class JSONArray;
415 explicit JSON(detail::JSONData* adopted_node);
416
417 detail::JSONData* node_ = nullptr;
418};
419
420///
421/// One object member, as visited by JSONObject iteration.
422///
424 String key; ///< The member name.
425 JSON value; ///< The member's value (shared with the document).
426};
427
428///
429/// Assignable reference to a value inside a mutable JSON document.
430///
431/// Indexing a non-const JSON document with JSON::operator[]() produces a JSON::Ref. It acts as an
432/// assignable reference to a member or element, letting you modify data or chain deeper lookups.
433///
434/// Assigning through a JSON::Ref writes directly into the underlying document. When indexing
435/// through keys that don't exist yet, the reference creates intermediate objects along the path
436/// automatically.
437///
438/// You can store a reference to a nested object to update several values in the same branch:
439///
440/// ```
441/// JSON save = JSON::Object();
442///
443/// auto audio = save["audio"]; // a JSON::Ref into save
444/// audio["volume"] = 0.8; // writes save["audio"]["volume"]
445/// audio["muted"] = false;
446///
447/// JSON audio_values = save["audio"]; // a JSON, for AsObject() and AsArray()
448/// size_t count = audio_values.AsObject().size();
449/// ```
450///
451/// ## Storing a Reference
452///
453/// The variable type you choose when holding a lookup determines how assignment behaves and which
454/// methods are available:
455///
456/// - **Assigning to a stored JSON::Ref doesn't rebind the variable.** It still writes into the
457/// document.
458/// - **Assigning to a JSON variable rebinds the variable.** Reassigning a JSON instance leaves the
459/// document untouched, and the type provides AsObject() and AsArray() to inspect collections.
460///
461/// @see JSON, JSON::operator[]()
462///
464 public:
465 ///
466 /// Copy constructor (copies the reference itself, not the referenced value).
467 ///
468 UExport Ref(const Ref& other);
469
470 ///
471 /// Destructor.
472 ///
474
475 ///
476 /// Chain to an object member, creating the referenced place as an object if it is missing or
477 /// holds null.
478 ///
479 /// @param key The member name.
480 ///
481 /// @return Returns a reference to the member.
482 ///
483 /// @note If the referenced place holds another kind of value, writes through the result do
484 /// nothing.
485 ///
487
488 ///
489 /// Chain to an array element.
490 ///
491 /// @param index The element index.
492 ///
493 /// @return Returns a reference to the element.
494 ///
495 /// @note The referenced place must already hold an array. Otherwise writes through the result
496 /// do nothing.
497 ///
498 UExport Ref operator[](size_t index);
499
500 ///
501 /// Write `value` into the referenced place, sharing its document.
502 ///
503 /// @note An invalid `value` is ignored.
504 ///
505 UExport Ref& operator=(const JSON& value);
506
507 ///
508 /// Write the value `other` refers to into the referenced place.
509 ///
510 /// @note This assigns the value. This Ref continues to refer to its existing place.
511 ///
512 UExport Ref& operator=(const Ref& other);
513
514 ///
515 /// Write a boolean into the referenced place.
516 ///
517 UExport Ref& operator=(bool value);
518
519 ///
520 /// Write a number into the referenced place.
521 ///
522 UExport Ref& operator=(double value);
523
524 ///
525 /// Write a number into the referenced place.
526 ///
527 UExport Ref& operator=(int value);
528
529 ///
530 /// Write a number into the referenced place.
531 ///
532 UExport Ref& operator=(unsigned value);
533
534 ///
535 /// Write a number into the referenced place.
536 ///
537 UExport Ref& operator=(long value);
538
539 ///
540 /// Write a number into the referenced place.
541 ///
542 UExport Ref& operator=(unsigned long value);
543
544 ///
545 /// Write a number into the referenced place.
546 ///
547 UExport Ref& operator=(long long value);
548
549 ///
550 /// Write a number into the referenced place.
551 ///
552 UExport Ref& operator=(unsigned long long value);
553
554 ///
555 /// Write a string into the referenced place.
556 ///
557 UExport Ref& operator=(const String& value);
558
559 ///
560 /// Write a string into the referenced place.
561 ///
562 UExport Ref& operator=(const char* value);
563
564 ///
565 /// Write a JSON null into the referenced place.
566 ///
567 UExport Ref& operator=(decltype(nullptr));
568
569 ///
570 /// Convert to a JSON sharing the referenced value.
571 ///
572 /// @return Returns the value, or an invalid JSON if the referenced place is missing.
573 ///
574 UExport operator JSON() const;
575
576 ///
577 /// Whether or not the referenced place holds a value.
578 ///
579 UExport bool is_valid() const;
580
581 ///
582 /// Whether or not the referenced place holds a value (the same as is_valid()).
583 ///
584 explicit operator bool() const { return is_valid(); }
585
586 ///
587 /// Whether or not the referenced place holds a JSON null.
588 ///
589 /// This pairs with assigning `nullptr`.
590 ///
591 /// @note This returns false when the referenced place is missing.
592 ///
593 UExport bool is_null() const;
594
595 ///
596 /// Read the referenced value as a number (the same as JSON::Or()).
597 ///
598 /// @param fallback The value to return when the referenced value is not a number.
599 ///
600 /// @return Returns the number or `fallback`.
601 ///
602 UExport double Or(double fallback) const;
603
604 ///
605 /// Read the referenced value as a number, truncated toward zero to an integer.
606 ///
607 /// @param fallback The value to return when the referenced value is not a number.
608 ///
609 /// @return Returns the truncated number or `fallback`. Numbers beyond the 64-bit range
610 /// clamp to the nearest representable integer. NaN reads as 0.
611 ///
612 UExport int64_t Or(long long fallback) const;
613
614 ///
615 /// Same as Or(long long).
616 ///
617 UExport int64_t Or(int fallback) const;
618
619 ///
620 /// Same as Or(long long).
621 ///
622 UExport int64_t Or(unsigned fallback) const;
623
624 ///
625 /// Same as Or(long long).
626 ///
627 UExport int64_t Or(long fallback) const;
628
629 ///
630 /// Same as Or(long long).
631 ///
632 UExport int64_t Or(unsigned long fallback) const;
633
634 ///
635 /// Same as Or(long long).
636 ///
637 UExport int64_t Or(unsigned long long fallback) const;
638
639 ///
640 /// Same as Or(double).
641 ///
642 UExport double Or(float fallback) const;
643
644 ///
645 /// Read the referenced value as a boolean.
646 ///
647 /// @param fallback The value to return when the referenced value is not a boolean.
648 ///
649 /// @return Returns the boolean or `fallback`.
650 ///
651 UExport bool Or(bool fallback) const;
652
653 ///
654 /// Read the referenced value as a string.
655 ///
656 /// @param fallback The value to return when the referenced value is not a string.
657 ///
658 /// @return Returns the string or `fallback`.
659 ///
660 UExport String Or(const String& fallback) const;
661
662 ///
663 /// Same as Or(const String&).
664 ///
665 UExport String Or(const char* fallback) const;
666
667 private:
668 friend class JSON;
669 Ref(detail::JSONData* parent, const String& key);
670 Ref(detail::JSONData* parent, size_t index);
671
672 detail::JSONData* parent_;
673 String key_;
674 size_t index_;
675 bool keyed_;
676};
677
678///
679/// Typed view of a JSON object for checking keys and stepping through its members.
680///
681/// You obtain a JSONObject by calling JSON::AsObject() on a JSON value.
682///
683/// This function checks for a key and iterates over every member in an object:
684///
685/// ```
686/// void ConnectServers(const JSON& prefs) {
687/// JSONObject servers = prefs["servers"].AsObject();
688/// if (!servers.Has("default"))
689/// Log("no default server");
690///
691/// for (JSON::Member member : servers)
692/// Connect(member.key, member.value.Or(""));
693/// }
694/// ```
695///
696/// ## Member Iteration
697///
698/// Iteration visits members in the order they were parsed or first added, and assigning to an
699/// existing key keeps its position.
700///
701/// Calling JSON::AsObject() on a missing key or a value that isn't an object produces an empty
702/// view, so the loop doesn't run.
703///
704/// @see JSON::AsObject(), JSON::Member, JSONArray
705///
707 public:
708 ///
709 /// Input iterator over an object's members (yields JSON::Member by value).
710 ///
711 class iterator {
712 public:
713 JSON::Member operator*() const { return object_->MemberAt(index_); }
714 iterator& operator++() {
715 index_++;
716 return *this;
717 }
718 bool operator==(const iterator& other) const { return index_ == other.index_; }
719 bool operator!=(const iterator& other) const { return index_ != other.index_; }
720
721 private:
722 friend class JSONObject;
723 iterator(const JSONObject* object, size_t index) : object_(object), index_(index) {}
724 const JSONObject* object_;
725 size_t index_;
726 };
727
728 ///
729 /// Copy constructor.
730 ///
731 JSONObject(const JSONObject& other);
732
733 ///
734 /// Destructor.
735 ///
737
738 ///
739 /// Get the number of members (0 for an empty view).
740 ///
741 size_t size() const;
742
743 ///
744 /// Whether or not the object has a member with the given key.
745 ///
746 bool Has(const String& key) const;
747
748 ///
749 /// Get an iterator to the first member (insertion order).
750 ///
751 iterator begin() const { return iterator(this, 0); }
752
753 ///
754 /// Get an iterator past the last member.
755 ///
756 iterator end() const { return iterator(this, size()); }
757
758 private:
759 friend class JSON;
760 explicit JSONObject(detail::JSONData* node);
761 JSONObject& operator=(const JSONObject&) = delete;
762 JSON::Member MemberAt(size_t index) const;
763
764 detail::JSONData* node_;
765};
766
767///
768/// A view of a JSON array.
769///
770/// Calling JSON::AsArray() on a JSON value returns a JSONArray view. You use it to step through
771/// array elements in a range-for loop and append new values.
772///
773/// This example appends values to an array and logs each element:
774///
775/// ```
776/// JSON scores = JSON::Array();
777/// JSONArray entries = scores.AsArray();
778/// entries.Push(1200);
779/// entries.Push(950);
780///
781/// for (JSON score : entries)
782/// Log(score.Stringify().utf8().data());
783/// ```
784///
785/// ## Elements and Appending
786///
787/// Iterating over the array yields each element as a JSON that shares the underlying document.
788///
789/// Calling JSON::AsArray() on a value that isn't an array returns an empty view. The loop doesn't
790/// run, and Push() does nothing.
791///
792/// @see JSON::AsArray(), JSONObject
793///
795 public:
796 ///
797 /// Input iterator over an array's elements (yields JSON by value).
798 ///
799 class iterator {
800 public:
801 JSON operator*() const { return array_->ElementAt(index_); }
802 iterator& operator++() {
803 index_++;
804 return *this;
805 }
806 bool operator==(const iterator& other) const { return index_ == other.index_; }
807 bool operator!=(const iterator& other) const { return index_ != other.index_; }
808
809 private:
810 friend class JSONArray;
811 iterator(const JSONArray* array, size_t index) : array_(array), index_(index) {}
812 const JSONArray* array_;
813 size_t index_;
814 };
815
816 ///
817 /// Copy constructor.
818 ///
819 JSONArray(const JSONArray& other);
820
821 ///
822 /// Destructor.
823 ///
825
826 ///
827 /// Get the number of elements (0 for an empty view).
828 ///
829 size_t size() const;
830
831 ///
832 /// Append `value`, sharing its document.
833 ///
834 /// @note An invalid `value` appends nothing.
835 ///
836 void Push(const JSON& value);
837
838 ///
839 /// Append a boolean.
840 ///
841 void Push(bool value);
842
843 ///
844 /// Append a number.
845 ///
846 void Push(double value);
847
848 ///
849 /// Append a number.
850 ///
851 void Push(int value);
852
853 ///
854 /// Append a number.
855 ///
856 void Push(unsigned value);
857
858 ///
859 /// Append a number.
860 ///
861 void Push(long value);
862
863 ///
864 /// Append a number.
865 ///
866 void Push(unsigned long value);
867
868 ///
869 /// Append a number.
870 ///
871 void Push(long long value);
872
873 ///
874 /// Append a number.
875 ///
876 void Push(unsigned long long value);
877
878 ///
879 /// Append a string.
880 ///
881 void Push(const String& value);
882
883 ///
884 /// Append a string.
885 ///
886 void Push(const char* value);
887
888 ///
889 /// Append a JSON null.
890 ///
891 void Push(decltype(nullptr));
892
893 ///
894 /// Get an iterator to the first element.
895 ///
896 iterator begin() const { return iterator(this, 0); }
897
898 ///
899 /// Get an iterator past the last element.
900 ///
901 iterator end() const { return iterator(this, size()); }
902
903 private:
904 friend class JSON;
905 explicit JSONArray(detail::JSONData* node);
906 JSONArray& operator=(const JSONArray&) = delete;
907 JSON ElementAt(size_t index) const;
908
909 detail::JSONData* node_;
910};
911
912} // namespace ultralight
#define UExport
Definition Exports.h:22
Assignable reference to a value inside a mutable JSON document.
Definition JSON.h:463
int64_t Or(unsigned long long fallback) const
Same as Or(long long).
Ref & operator=(const Ref &other)
Write the value other refers to into the referenced place.
double Or(float fallback) const
Same as Or(double).
Ref & operator=(unsigned long long value)
Write a number into the referenced place.
int64_t Or(long long fallback) const
Read the referenced value as a number, truncated toward zero to an integer.
Ref & operator=(long long value)
Write a number into the referenced place.
String Or(const char *fallback) const
Same as Or(const String&).
Ref(const Ref &other)
Copy constructor (copies the reference itself, not the referenced value).
Ref & operator=(const String &value)
Write a string into the referenced place.
Ref operator[](const String &key)
Chain to an object member, creating the referenced place as an object if it is missing or holds null.
String Or(const String &fallback) const
Read the referenced value as a string.
Ref & operator=(const char *value)
Write a string into the referenced place.
bool is_null() const
Whether or not the referenced place holds a JSON null.
Ref operator[](size_t index)
Chain to an array element.
Ref & operator=(const JSON &value)
Write value into the referenced place, sharing its document.
friend class JSON
Definition JSON.h:668
int64_t Or(unsigned fallback) const
Same as Or(long long).
int64_t Or(int fallback) const
Same as Or(long long).
bool is_valid() const
Whether or not the referenced place holds a value.
double Or(double fallback) const
Read the referenced value as a number (the same as JSON::Or()).
bool Or(bool fallback) const
Read the referenced value as a boolean.
Ref & operator=(int value)
Write a number into the referenced place.
Ref & operator=(bool value)
Write a boolean into the referenced place.
Ref & operator=(long value)
Write a number into the referenced place.
Ref & operator=(unsigned value)
Write a number into the referenced place.
Ref & operator=(double value)
Write a number into the referenced place.
Ref & operator=(decltype(nullptr))
Write a JSON null into the referenced place.
int64_t Or(long fallback) const
Same as Or(long long).
Ref & operator=(unsigned long value)
Write a number into the referenced place.
int64_t Or(unsigned long fallback) const
Same as Or(long long).
Input iterator over an array's elements (yields JSON by value).
Definition JSON.h:799
bool operator!=(const iterator &other) const
Definition JSON.h:807
bool operator==(const iterator &other) const
Definition JSON.h:806
friend class JSONArray
Definition JSON.h:810
iterator & operator++()
Definition JSON.h:802
JSON operator*() const
Definition JSON.h:801
A view of a JSON array.
Definition JSON.h:794
void Push(long long value)
Append a number.
void Push(unsigned value)
Append a number.
iterator begin() const
Get an iterator to the first element.
Definition JSON.h:896
size_t size() const
Get the number of elements (0 for an empty view).
~JSONArray()
Destructor.
friend class JSON
Definition JSON.h:904
iterator end() const
Get an iterator past the last element.
Definition JSON.h:901
void Push(unsigned long long value)
Append a number.
void Push(double value)
Append a number.
void Push(const char *value)
Append a string.
void Push(int value)
Append a number.
void Push(unsigned long value)
Append a number.
void Push(const String &value)
Append a string.
JSONArray(const JSONArray &other)
Copy constructor.
void Push(long value)
Append a number.
void Push(bool value)
Append a boolean.
void Push(decltype(nullptr))
Append a JSON null.
void Push(const JSON &value)
Append value, sharing its document.
int64_t Or(unsigned long long fallback) const
Same as Or(long long).
static JSON Parse(const String &text)
Parse JSON text.
double Or(float fallback) const
Same as Or(double).
int64_t Or(long long fallback) const
Read this value as a number, truncated toward zero to an integer.
JSON & operator=(JSON &&other)
Move assignment operator.
String Or(const char *fallback) const
Same as Or(const String&).
JSON operator[](size_t index) const
Read an array element.
JSON(const JSON &other)
Create a copy of another JSON.
friend class JSONObject
Definition JSON.h:413
unsigned error_line() const
Get the 1-based line number of the parse error (0 unless this is a failed Parse() result).
JSON()
Create an empty, invalid JSON value.
String Stringify(unsigned indent=0) const
Serialize this value to JSON text.
bool is_string() const
Whether or not this value is a string.
String error_message() const
Get the parse error description ("" unless this is a failed Parse() result).
Ref operator[](const String &key)
Get an assignable reference to an object member.
String Or(const String &fallback) const
Read this value as a string.
bool is_null() const
Whether or not this value is null.
Ref operator[](size_t index)
Get an assignable reference to an array element.
JSON & operator=(const JSON &other)
Assignment operator.
int64_t Or(unsigned fallback) const
Same as Or(long long).
bool is_bool() const
Whether or not this value is a boolean.
int64_t Or(int fallback) const
Same as Or(long long).
JSONArray AsArray() const
Get a typed view of this value as an array (for iteration, size(), and Push()).
JSON(JSON &&other)
Move constructor.
bool is_number() const
Whether or not this value is a number.
bool is_valid() const
Whether or not this JSON holds a value.
JSON operator[](const String &key) const
Read an object member.
friend class JSONArray
Definition JSON.h:414
double Or(double fallback) const
Read this value as a number.
bool Or(bool fallback) const
Read this value as a boolean.
JSONObject AsObject() const
Get a typed view of this value as an object (for iteration, Has(), and size()).
~JSON()
Destructor.
JSON Clone() const
Deep-copy this value into an independent document.
bool is_array() const
Whether or not this value is an array.
int64_t Or(long fallback) const
Same as Or(long long).
static JSON Object()
Create a new empty object value.
bool is_object() const
Whether or not this value is an object.
static JSON Array()
Create a new empty array value.
int64_t Or(unsigned long fallback) const
Same as Or(long long).
Input iterator over an object's members (yields JSON::Member by value).
Definition JSON.h:711
bool operator!=(const iterator &other) const
Definition JSON.h:719
friend class JSONObject
Definition JSON.h:722
bool operator==(const iterator &other) const
Definition JSON.h:718
JSON::Member operator*() const
Definition JSON.h:713
iterator & operator++()
Definition JSON.h:714
Typed view of a JSON object for checking keys and stepping through its members.
Definition JSON.h:706
iterator begin() const
Get an iterator to the first member (insertion order).
Definition JSON.h:751
~JSONObject()
Destructor.
size_t size() const
Get the number of members (0 for an empty view).
friend class JSON
Definition JSON.h:759
iterator end() const
Get an iterator past the last member.
Definition JSON.h:756
JSONObject(const JSONObject &other)
Copy constructor.
bool Has(const String &key) const
Whether or not the object has a member with the given key.
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
Root namespace for every public Ultralight type, function, and enumeration.
One object member, as visited by JSONObject iteration.
Definition JSON.h:423
JSON value
The member's value (shared with the document).
Definition JSON.h:425
String key
The member name.
Definition JSON.h:424