#include "evo/serialization/json.hpp" #include #include namespace evo::serialization { namespace { void append_utf8(std::string& out, std::uint32_t code) { if (code < 0x80) { out.push_back(static_cast(code)); } else if (code < 0x800) { out.push_back(static_cast(0xC0 | (code >> 6U))); out.push_back(static_cast(0x80 | (code & 0x3FU))); } else if (code < 0x10000) { out.push_back(static_cast(0xE0 | (code >> 12U))); out.push_back(static_cast(0x80 | ((code >> 6U) & 0x3FU))); out.push_back(static_cast(0x80 | (code & 0x3FU))); } else { out.push_back(static_cast(0xF0 | (code >> 18U))); out.push_back(static_cast(0x80 | ((code >> 12U) & 0x3FU))); out.push_back(static_cast(0x80 | ((code >> 6U) & 0x3FU))); out.push_back(static_cast(0x80 | (code & 0x3FU))); } } } // namespace class JsonParser { public: explicit JsonParser(std::string_view text) : text_{text} {} std::optional document() { auto value = parse_value(0); skip_space(); if (!value || at_ != text_.size()) return std::nullopt; return value; } private: void skip_space() { while (at_ < text_.size() && (text_[at_] == ' ' || text_[at_] == '\t' || text_[at_] == '\n' || text_[at_] == '\r')) ++at_; } bool literal(std::string_view word) { if (text_.substr(at_, word.size()) != word) return false; at_ += word.size(); return true; } std::optional hex4() { if (at_ + 4 > text_.size()) return std::nullopt; std::uint32_t value{}; for (int i = 0; i < 4; ++i) { const char ch = text_[at_++]; value <<= 4U; if (ch >= '0' && ch <= '9') value |= static_cast(ch - '0'); else if (ch >= 'a' && ch <= 'f') value |= static_cast(ch - 'a' + 10); else if (ch >= 'A' && ch <= 'F') value |= static_cast(ch - 'A' + 10); else return std::nullopt; } return value; } std::optional parse_string() { if (at_ >= text_.size() || text_[at_] != '"') return std::nullopt; ++at_; std::string out; while (at_ < text_.size()) { const auto ch = static_cast(text_[at_++]); if (ch == '"') return out; if (ch < 0x20) return std::nullopt; if (ch != '\\') { out.push_back(static_cast(ch)); continue; } if (at_ >= text_.size()) return std::nullopt; switch (text_[at_++]) { case '"': out.push_back('"'); break; case '\\': out.push_back('\\'); break; case '/': out.push_back('/'); break; case 'b': out.push_back('\b'); break; case 'f': out.push_back('\f'); break; case 'n': out.push_back('\n'); break; case 'r': out.push_back('\r'); break; case 't': out.push_back('\t'); break; case 'u': { auto code = hex4(); if (!code || (*code >= 0xDC00 && *code <= 0xDFFF)) return std::nullopt; if (*code >= 0xD800 && *code <= 0xDBFF) { if (!literal("\\u")) return std::nullopt; const auto low = hex4(); if (!low || *low < 0xDC00 || *low > 0xDFFF) return std::nullopt; code = 0x10000 + ((*code - 0xD800) << 10U) + (*low - 0xDC00); } append_utf8(out, *code); break; } default: return std::nullopt; } } return std::nullopt; } std::optional parse_number() { const auto start = at_; if (at_ < text_.size() && text_[at_] == '-') ++at_; if (at_ >= text_.size()) return std::nullopt; if (text_[at_] == '0') { ++at_; } else if (text_[at_] >= '1' && text_[at_] <= '9') { while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9') ++at_; } else { return std::nullopt; } if (at_ < text_.size() && text_[at_] == '.') { ++at_; const auto digits = at_; while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9') ++at_; if (at_ == digits) return std::nullopt; } if (at_ < text_.size() && (text_[at_] == 'e' || text_[at_] == 'E')) { ++at_; if (at_ < text_.size() && (text_[at_] == '+' || text_[at_] == '-')) ++at_; const auto digits = at_; while (at_ < text_.size() && text_[at_] >= '0' && text_[at_] <= '9') ++at_; if (at_ == digits) return std::nullopt; } JsonValue value; value.kind_ = JsonValue::Kind::number; value.text_ = std::string(text_.substr(start, at_ - start)); return value; } // `depth` counts the containers enclosing this value. std::optional parse_value(std::size_t depth) { skip_space(); if (at_ >= text_.size()) return std::nullopt; if ((text_[at_] == '{' || text_[at_] == '[') && depth >= max_json_depth) return std::nullopt; JsonValue value; const char ch = text_[at_]; if (ch == '{') { ++at_; value.kind_ = JsonValue::Kind::object; skip_space(); if (at_ < text_.size() && text_[at_] == '}') { ++at_; return value; } for (;;) { skip_space(); auto name = parse_string(); skip_space(); if (!name || at_ >= text_.size() || text_[at_] != ':') return std::nullopt; ++at_; auto member = parse_value(depth + 1); if (!member) return std::nullopt; value.members_.emplace_back(std::move(*name), std::move(*member)); skip_space(); if (at_ >= text_.size()) return std::nullopt; if (text_[at_] == ',') { ++at_; continue; } if (text_[at_++] != '}') return std::nullopt; return value; } } if (ch == '[') { ++at_; value.kind_ = JsonValue::Kind::array; skip_space(); if (at_ < text_.size() && text_[at_] == ']') { ++at_; return value; } for (;;) { auto item = parse_value(depth + 1); if (!item) return std::nullopt; value.items_.push_back(std::move(*item)); skip_space(); if (at_ >= text_.size()) return std::nullopt; if (text_[at_] == ',') { ++at_; continue; } if (text_[at_++] != ']') return std::nullopt; return value; } } if (ch == '"') { auto text = parse_string(); if (!text) return std::nullopt; value.kind_ = JsonValue::Kind::string; value.text_ = std::move(*text); return value; } if (literal("true")) { value.kind_ = JsonValue::Kind::boolean; value.boolean_ = true; return value; } if (literal("false")) { value.kind_ = JsonValue::Kind::boolean; return value; } if (literal("null")) return value; return parse_number(); } std::string_view text_; std::size_t at_{}; }; std::optional JsonValue::parse(std::string_view text, std::size_t limit) { if (text.size() > limit) return std::nullopt; return JsonParser{text}.document(); } std::optional JsonValue::boolean() const { if (kind_ != Kind::boolean) return std::nullopt; return boolean_; } std::optional JsonValue::string() const { if (kind_ != Kind::string) return std::nullopt; return std::string_view{text_}; } std::optional JsonValue::integer() const { if (kind_ != Kind::number || text_.empty() || text_.front() == '-' || text_.find_first_of(".eE") != std::string::npos) return std::nullopt; std::uint64_t value{}; const auto [end, error] = std::from_chars(text_.data(), text_.data() + text_.size(), value); if (error != std::errc{} || end != text_.data() + text_.size() || value > static_cast(std::numeric_limits::max())) return std::nullopt; return value; } const JsonValue* JsonValue::member(std::string_view name) const { if (kind_ != Kind::object) return nullptr; for (const auto& [key, value] : members_) if (key == name) return &value; return nullptr; } const std::vector* JsonValue::array() const { return kind_ == Kind::array ? &items_ : nullptr; } } // namespace evo::serialization