// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Value's JSON text, the format the app and crafter-debug exchange. Parse // and Dump round-trip every kind, escapes and \u sequences (surrogate pairs // included) decode to UTF-8, integers stay integers and whole doubles stay // doubles, non-finite doubles are written as the strings NaN and Infinity, // invalid UTF-8 in a string is replaced so the output always parses, and // malformed text is refused with the offset of the problem. import std; import Crafter.Debug.Core; using namespace Crafter::Debug; namespace { std::int32_t failures = 0; void Check(bool ok, std::string_view what) { std::println("{} {}", ok ? "PASS" : "FAIL", what); if (!ok) ++failures; } bool Refused(std::string_view text) { try { (void)Value::Parse(text); return false; } catch (const ParseError&) { return true; } } } int main() { const Value parsed = Value::Parse(R"({"a":1,"b":[true,false,null],"c":"x\"y\\z\n","d":-2.5,"e":{"f":"éé😀"}})"); Check(parsed.IsObject() && parsed.Size() == 5, "parses an object with five members"); Check(parsed.Find("a") != nullptr && parsed.Find("a")->IsInteger() && parsed.Find("a")->AsInteger() == 1, "an integer stays an integer"); Check(parsed.Find("d")->Kind() == ValueKind::Number && parsed.Find("d")->AsNumber() == -2.5, "a fraction is a number"); Check(parsed.Find("c")->AsString() == "x\"y\\z\n", "escapes decode"); Check(parsed.Find("e")->Find("f")->AsString() == "\xC3\xA9\xC3\xA9\xF0\x9F\x98\x80", "\\u escapes and surrogate pairs decode to UTF-8"); Check(Value::Parse(parsed.Dump()) == parsed, "compact text round-trips"); Check(Value::Parse(parsed.DumpPretty()) == parsed, "pretty text round-trips"); Check(parsed.Dump().starts_with(R"({"a":1,"b":[true,false,null],)"), "members keep their order"); Check(Value(1.0).Dump() == "1.0", "a whole double keeps a fraction"); Check(Value::Parse("1.0").Kind() == ValueKind::Number, "1.0 reads back as a number"); Check(Value(1.0) == Value(1), "Integer 1 equals Number 1.0"); Check(Value(std::numeric_limits::quiet_NaN()).Dump() == "\"NaN\"", "NaN is written as a string"); Check(Value(-std::numeric_limits::infinity()).Dump() == "\"-Infinity\"", "-Infinity is written as a string"); Check(Value(std::numeric_limits::max()).Kind() == ValueKind::Number, "a uint64 above the int64 range becomes a number"); Check(Value::Parse("9223372036854775807").AsInteger() == std::numeric_limits::max(), "int64 max parses as an integer"); Check(Value::Parse("9223372036854775808").Kind() == ValueKind::Number, "beyond int64 parses as a number"); Check(Value(std::string("ok\xff\xc3")).Dump() == "\"ok\xEF\xBF\xBD\xEF\xBF\xBD\"", "invalid UTF-8 becomes U+FFFD"); Check(Value(std::string("\x01\x7f")).Dump() == "\"\\u0001\\u007f\"", "control characters are escaped"); Check(Value::Parse(Value(std::string("tab\tquote\"")).Dump()).AsString() == "tab\tquote\"", "escaped text reads back"); Check(Refused(R"({"a":1,})"), "a trailing comma is refused"); Check(Refused("[1 2]"), "a missing comma is refused"); Check(Refused("01"), "a leading zero is refused"); Check(Refused(R"("\ud800")"), "an unpaired surrogate is refused"); Check(Refused("\"a\nb\""), "a raw newline in a string is refused"); Check(Refused("1e999"), "an out-of-range number is refused"); Check(Refused(std::string(300, '[') + std::string(300, ']')), "nesting beyond the limit is refused"); Check(Refused(""), "empty text is refused"); try { (void)Value::Parse("[1, x]"); Check(false, "a parse error carries its offset"); } catch (const ParseError& error) { Check(error.offset == 4, std::format("a parse error carries its offset ({})", error.offset)); } Value object; object["z"] = Value(1); object["a"] = Value("two"); object.Set("z", Value(3)); Check(object.Dump() == R"({"z":3,"a":"two"})", "Set replaces in place, keeping the order"); Check(object.Erase("z") && object.Dump() == R"({"a":"two"})", "Erase removes a member"); Check(Value::Parse(R"({"a":1,"b":2})") == Value::Parse(R"({"b":2,"a":1})"), "member order does not affect equality"); Check(!(Value::Parse("[1,2]") == Value::Parse("[2,1]")), "item order does affect equality"); return failures == 0 ? 0 : 1; }