Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
299 lines
8.3 KiB
C++
299 lines
8.3 KiB
C++
#pragma once
|
|
// Spec: molo::app::validate JSON codec. Minimal recursive descent parser
|
|
// and serializer for form binding, error bodies and template tojson.
|
|
// Depth cap kMaxJsonDepth; document size capped by the caller
|
|
// (kMaxJsonDocument default, overridable per route down to 0).
|
|
|
|
#include <cctype>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#include "../caps.h"
|
|
#include "value.h"
|
|
|
|
namespace molo::app::validate {
|
|
|
|
class JsonError : public std::runtime_error {
|
|
public:
|
|
explicit JsonError(const std::string& msg) : std::runtime_error(msg) {}
|
|
};
|
|
|
|
class JsonParser {
|
|
public:
|
|
explicit JsonParser(std::string_view text, std::size_t max_depth = caps::kMaxJsonDepth)
|
|
: text_(text), max_depth_(max_depth) {}
|
|
|
|
Value parse() {
|
|
skip_ws();
|
|
if (pos_ >= text_.size()) throw JsonError("empty JSON document");
|
|
Value v = parse_value(0);
|
|
skip_ws();
|
|
if (pos_ != text_.size()) throw JsonError("trailing bytes after JSON document");
|
|
return v;
|
|
}
|
|
|
|
private:
|
|
void skip_ws() {
|
|
while (pos_ < text_.size()) {
|
|
char c = text_[pos_];
|
|
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
|
|
++pos_;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
char peek() const {
|
|
if (pos_ >= text_.size()) throw JsonError("unexpected end of JSON");
|
|
return text_[pos_];
|
|
}
|
|
|
|
Value parse_value(std::size_t depth) {
|
|
if (depth > max_depth_) throw JsonError("JSON nesting exceeds depth cap");
|
|
char c = peek();
|
|
if (c == '{') return parse_object(depth);
|
|
if (c == '[') return parse_array(depth);
|
|
if (c == '"') return Value(parse_string());
|
|
if (c == 't') return parse_literal("true", Value(true));
|
|
if (c == 'f') return parse_literal("false", Value(false));
|
|
if (c == 'n') return parse_literal("null", Value::null());
|
|
if (c == '-' || (c >= '0' && c <= '9')) return parse_number();
|
|
throw JsonError(std::string("unexpected character in JSON: ") + c);
|
|
}
|
|
|
|
Value parse_literal(const char* word, Value v) {
|
|
for (const char* p = word; *p; ++p) {
|
|
if (pos_ >= text_.size() || text_[pos_] != *p) throw JsonError("bad JSON literal");
|
|
++pos_;
|
|
}
|
|
return v;
|
|
}
|
|
|
|
Value parse_object(std::size_t depth) {
|
|
++pos_; // {
|
|
Object obj;
|
|
skip_ws();
|
|
if (peek() == '}') {
|
|
++pos_;
|
|
return Value(std::move(obj));
|
|
}
|
|
while (true) {
|
|
skip_ws();
|
|
if (peek() != '"') throw JsonError("expected string key in JSON object");
|
|
std::string key = parse_string();
|
|
skip_ws();
|
|
if (peek() != ':') throw JsonError("expected colon in JSON object");
|
|
++pos_;
|
|
skip_ws();
|
|
obj[std::move(key)] = parse_value(depth + 1);
|
|
skip_ws();
|
|
char c = peek();
|
|
if (c == ',') {
|
|
++pos_;
|
|
continue;
|
|
}
|
|
if (c == '}') {
|
|
++pos_;
|
|
return Value(std::move(obj));
|
|
}
|
|
throw JsonError("expected comma or brace in JSON object");
|
|
}
|
|
}
|
|
|
|
Value parse_array(std::size_t depth) {
|
|
++pos_; // [
|
|
Array arr;
|
|
skip_ws();
|
|
if (peek() == ']') {
|
|
++pos_;
|
|
return Value(std::move(arr));
|
|
}
|
|
while (true) {
|
|
skip_ws();
|
|
arr.push_back(parse_value(depth + 1));
|
|
skip_ws();
|
|
char c = peek();
|
|
if (c == ',') {
|
|
++pos_;
|
|
continue;
|
|
}
|
|
if (c == ']') {
|
|
++pos_;
|
|
return Value(std::move(arr));
|
|
}
|
|
throw JsonError("expected comma or bracket in JSON array");
|
|
}
|
|
}
|
|
|
|
std::string parse_string() {
|
|
++pos_; // opening quote
|
|
std::string out;
|
|
while (true) {
|
|
if (pos_ >= text_.size()) throw JsonError("unterminated JSON string");
|
|
char c = text_[pos_++];
|
|
if (c == '"') return out;
|
|
if (c == '\\') {
|
|
if (pos_ >= text_.size()) throw JsonError("bad JSON escape");
|
|
char e = text_[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': {
|
|
if (pos_ + 4 > text_.size()) throw JsonError("bad unicode escape");
|
|
unsigned code = 0;
|
|
for (int i = 0; i < 4; ++i) {
|
|
char h = text_[pos_++];
|
|
code <<= 4;
|
|
if (h >= '0' && h <= '9') code |= (unsigned)(h - '0');
|
|
else if (h >= 'a' && h <= 'f') code |= (unsigned)(h - 'a' + 10);
|
|
else if (h >= 'A' && h <= 'F') code |= (unsigned)(h - 'A' + 10);
|
|
else throw JsonError("bad unicode escape");
|
|
}
|
|
if (code < 0x80) {
|
|
out += (char)code;
|
|
} else if (code < 0x800) {
|
|
out += (char)(0xC0 | (code >> 6));
|
|
out += (char)(0x80 | (code & 0x3F));
|
|
} else {
|
|
out += (char)(0xE0 | (code >> 12));
|
|
out += (char)(0x80 | ((code >> 6) & 0x3F));
|
|
out += (char)(0x80 | (code & 0x3F));
|
|
}
|
|
break;
|
|
}
|
|
default: throw JsonError("bad JSON escape");
|
|
}
|
|
} else {
|
|
out += c;
|
|
}
|
|
}
|
|
}
|
|
|
|
Value parse_number() {
|
|
std::size_t start = pos_;
|
|
if (peek() == '-') ++pos_;
|
|
while (pos_ < text_.size() && std::isdigit((unsigned char)text_[pos_])) ++pos_;
|
|
bool is_float = false;
|
|
if (pos_ < text_.size() && text_[pos_] == '.') {
|
|
is_float = true;
|
|
++pos_;
|
|
while (pos_ < text_.size() && std::isdigit((unsigned char)text_[pos_])) ++pos_;
|
|
}
|
|
if (pos_ < text_.size() && (text_[pos_] == 'e' || text_[pos_] == 'E')) {
|
|
is_float = true;
|
|
++pos_;
|
|
if (pos_ < text_.size() && (text_[pos_] == '+' || text_[pos_] == '-')) ++pos_;
|
|
while (pos_ < text_.size() && std::isdigit((unsigned char)text_[pos_])) ++pos_;
|
|
}
|
|
std::string tok(text_.substr(start, pos_ - start));
|
|
try {
|
|
if (is_float) return Value(std::stod(tok));
|
|
return Value((long long)std::stoll(tok));
|
|
} catch (...) {
|
|
throw JsonError("bad JSON number: " + tok);
|
|
}
|
|
}
|
|
|
|
std::string_view text_;
|
|
std::size_t pos_ = 0;
|
|
std::size_t max_depth_;
|
|
};
|
|
|
|
inline Value parse_json(std::string_view text, std::size_t max_depth = caps::kMaxJsonDepth) {
|
|
JsonParser parser(text, max_depth);
|
|
return parser.parse();
|
|
}
|
|
|
|
inline void escape_json_string(const std::string& s, std::string& out) {
|
|
out += '"';
|
|
for (unsigned char c : s) {
|
|
switch (c) {
|
|
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;
|
|
default:
|
|
if (c < 0x20) {
|
|
char buf[8];
|
|
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
|
|
out += buf;
|
|
} else {
|
|
out += (char)c;
|
|
}
|
|
}
|
|
}
|
|
out += '"';
|
|
}
|
|
|
|
inline void write_json_value(const Value& v, std::string& out, std::size_t depth) {
|
|
if (depth > caps::kMaxJsonDepth) throw JsonError("value exceeds JSON depth cap");
|
|
if (v.is_null() || v.is_undefined()) {
|
|
out += "null";
|
|
return;
|
|
}
|
|
if (const auto* b = std::get_if<bool>(&v.storage())) {
|
|
out += *b ? "true" : "false";
|
|
return;
|
|
}
|
|
if (const auto* i = std::get_if<long long>(&v.storage())) {
|
|
out += std::to_string(*i);
|
|
return;
|
|
}
|
|
if (const auto* d = std::get_if<double>(&v.storage())) {
|
|
char buf[32];
|
|
std::snprintf(buf, sizeof(buf), "%.17g", *d);
|
|
out += buf;
|
|
return;
|
|
}
|
|
if (v.is_string() || v.is_safe()) {
|
|
escape_json_string(v.as_text(), out);
|
|
return;
|
|
}
|
|
if (const auto* a = std::get_if<Array>(&v.storage())) {
|
|
out += '[';
|
|
bool first = true;
|
|
for (const auto& item : *a) {
|
|
if (!first) out += ',';
|
|
first = false;
|
|
write_json_value(item, out, depth + 1);
|
|
}
|
|
out += ']';
|
|
return;
|
|
}
|
|
if (const auto* o = std::get_if<Object>(&v.storage())) {
|
|
out += '{';
|
|
bool first = true;
|
|
for (const auto& kv : *o) {
|
|
if (!first) out += ',';
|
|
first = false;
|
|
escape_json_string(kv.first, out);
|
|
out += ':';
|
|
write_json_value(kv.second, out, depth + 1);
|
|
}
|
|
out += '}';
|
|
return;
|
|
}
|
|
out += "null";
|
|
}
|
|
|
|
inline std::string to_json(const Value& v) {
|
|
std::string out;
|
|
write_json_value(v, out, 0);
|
|
return out;
|
|
}
|
|
|
|
} // namespace molo::app::validate
|