JsonParserByLeo/Test/Dom.mq5
2026-09-03 11:13:49 -05:00

83 lines
2.7 KiB
MQL5

//+------------------------------------------------------------------+
//| Dom.mq5 |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com/"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "..\\Src\\Json.mqh"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
string json = "{\"ae\":[[10,10],[20,20]],\"comida\":\"\\ud83d\\ude00\",\"trapo\":10.05,\"a\":true,\"invalid\":\"\\xFF\",\"precios\":[10,20,30,40,50],\"va\":1"
",\"interpolado\":\"${{valor}}\"}";
//Print(json);
//---
TSN::CJsonParser parser;
parser.Assing(json);
parser.CorrectPadding();
parser.Parse();
parser.PrintCintaTypes(0, WHOLE_ARRAY);
//---
TSN::CDomNodeManager manager;
TSN::CJsonDomSerializer serializer;
TSN::CDomNodeBase root(&manager);
parser.GetRoot().ToDom(&root, &manager);
//---
string kes[];
root.GetKeys(kes);
ArrayPrint(kes);
//---
Print(root.Size());
Print(root["trapo"].ToDouble(0.0));
Print(root["ae"].At(1).At(1).ToInt(0));
Print(root["comida"].ToString());
// Agregar y cambiar tipo
root["sumando"] = 20.0;
Print(root.Size());
Print(root["sumando"].ToDouble(0.0));
root["sumando"] = true;
Print(root["sumando"].ToBool(false));
//--- Estadisticas
Print("Numero de arrays creados: ", manager.m_node_size);
Print("Numero de objetos creados: ", manager.m_objs_size);
Print("Numero de strgins creados: ", manager.m_string_stack_size);
//--- Otro json
TSN::CDomNodeManager manager2;
TSN::CDomNodeBase* ptr = new TSN::CDomNodeBase(&manager2); // vacio
ptr.NewObj(128);
ptr["valores"] = 20.0;
//---
root.Set("nueva_key", ptr); // No copia profunda si no PUNTERO..
ptr["coma"] = true;
TSN::CDomNodeBase* out[];
const int t = root["precios"].PySlicing(out, 0, -1, -1, -1);
for(int i = 0; i < t; i++)
Print("valor: ", out[i].ToInt(0)); // al revez 50,40,30,20,10
//---
Print(serializer.Serialize(&root));
Print(CharArrayToString(serializer.m_buf, 0, serializer.m_buf_s));
//---
delete ptr;
}
//+------------------------------------------------------------------+