JsonParserByLeo/Test/SaxTest.mq5

90 lines
4.3 KiB
MQL5

2026-08-04 09:03:45 -05:00
//+------------------------------------------------------------------+
//| SaxTest.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
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-09-03 11:13:49 -05:00
#include "..\\Src\\Json.mqh"
2026-08-04 09:03:45 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CTestStream : public TSN::ITsnJsonParserStream
{
2026-10-01 13:36:22 -05:00
private:
string ToStr(const uchar& in[], int i, int len)
{
string str;
return TSN::CJsonParser::MakeString(in, i, len, str) ? str : NULL;
}
2026-08-04 09:03:45 -05:00
public:
CTestStream(void) {}
~CTestStream(void) {}
//---
void OnStartDocument() override final { Print("Inicio de documento"); }
void OnEndDocument() override final { Print("Fin de documento"); }
void OnStartObj() override final { Print("Inicio de objeto: ", m_ctx.CurrentStack()); }
void OnEndObJ() override final { Print("Fin de objeto: ", m_ctx.CurrentStack()); }
void OnStartArr() override final { Print("Inicio de array: ", m_ctx.CurrentStack()); }
void OnEndArr() override final { Print("Fin de array: ", m_ctx.CurrentStack()); }
void OnInteger(long v) override final { Print("Numero: ", v); }
void OnReal(double v) override final { Print("Real: ", v); }
2026-10-01 13:36:22 -05:00
void OnString(int s, int len) override final { Print("Texto: ", ToStr(m_ctx.m_raw, s, len)); }
2026-08-04 09:03:45 -05:00
void OnBool(bool v) override final { Print("Boleano: ", v); }
void OnNull() override final { Print("Null"); }
2026-10-01 13:36:22 -05:00
void OnKey(int s, int len) override final { Print("Clave: ", ToStr(m_ctx.m_raw, s, len)); }
2026-10-03 17:00:34 -05:00
void OnError() { Print(EnumToString(m_ctx.LastErr())); }
2026-10-01 13:36:22 -05:00
2026-10-02 22:01:54 -05:00
void OnBigDecimal(int s, int len) override final { Print("Big decimal: ", CharArrayToString(m_ctx.m_raw, s, len)); }
void OnBigInteger(int s, int len) override final { Print("Big integer: ", CharArrayToString(m_ctx.m_raw, s, len)); }
2026-08-04 09:03:45 -05:00
};
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
TSN::CJsonParserStream stream;
stream.SetCaller(new CTestStream(), true);
//---
2026-10-01 13:36:22 -05:00
string json = "{\"ae\":[[10,10],[20,20]],\"comida\":\"\\ud83d\\ude00\",\"trapo\":10e+2,\"a\":true,\"invalid\":\"☣\",\"precios\":[10,20,30,40,50],\"va\":1"
2026-08-04 09:03:45 -05:00
",\"interpolado\":\"${{valor}}\"}";
uchar raw[];
2026-10-03 17:00:34 -05:00
const int l = StringToCharArray(json, raw, 0, WHOLE_ARRAY, CP_UTF8);
2026-08-04 09:03:45 -05:00
int p = 0;
2026-10-03 17:00:34 -05:00
//Print(CharArrayToString(raw));
//Print(CharToString(raw[0]));
2026-08-04 09:03:45 -05:00
// Simular pasadas de 64 bytes
while(p < l) // tien que ser menor, en un caso real quizas ahsta on end document
{
//--- le damos datos
// Tambien antes de dar los datos odemos llamar a CheckLimpieza
// Para que mueva pos a posicion 0 (reutlziacion) en caso se pueda si no seguir acomulando
// Ahora en este caso vamos sumando dado que m_len neceista crecer.... luego quizas se le peude reinicar..
stream.m_len += ArrayCopy(stream.m_raw, raw, stream.m_pos, p, 64); //
// intermante va parseando aumentado m_pos
// Otro si no da el documento para otro el bulce se dentecia aunqeu array copy ya menaja eso intemante
// si nos pamaos normaliza asi que no hay problema pero eso un aviso..
2026-10-03 17:00:34 -05:00
if(!stream.ParseSreamSax())
{
Print("Error");
return;
}
2026-08-04 09:03:45 -05:00
// Aqui por ejemplo se peuden hacer mas cosas.. tu lo llamas cuando quieres
p += 64; // ya le dimos 64
}
}
//+------------------------------------------------------------------+