2026-01-25 23:16:07 -03:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| TestJsonBuilder.mq5 |
|
|
|
|
|
//| AI-Toolkit |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
#property script_show_inputs
|
2026-01-25 23:39:19 -03:00
|
|
|
#include "../fast_json.mqh"
|
2026-01-25 23:16:07 -03:00
|
|
|
|
2026-01-25 23:39:19 -03:00
|
|
|
void OnStart() {
|
|
|
|
|
Print("=== JSON V3: THE BUILDER & UNICODE UPDATE ===");
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
// 1. UNICODE TEST
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
Print("--- 1. Unicode Escape Support ---");
|
|
|
|
|
string json_uni =
|
|
|
|
|
"{\"msg\": \"\\u0048\\u0065\\u006C\\u006C\\u006F\"}"; // Hello
|
|
|
|
|
CJson doc_uni;
|
|
|
|
|
if (doc_uni.Parse(json_uni)) {
|
|
|
|
|
string msg = doc_uni["msg"].ToString();
|
|
|
|
|
Print("Encoded: \\u0048\\u0065\\u006C\\u006C\\u006F");
|
|
|
|
|
Print("Decoded: ", msg);
|
|
|
|
|
|
|
|
|
|
if (msg == "Hello")
|
|
|
|
|
Print("PASS: Unicode Basic");
|
|
|
|
|
else
|
|
|
|
|
Print("FAIL: Unicode Decoding");
|
|
|
|
|
} else
|
|
|
|
|
Print("FAIL: Unicode Parse Error");
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
// 2. BUILDER TEST
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
Print("\n--- 2. CJsonBuilder Performance ---");
|
|
|
|
|
ulong t0 = GetMicrosecondCount();
|
|
|
|
|
|
|
|
|
|
CJsonBuilder b(1024);
|
|
|
|
|
b.Obj()
|
|
|
|
|
.Key("action")
|
|
|
|
|
.Val("trade")
|
2026-01-25 23:16:07 -03:00
|
|
|
.Key("params")
|
|
|
|
|
.Obj()
|
2026-01-25 23:39:19 -03:00
|
|
|
.Key("symbol")
|
|
|
|
|
.Val("EURUSD")
|
|
|
|
|
.Key("volume")
|
|
|
|
|
.Val(0.1)
|
|
|
|
|
.Key("magic")
|
|
|
|
|
.Val(123456)
|
|
|
|
|
.Key("comment")
|
|
|
|
|
.Val("AI-Generated \u2764") // Heart emoji (native string)
|
2026-01-25 23:16:07 -03:00
|
|
|
.EndObj()
|
2026-01-25 23:39:19 -03:00
|
|
|
.Key("flags")
|
|
|
|
|
.Arr()
|
|
|
|
|
.Val(1)
|
|
|
|
|
.Val(2)
|
|
|
|
|
.Val(3)
|
2026-01-25 23:16:07 -03:00
|
|
|
.EndArr()
|
2026-01-25 23:39:19 -03:00
|
|
|
.EndObj();
|
2026-01-25 23:16:07 -03:00
|
|
|
|
2026-01-25 23:39:19 -03:00
|
|
|
string built = b.Build();
|
|
|
|
|
ulong t_build = GetMicrosecondCount() - t0;
|
|
|
|
|
|
|
|
|
|
Print("Built JSON: ", built);
|
|
|
|
|
Print("Build Time: ", t_build, " us");
|
2026-01-25 23:16:07 -03:00
|
|
|
|
2026-01-25 23:39:19 -03:00
|
|
|
// Verify Validity
|
|
|
|
|
CJson check;
|
|
|
|
|
if (check.Parse(built)) {
|
|
|
|
|
if (check["params"]["symbol"].ToString() == "EURUSD" &&
|
|
|
|
|
check["flags"][1].ToInt() == 2) {
|
|
|
|
|
Print("PASS: Builder Output is Valid JSON");
|
|
|
|
|
} else
|
|
|
|
|
Print("FAIL: Builder Content Verification");
|
|
|
|
|
} else
|
|
|
|
|
Print("FAIL: Builder produced invalid JSON");
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
// 3. SAFE ACCESSORS
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
Print("\n--- 3. Safe Accessors ---");
|
|
|
|
|
CJsonNode root = check.GetRoot();
|
|
|
|
|
|
|
|
|
|
string missing = root["non_existent"].ToString("default_value");
|
|
|
|
|
Print("Missing Key (String): ", missing);
|
|
|
|
|
if (missing == "default_value")
|
|
|
|
|
Print("PASS: Safe String");
|
|
|
|
|
else
|
|
|
|
|
Print("FAIL: Safe String");
|
|
|
|
|
|
|
|
|
|
double def_dbl = root["non_existent"].ToDouble(-1.0);
|
|
|
|
|
Print("Missing Key (Double): ", def_dbl);
|
|
|
|
|
if (def_dbl == -1.0)
|
|
|
|
|
Print("PASS: Safe Double");
|
|
|
|
|
else
|
|
|
|
|
Print("FAIL: Safe Double");
|
|
|
|
|
|
|
|
|
|
Print("=== TEST COMPLETE ===");
|
|
|
|
|
}
|