2026-06-03 07:50:45 -05:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Test.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\\JsonNode.mqh"
|
2026-07-10 13:21:26 -05:00
|
|
|
#include <TSN\\MQLArticles\\Utils\\Dict.mqh>
|
2026-06-03 07:50:45 -05:00
|
|
|
|
2026-07-10 13:17:36 -05:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
namespace TSN
|
|
|
|
|
{
|
|
|
|
|
class CInterpolate
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CInterpolate(void) {}
|
|
|
|
|
~CInterpolate(void) {}
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
CDict* m_dict;
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
string Interpolate(const uchar& text[], int i, const int e, const string& def) const
|
|
|
|
|
{
|
|
|
|
|
int r = (e - i) + 1;
|
|
|
|
|
if(r < 1)
|
|
|
|
|
return def;
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
string out = "";
|
|
|
|
|
StringSetLength(out, r);
|
|
|
|
|
int o = 0;
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
while(i <= e)
|
|
|
|
|
{
|
|
|
|
|
const uchar c = text[i];
|
|
|
|
|
if(c == '$' && i + 2 <= e
|
|
|
|
|
&& text[i + 1] == '{'
|
|
|
|
|
&& text[i + 2] == '{')
|
|
|
|
|
{
|
|
|
|
|
int locked = i;
|
|
|
|
|
i += 3;
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
while(i <= e && text[i] < 33)
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
const int start = i;
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
while(i <= e)
|
|
|
|
|
{
|
2026-07-10 17:26:50 -05:00
|
|
|
if(text[i] == '}' && i + 1 <= e && text[i + 1] == '}')
|
2026-07-10 13:17:36 -05:00
|
|
|
{
|
|
|
|
|
int end = i;
|
|
|
|
|
i += 2;
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
end--;
|
|
|
|
|
}
|
|
|
|
|
while(end >= 0 && text[end] < 33);
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
string v;
|
|
|
|
|
if(m_dict.TryGet(text, start, end, v))
|
|
|
|
|
{
|
|
|
|
|
const int len = StringLen(v);
|
|
|
|
|
if(o + len >= r)
|
|
|
|
|
{
|
|
|
|
|
r += (len << 1);
|
|
|
|
|
out.Truncate(r);
|
|
|
|
|
out.Reserve(r);
|
|
|
|
|
}
|
|
|
|
|
for(int k = 0; k < len; k++)
|
|
|
|
|
out.SetChar(o++, v[k]);
|
|
|
|
|
|
|
|
|
|
locked = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-07-10 17:26:50 -05:00
|
|
|
continue;
|
2026-07-10 13:17:36 -05:00
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
if(locked != -1)
|
|
|
|
|
{
|
2026-07-10 17:26:50 -05:00
|
|
|
if(i > e)
|
|
|
|
|
{
|
|
|
|
|
//Print(CharToString(text[locked]));
|
|
|
|
|
for(; locked <= e; locked++)
|
|
|
|
|
out.SetChar(o++, text[locked]);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for(; locked <= i; locked++)
|
|
|
|
|
out.SetChar(o++, text[locked]);
|
|
|
|
|
}
|
2026-07-10 13:17:36 -05:00
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
out.SetChar(o++, c);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
//---
|
|
|
|
|
StringSetLength(out, o);
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-03 07:50:45 -05:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| Script program start function |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
void OnStart()
|
|
|
|
|
{
|
2026-07-10 13:17:36 -05:00
|
|
|
|
2026-06-03 07:50:45 -05:00
|
|
|
//---
|
2026-07-15 16:03:07 -05:00
|
|
|
string json = "{\"ae\":[[10,10],[20,20]],\"comida\":\"\\ud83d\\ude00\",\"trapo\":10.05,\"a\":true,\"invalid\":\"abcdefg\\\\\",\"precios\":[10,20,30,40,50],\"va\":1"
|
|
|
|
|
",\"interpolado\":\"${{valor}}\"}";
|
2026-06-20 13:57:50 -05:00
|
|
|
//Print(json);
|
2026-06-03 07:50:45 -05:00
|
|
|
//---
|
|
|
|
|
TSN::CJsonParser parser;
|
2026-06-04 11:36:42 -05:00
|
|
|
parser.Assing(json);
|
2026-07-19 07:41:52 -05:00
|
|
|
parser.CorrectPadding();
|
2026-06-04 11:36:42 -05:00
|
|
|
parser.Parse();
|
2026-06-20 18:08:19 -05:00
|
|
|
parser.PrintCintaTypes(0, WHOLE_ARRAY);
|
2026-06-03 07:50:45 -05:00
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
TSN::CJsonNode root = parser.GetRoot();
|
2026-07-15 16:03:07 -05:00
|
|
|
uchar buffer[];
|
|
|
|
|
ArrayResize(buffer, 3);
|
|
|
|
|
buffer[0] = '-';
|
|
|
|
|
buffer[1] = '-';
|
|
|
|
|
buffer[2] = ':';
|
|
|
|
|
Print("Copied elemnts: ", root.ComplexToData(buffer, false, 3));
|
2026-07-17 10:08:58 -05:00
|
|
|
Print("Size: ", ArraySize(buffer));
|
2026-07-15 16:03:07 -05:00
|
|
|
Print("Raw string data:\n", CharArrayToString(buffer));
|
2026-06-20 18:08:19 -05:00
|
|
|
|
2026-06-21 20:08:59 -05:00
|
|
|
//---
|
|
|
|
|
Print("Claves: ");
|
|
|
|
|
string keys[];
|
|
|
|
|
root.GetKeys(keys);
|
|
|
|
|
ArrayPrint(keys, _Digits, " | ");
|
|
|
|
|
|
2026-06-24 17:20:42 -05:00
|
|
|
|
|
|
|
|
//--- Calentar para JIT (3 iter)
|
|
|
|
|
for(int i = 0; i < 7; i++)
|
|
|
|
|
{
|
|
|
|
|
root["trapo"].ToDouble(0.00);
|
|
|
|
|
}
|
|
|
|
|
Print("Root is JIT: ", root.ObjectIsJit());
|
|
|
|
|
|
|
|
|
|
|
2026-07-10 13:17:36 -05:00
|
|
|
//---
|
|
|
|
|
TSN::CDict dict;
|
|
|
|
|
TSN::CInterpolate inter;
|
|
|
|
|
dict.Set("valor", true);
|
|
|
|
|
inter.m_dict = &dict;
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
Print("Interpolado: ", root["interpolado"].Interpolate(inter, ""));
|
|
|
|
|
|
2026-06-21 20:08:59 -05:00
|
|
|
//---
|
2026-06-24 17:20:42 -05:00
|
|
|
const string tres = root.AtObjKey(3);
|
|
|
|
|
Print("Tercera key: ", tres, " index: ", root.KeyToPosition(tres));
|
2026-06-21 20:08:59 -05:00
|
|
|
|
2026-06-20 13:57:50 -05:00
|
|
|
//---
|
2026-06-20 18:08:19 -05:00
|
|
|
Print("Trapo val: ", root["trapo"].ToDouble(0.00));
|
2026-07-19 07:41:52 -05:00
|
|
|
|
2026-07-17 10:08:58 -05:00
|
|
|
//---
|
2026-06-21 20:08:59 -05:00
|
|
|
TSN::CJsonNode fa = root["ae"].At(0);
|
|
|
|
|
Print("Valor anidado: ", fa.At(0).ToInt(0)); // 10
|
|
|
|
|
|
|
|
|
|
//---
|
|
|
|
|
int precios[];
|
|
|
|
|
Print("Array precios: ");
|
|
|
|
|
root["precios"].ToArray(precios);
|
|
|
|
|
ArrayPrint(precios, _Digits, " | ");
|
2026-06-20 13:57:50 -05:00
|
|
|
|
2026-06-20 18:08:19 -05:00
|
|
|
//---
|
|
|
|
|
Print("Root key el 0: ", root.AtObjKey(0));
|
|
|
|
|
Print("Root size: ", root.Size());
|
|
|
|
|
Print("Root is valid: ", root.IsValid());
|
|
|
|
|
Print("Root emoji: ", root["comida"].ToString());
|
2026-06-23 08:02:37 -05:00
|
|
|
Print("Root emoji no escape: ", root["comida"].ToStringNoEscape());
|
2026-06-20 18:08:19 -05:00
|
|
|
Print("Root invalid: ", root["invalid"].ToString());
|
2026-06-23 08:02:37 -05:00
|
|
|
Print("Has key comida: ", root.HasKey("comida"));
|
|
|
|
|
Print("First value type obj: ", EnumToString(root.AtObj(0).GetType()));
|
2026-06-30 08:24:44 -05:00
|
|
|
Print("Va value:", root["va"].ToInt(0));
|
2026-06-21 20:08:59 -05:00
|
|
|
|
2026-06-20 18:08:19 -05:00
|
|
|
//---
|
|
|
|
|
TSN::CJsonIteratorObj it = root.BeginObj();
|
|
|
|
|
while(it.IsValid())
|
|
|
|
|
{
|
|
|
|
|
Print("Element of root: ", it.Key());
|
|
|
|
|
it.Next();
|
|
|
|
|
}
|
2026-06-03 07:50:45 -05:00
|
|
|
}
|
|
|
|
|
//+------------------------------------------------------------------+
|