| //+------------------------------------------------------------------+
|
| //| 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"
|
| #include <TSN\\GCol\\Dict.mqh>
|
|
|
| //+------------------------------------------------------------------+
|
| //| |
|
| //+------------------------------------------------------------------+
|
| 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)
|
| {
|
| if(text[i] == '}' && i + 1 <= e && text[i + 1] == '}')
|
| {
|
| 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;
|
| }
|
| continue;
|
| }
|
| i++;
|
| }
|
|
|
| //---
|
| if(locked != -1)
|
| {
|
| 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]);
|
| }
|
| }
|
| continue;
|
| }
|
|
|
| //---
|
| out.SetChar(o++, c);
|
| i++;
|
| }
|
| //---
|
| StringSetLength(out, o);
|
| return out;
|
| }
|
| };
|
| }
|
|
|
| //+------------------------------------------------------------------+
|
| //| Script program start function |
|
| //+------------------------------------------------------------------+
|
| void OnStart()
|
| {
|
|
|
| //---
|
|
|
| string json = "{\"ae\":[[10,10],[20,20],[]],\"comida\":\"\\ud83d\\ude00\",\"trapo\":10.05,\"a\":true,\"invalid\":\"<a href=\\\"https://twitter.com/shiawaseomamori\\\" rel=\\\"nofollow\\\">幸せの☆お守り</a>\",\"precios\":[10,20,30,40,50],\"va\":1"
|
| ",\"interpolado\":\"${{valor}}\"}";
|
|
|
|
|
|
|
| //Print(json);
|
| //---
|
| TSN::CJsonParser parser;
|
| parser.Assing(json);
|
| parser.CorrectPadding();
|
| Print(parser.Parse());
|
| parser.ErrorInfo();
|
| parser.PrintCintaTypes(0, WHOLE_ARRAY);
|
|
|
| //---
|
| TSN::CJsonNode root = parser.GetRoot();
|
| uchar buffer[];
|
| ArrayResize(buffer, 3);
|
| buffer[0] = '-';
|
| buffer[1] = '-';
|
| buffer[2] = ':';
|
| Print("Copied elemnts: ", root.ComplexToData(buffer, false, 3));
|
| Print("Size: ", ArraySize(buffer));
|
| Print("Raw string data:\n", CharArrayToString(buffer));
|
|
|
| //---
|
| Print("Claves: ");
|
| string keys[];
|
| root.GetKeys(keys);
|
| ArrayPrint(keys, _Digits, " | ");
|
|
|
|
|
| //--- Calentar para JIT (3 iter)
|
| for(int i = 0; i < 7; i++)
|
| {
|
| root["trapo"].ToDouble(0.00);
|
| }
|
| Print("Root is JIT: ", root.ObjectIsJit());
|
|
|
|
|
| //---
|
| TSN::CDict dict;
|
| TSN::CInterpolate inter;
|
| dict.Set("valor", true);
|
| inter.m_dict = &dict;
|
|
|
| //---
|
| const string tres = root.AtObjKey(3);
|
| Print("Tercera key: ", tres, " index: ", root.KeyToPosition(tres));
|
|
|
| //---
|
| Print("Trapo val: ", root["trapo"].ToDouble(0.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, " | ");
|
|
|
| //---
|
| 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());
|
| Print("Root emoji no escape: ", root["comida"].ToStringNoEscape());
|
| Print("Root invalid: ", root["invalid"].ToString());
|
| Print("Has key comida: ", root.HasKey("comida"));
|
| Print("First value type obj: ", EnumToString(root.AtObj(0).GetType()));
|
| Print("Va value:", root["va"].ToInt(0));
|
|
|
| //---
|
| Print("Iteracion ----");
|
| CJsonIteratorObj it = root.BeginObj();
|
| while(it.IsValid())
|
| {
|
| Print("Element of root: ", it.Key());
|
| it.Next();
|
| }
|
| }
|
| //+------------------------------------------------------------------+
|