Lines Matching refs:value
61 struct value;
63 // parses 'data' and writes the corresponding JSON structure to 'value'
64 char* parse_value(char* data, value& value);
66 // parses 'data' and return JSON structure. the value::type will be 'uninitialized' if parsing failed.
67 value parse(char* data);
69 std::string to_string(const value& value);
75 using array = array_t<value>;
78 struct value {
88 value() : type{ type::uninitialized } {}
89 value(object&& value) : type{ type::object }, object_(std::move(value)) {}
90 value(array&& value) : type{ type::array }, array_(std::move(value)) {}
91 value(string value) : type{ type::string }, string_(value) {}
92 value(double value) : type{ type::number }, number_(value) {}
93 explicit value(bool value) : type{ type::boolean }, boolean_(value) {}
94 value(null value) : type{ type::null } {}
97 value(T value) : type{ type::number }, number_(static_cast<double>(value))
101 value(array_t<T> values) : type{ type::array }, array_(array{})
104 for (const auto& value : values) {
105 array_.push_back({ value });
110 value(T (&value)[N]) : type{ type::array }, array_(array{})
114 array_.push_back({ value[i] });
118 value(value&& rhs) : type{ std::exchange(rhs.type, type::uninitialized) }
145 value& operator=(value&& rhs)
177 ~value()
241 const value* find(string_t key) const noexcept;
243 value& operator[](const string_t& key);
248 value value;
251 inline const value* value::find(string_t key) const noexcept
256 return &t.value;
263 inline value& value::operator[](const string_t& key)
268 return t.value;
271 object_.push_back({ key, value{} });
272 return object_.back().value;
277 inline bool HasKey(const value& object, const string_t key)
289 inline const value& GetKey(const value& object, const string_t key)
291 static value dummy;
295 return t.value;
303 char* parse_value(char* data, value& value);
304 char* parse_number(char* data, value&);
305 char* parse_string(char* data, value&);
335 char* parse_object(char* data, value& res)
340 res = value(object{});
352 value key;
363 data = trim(parse_value(data, pair.value));
366 res = value(std::move(values));
379 char* parse_array(char* data, value& res)
384 res = value(array{});
389 value tmp;
395 res = value(std::move(values));
407 char* parse_string(char* data, value& res)
432 res = value(string_t{ start, (size_t)(data - start) });
443 char* parse_number(char* data, value& res)
522 res = value(strtod(beg, &end));
529 char* parse_boolean(char* data, value& res)
541 res = value(true);
552 res = value(false);
562 char* parse_null(char* data, value& res)
573 res = value(null{});
583 char* parse_value(char* data, value& value)
587 data = trim(parse_object(data + 1, value));
589 data = trim(parse_array(data + 1, value));
591 data = trim(parse_string(data + 1, value));
593 data = trim(parse_number(data, value));
595 data = trim(parse_boolean(data, value));
597 data = trim(parse_null(data, value));
606 void add(value& v, value&& value)
610 v = std::move(value);
613 v.object_.back().value = std::move(value);
616 v.array_.push_back(std::move(value));
627 value parse(char* data)
630 // push an uninitialized value which will get the final value during parsing
631 stack.push_back(value{});
645 add(stack.back(), value(object{}));
649 value key;
657 // push the object with key and missing value on the stack and hope to find a value next
658 stack.push_back(value(object{}));
659 stack.back().object_.push_back(pair{ key.string_, value{} });
671 // check are we missing a value ('{"":}', '{"":"",}' )
677 auto value = std::move(stack.back());
683 add(stack.back(), std::move(value));
694 add(stack.back(), value(array{}));
698 stack.push_back(value(array{}));
707 // check are we missing a value ('[1,]' '[1]]')
714 auto value = std::move(stack.back());
720 add(stack.back(), std::move(value));
723 // comma is allowed when the previous value was complete and we have an incomplete object or array on the
732 value key;
740 stack.back().object_.push_back(pair{ key.string_, value{} });
750 value value;
751 data = trim(parse_string(data + 1, value));
752 if (acceptValue && value.type == type::string) {
753 add(stack.back(), std::move(value));
760 value value;
761 data = trim(parse_number(data, value));
762 if (acceptValue && value.type == type::number) {
763 add(stack.back(), std::move(value));
770 value value;
771 data = trim(parse_boolean(data, value));
772 if (acceptValue && value.type == type::boolean) {
773 add(stack.back(), std::move(value));
780 value value;
781 data = trim(parse_null(data, value));
782 if (acceptValue && value.type == type::null) {
783 add(stack.back(), std::move(value));
794 // check if we are missing a value ('{"":' '[')
799 auto value = std::move(stack.front());
800 return value;
805 std::string to_string(const value& value)
808 switch (value.type) {
815 for (const auto& v : value.object_) {
823 out += to_string(v.value);
832 for (const auto& v : value.array_) {
844 out.append(value.string_.data(), value.string_.size());
849 if (value.number_ == static_cast<int64_t>(value.number_)) {
850 out += std::to_string(static_cast<int64_t>(value.number_));
854 if (auto result = std::to_chars(std::begin(asStr), std::end(asStr), value.number_);
859 out += std::to_string(value.number_).data();
865 if (value.boolean_) {