#include "Json.h" #include namespace updater { struct Json::Impl { bool boolVal = false; double numVal = 0.0; std::string strVal; std::vector arrVal; std::map objVal; }; struct JsonAccess { static Json::Impl& impl(Json& j) { return *j.impl_; } static void setType(Json& j, Json::Type t) { j.type_ = t; } }; Json::Json() : impl_(std::make_shared()), type_(Null) {} Json::Json(Type type) : impl_(std::make_shared()), type_(type) {} bool Json::boolValue(bool def) const { if (type_ == Bool) return impl_->boolVal; if (type_ == Number) return impl_->numVal != 0.0; return def; } double Json::numberValue(double def) const { if (type_ == Number) return impl_->numVal; return def; } long long Json::intValue(long long def) const { if (type_ == Number) return static_cast(impl_->numVal); return def; } std::string Json::stringValue(const std::string& def) const { if (type_ == String) return impl_->strVal; return def; } const Json& Json::operator[](const char* key) const { static const Json nullValue; if (type_ != Object) return nullValue; auto it = impl_->objVal.find(key); if (it == impl_->objVal.end()) return nullValue; return it->second; } const Json& Json::operator[](const std::string& key) const { return (*this)[key.c_str()]; } const Json& Json::operator[](size_t index) const { static const Json nullValue; if (type_ != Array || index >= impl_->arrVal.size()) return nullValue; return impl_->arrVal[index]; } bool Json::contains(const char* key) const { return type_ == Object && impl_->objVal.find(key) != impl_->objVal.end(); } size_t Json::arraySize() const { return type_ == Array ? impl_->arrVal.size() : 0; } namespace { void AppendUtf8(std::string& out, unsigned int cp) { if (cp < 0x80) { out += static_cast(cp); } else if (cp < 0x800) { out += static_cast(0xC0 | (cp >> 6)); out += static_cast(0x80 | (cp & 0x3F)); } else if (cp < 0x10000) { out += static_cast(0xE0 | (cp >> 12)); out += static_cast(0x80 | ((cp >> 6) & 0x3F)); out += static_cast(0x80 | (cp & 0x3F)); } else { out += static_cast(0xF0 | (cp >> 18)); out += static_cast(0x80 | ((cp >> 12) & 0x3F)); out += static_cast(0x80 | ((cp >> 6) & 0x3F)); out += static_cast(0x80 | (cp & 0x3F)); } } bool Hex4(const std::string& s, size_t pos, unsigned int& out) { if (pos + 4 > s.size()) return false; unsigned int v = 0; for (size_t i = 0; i < 4; ++i) { char c = s[pos + i]; v <<= 4; if (c >= '0' && c <= '9') v |= static_cast(c - '0'); else if (c >= 'a' && c <= 'f') v |= static_cast(c - 'a' + 10); else if (c >= 'A' && c <= 'F') v |= static_cast(c - 'A' + 10); else return false; } out = v; return true; } class Parser { public: explicit Parser(const std::string& text) : s_(text) {} bool ParseValue(Json& out, std::string& err) { SkipSpace(); if (pos_ >= s_.size()) { err = "unexpected end"; return false; } char c = s_[pos_]; if (c == '{') return ParseObject(out, err); if (c == '[') return ParseArray(out, err); if (c == '"') { std::string str; if (!ParseString(str, err)) return false; JsonAccess::setType(out, Json::String); JsonAccess::impl(out).strVal = str; return true; } if (s_.compare(pos_, 4, "true") == 0) { pos_ += 4; JsonAccess::setType(out, Json::Bool); JsonAccess::impl(out).boolVal = true; return true; } if (s_.compare(pos_, 5, "false") == 0) { pos_ += 5; JsonAccess::setType(out, Json::Bool); JsonAccess::impl(out).boolVal = false; return true; } if (s_.compare(pos_, 4, "null") == 0) { pos_ += 4; JsonAccess::setType(out, Json::Null); return true; } return ParseNumber(out, err); } private: void SkipSpace() { while (pos_ < s_.size()) { char c = s_[pos_]; if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++pos_; else break; } } bool ParseString(std::string& out, std::string& err) { if (pos_ >= s_.size() || s_[pos_] != '"') { err = "expected string"; return false; } ++pos_; out.clear(); while (pos_ < s_.size()) { char c = s_[pos_]; if (c == '"') { ++pos_; return true; } if (c == '\\') { ++pos_; if (pos_ >= s_.size()) break; char e = s_[pos_++]; switch (e) { case '"': out += '"'; break; case '\\': out += '\\'; break; case '/': out += '/'; break; case 'b': out += '\b'; break; case 'f': out += '\f'; break; case 'n': out += '\n'; break; case 'r': out += '\r'; break; case 't': out += '\t'; break; case 'u': { unsigned int cp = 0; if (!Hex4(s_, pos_, cp)) { err = "bad unicode escape"; return false; } pos_ += 4; if (cp >= 0xD800 && cp <= 0xDBFF && pos_ + 1 < s_.size() && s_[pos_] == '\\' && s_[pos_ + 1] == 'u') { unsigned int lo = 0; if (Hex4(s_, pos_ + 2, lo) && lo >= 0xDC00 && lo <= 0xDFFF) { cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00); pos_ += 6; } } AppendUtf8(out, cp); break; } default: out += e; break; } } else { out += c; ++pos_; } } err = "unterminated string"; return false; } bool ParseNumber(Json& out, std::string& err) { size_t start = pos_; if (pos_ < s_.size() && (s_[pos_] == '-' || s_[pos_] == '+')) ++pos_; bool hasDigit = false; while (pos_ < s_.size() && s_[pos_] >= '0' && s_[pos_] <= '9') { ++pos_; hasDigit = true; } if (pos_ < s_.size() && s_[pos_] == '.') { ++pos_; while (pos_ < s_.size() && s_[pos_] >= '0' && s_[pos_] <= '9') { ++pos_; hasDigit = true; } } if (hasDigit && pos_ < s_.size() && (s_[pos_] == 'e' || s_[pos_] == 'E')) { ++pos_; if (pos_ < s_.size() && (s_[pos_] == '-' || s_[pos_] == '+')) ++pos_; while (pos_ < s_.size() && s_[pos_] >= '0' && s_[pos_] <= '9') ++pos_; } if (!hasDigit) { err = "invalid number"; return false; } std::string num = s_.substr(start, pos_ - start); JsonAccess::setType(out, Json::Number); JsonAccess::impl(out).numVal = strtod(num.c_str(), nullptr); return true; } bool ParseArray(Json& out, std::string& err) { ++pos_; JsonAccess::setType(out, Json::Array); SkipSpace(); if (pos_ < s_.size() && s_[pos_] == ']') { ++pos_; return true; } while (pos_ < s_.size()) { Json item; if (!ParseValue(item, err)) return false; JsonAccess::impl(out).arrVal.push_back(item); SkipSpace(); if (pos_ < s_.size() && s_[pos_] == ',') { ++pos_; continue; } if (pos_ < s_.size() && s_[pos_] == ']') { ++pos_; return true; } err = "expected , or ]"; return false; } err = "unterminated array"; return false; } bool ParseObject(Json& out, std::string& err) { ++pos_; JsonAccess::setType(out, Json::Object); SkipSpace(); if (pos_ < s_.size() && s_[pos_] == '}') { ++pos_; return true; } while (pos_ < s_.size()) { SkipSpace(); std::string key; if (!ParseString(key, err)) return false; SkipSpace(); if (pos_ >= s_.size() || s_[pos_] != ':') { err = "expected :"; return false; } ++pos_; Json value; if (!ParseValue(value, err)) return false; JsonAccess::impl(out).objVal[key] = value; SkipSpace(); if (pos_ < s_.size() && s_[pos_] == ',') { ++pos_; continue; } if (pos_ < s_.size() && s_[pos_] == '}') { ++pos_; return true; } err = "expected , or }"; return false; } err = "unterminated object"; return false; } const std::string& s_; size_t pos_ = 0; }; } bool Json::Parse(const std::string& text, Json& out, std::string& err) { Parser parser(text); Json value; if (!parser.ParseValue(value, err)) return false; JsonAccess::setType(out, value.type()); JsonAccess::impl(out) = JsonAccess::impl(value); return true; } }