Files
aevolution/src/serialization/json.cpp
T
dingoandClaude Opus 5.5 9b854e6960
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m31s
Foundation / postgres-tools (push) Successful in 27s
Foundation / server-image (push) Successful in 4m33s
Foundation / image (push) Successful in 5m39s
feat(admin): open a fresh region from the portal (ADR-0182 section 15)
The Regions page opens the next region in place of the one DEV serves: the
admin API reads the region overlay, its kustomization, the grant broker and the
portal deployment from main at one commit, rewrites only the region id, routes,
genesis commit and seeded wildlife range (refusing files whose layout differs),
commits them in one Gitea multi-file commit as aevolution-bot, then starts a full
Argo sync of exactly that commit. The page follows the sync until the region is
live, across the portal's own restart.

Credentials come from Secret aevolution-admin mounted read-only as files
(--deploy-config); without it the feature stays off. A small OpenSSL HTTPS
client talks to Gitea (verified) and argocd-server (verification per
ARGOCD_TLS_VERIFY). The portal's NetworkPolicy gains egress to the node's
ingress for Gitea and to the argocd-server pods, as approved.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 13:51:36 -04:00

302 lines
9.9 KiB
C++

#include "evo/serialization/json.hpp"
#include <charconv>
#include <limits>
namespace evo::serialization {
namespace {
void append_utf8(std::string& out, std::uint32_t code) {
if (code < 0x80) {
out.push_back(static_cast<char>(code));
} else if (code < 0x800) {
out.push_back(static_cast<char>(0xC0 | (code >> 6U)));
out.push_back(static_cast<char>(0x80 | (code & 0x3FU)));
} else if (code < 0x10000) {
out.push_back(static_cast<char>(0xE0 | (code >> 12U)));
out.push_back(static_cast<char>(0x80 | ((code >> 6U) & 0x3FU)));
out.push_back(static_cast<char>(0x80 | (code & 0x3FU)));
} else {
out.push_back(static_cast<char>(0xF0 | (code >> 18U)));
out.push_back(static_cast<char>(0x80 | ((code >> 12U) & 0x3FU)));
out.push_back(static_cast<char>(0x80 | ((code >> 6U) & 0x3FU)));
out.push_back(static_cast<char>(0x80 | (code & 0x3FU)));
}
}
} // namespace
class JsonParser {
public:
explicit JsonParser(std::string_view text) : text_{text} {}
std::optional<JsonValue> 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<std::uint32_t> 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<std::uint32_t>(ch - '0');
else if (ch >= 'a' && ch <= 'f')
value |= static_cast<std::uint32_t>(ch - 'a' + 10);
else if (ch >= 'A' && ch <= 'F')
value |= static_cast<std::uint32_t>(ch - 'A' + 10);
else
return std::nullopt;
}
return value;
}
std::optional<std::string> parse_string() {
if (at_ >= text_.size() || text_[at_] != '"')
return std::nullopt;
++at_;
std::string out;
while (at_ < text_.size()) {
const auto ch = static_cast<unsigned char>(text_[at_++]);
if (ch == '"')
return out;
if (ch < 0x20)
return std::nullopt;
if (ch != '\\') {
out.push_back(static_cast<char>(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<JsonValue> 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<JsonValue> 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> JsonValue::parse(std::string_view text, std::size_t limit) {
if (text.size() > limit)
return std::nullopt;
return JsonParser{text}.document();
}
std::optional<bool> JsonValue::boolean() const {
if (kind_ != Kind::boolean)
return std::nullopt;
return boolean_;
}
std::optional<std::string_view> JsonValue::string() const {
if (kind_ != Kind::string)
return std::nullopt;
return std::string_view{text_};
}
std::optional<std::uint64_t> 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::uint64_t>(std::numeric_limits<std::int64_t>::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>* JsonValue::array() const {
return kind_ == Kind::array ? &items_ : nullptr;
}
} // namespace evo::serialization