JsonParserByLeo/Src/JsonNode/JsonNodeMethods.mqh

324 lines
8.5 KiB
MQL5

2026-10-06 08:00:55 -05:00
//+------------------------------------------------------------------+
//| JsonUnescape.mqh |
//| Copyright 2026, Niquel Mendoza. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza."
#property link "https://www.mql5.com"
#property strict
2026-10-08 13:39:44 -05:00
#ifndef JSONPARSERBYLEO_SRC_JSONNODE_JSONNODEMETHODS_MQH
#define JSONPARSERBYLEO_SRC_JSONNODE_JSONNODEMETHODS_MQH
2026-10-06 08:00:55 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//--- incluyte al parser y unescape tables
2026-10-08 13:39:44 -05:00
#include "..\\JsonCore\\JsonParser.mqh"
2026-10-06 08:00:55 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-10-08 13:39:44 -05:00
//--- macro que se usa en cada iteracion de key
#define JSONPARSERBYLEO_KEY_WORK(KH) \
if(TSN_SBL_KEY_HAS_ISN_HASH(m_cinta[cur])) \
{ \
int i = TSN_SBL_CLS_STR_START(cur, int); \
if(!CBSFLUnescape::MakeHash<CJsonParser>(m_raw, i, m_len, m_cinta[cur + 1])) \
m_cinta[cur + 1] = KH + 1; \
else \
m_cinta[cur] |= TSN_SBL_KEY_FLAG_HASH; \
} \
//+------------------------------------------------------------------+
//| GetStr |
//+------------------------------------------------------------------+
#define JPBL_STR_KEY uchar
#define JPBL_STR_STR ushort
2026-10-06 08:00:55 -05:00
//+------------------------------------------------------------------+
//| Arrays helpers para evitar la resolcuino de ctx en todo mommento |
//+------------------------------------------------------------------+
template <typename T>
void CJsonParser::ToArray(int cur, T& out_arr[], const int end) const
{
//---
int k = 0;
switch(int(m_cinta[cur] & TSN_SBL_BIT_MASK_TYPE))
{
case JSON_VTYPE_UINTEGER:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
out_arr[k++] = T(m_cinta[cur + 1]);
2026-10-06 08:00:55 -05:00
cur += 2;
}
break;
}
case JSON_VTYPE_INTEGER:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
out_arr[k++] = T(long(m_cinta[cur + 1]));
2026-10-06 08:00:55 -05:00
cur += 2;
}
break;
}
case JSON_VTYPE_REAL:
{
while(cur < end)
{
TsnBitInterprete bit;
2026-10-08 13:39:44 -05:00
bit.ulong_value = m_cinta[cur + 1];
2026-10-06 08:00:55 -05:00
out_arr[k++] = T(bit.double_value);
cur += 2;
}
break;
}
case JSON_VTYPE_BOOLEAN:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
out_arr[k++] = T(TSN_SBL_CLS_BOOL(cur));
2026-10-06 08:00:55 -05:00
cur++;
}
break;
}
case JSON_VTYPE_STRING:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
if(GetStrAsStr<JPBL_STR_STR>(cur, s_str))
out_arr[k++] = T(s_str);
2026-10-06 08:00:55 -05:00
//---
cur++;
}
break;
}
default:
break;
}
}
//+------------------------------------------------------------------+
template <typename T>
bool CJsonParser::ExistInArray(int cur, const T val, const uint flags, const int end) const
{
while(cur < end)
{
//---
uint ct = uint(m_cinta[cur] & TSN_SBL_BIT_MASK_TYPE);
if(((1U << ct) & flags) == 0)
{
cur += GetStepPure(cur);
continue;
}
//---
switch(ct)
{
case JSON_VTYPE_UINTEGER:
{
2026-10-08 13:39:44 -05:00
if(m_cinta[cur + 1] == T(val))
2026-10-06 08:00:55 -05:00
return true;
break;
}
case JSON_VTYPE_INTEGER:
{
2026-10-08 13:39:44 -05:00
if((long)m_cinta[cur + 1] == T(val))
2026-10-06 08:00:55 -05:00
return true;
break;
}
case JSON_VTYPE_REAL:
{
2026-10-08 13:39:44 -05:00
bit.ulong_value = m_cinta[cur + 1];
2026-10-06 08:00:55 -05:00
if(bit.double_value == T(val))
return true;
break;
}
case JSON_VTYPE_BOOLEAN:
{
2026-10-08 13:39:44 -05:00
if(val == T(TSN_SBL_CLS_BOOL(cur)))
2026-10-06 08:00:55 -05:00
return true;
break;
}
case JSON_VTYPE_STRING:
{
2026-10-08 13:39:44 -05:00
if(GetStrAsStr<JPBL_STR_STR>(cur, s_str) && val == T(s_str))
2026-10-06 08:00:55 -05:00
return true;
break;
}
case JSON_VTYPE_NULL:
return true;
default:
return false;
}
//---
cur += GetStepPure(cur);
}
return false;
}
//+------------------------------------------------------------------+
template <typename T>
bool CJsonParser::ExistInArrayHomogeneo(int cur, const T val, const int end) const
{
switch(int(m_cinta[cur] & TSN_SBL_BIT_MASK_TYPE))
{
case TSN_SBL_BASE_TYPE_UINT:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
if(val == T(m_cinta[cur + 1]))
2026-10-06 08:00:55 -05:00
return true;
cur += 2;
}
}
case TSN_SBL_BASE_TYPE_INT:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
if(val == T(long(m_cinta[cur + 1])))
2026-10-06 08:00:55 -05:00
return true;
cur += 2;
}
return false;
}
case TSN_SBL_BASE_TYPE_FLT:
{
while(cur < end)
{
TsnBitInterprete bit;
2026-10-08 13:39:44 -05:00
bit.ulong_value = m_cinta[cur + 1];
2026-10-06 08:00:55 -05:00
if(val == T(bit.double_value))
return true;
cur += 2;
}
return false;
}
case TSN_SBL_BASE_TYPE_BOL:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
if(val == T(TSN_SBL_CLS_BOOL(cur)))
2026-10-06 08:00:55 -05:00
return true;
cur++;
}
return false;
}
case TSN_SBL_BASE_TYPE_STR:
{
while(cur < end)
{
2026-10-08 13:39:44 -05:00
if(GetStrAsStr<JPBL_STR_STR>(cur, s_str) && val == T(s_str))
2026-10-06 08:00:55 -05:00
return true;
cur++;
}
break;
}
default:
return false;
}
return false;
}
2026-10-08 13:39:44 -05:00
//+------------------------------------------------------------------+
//| Node at |
2026-10-06 08:00:55 -05:00
//+------------------------------------------------------------------+
__forceinline int CJsonParser::NodeAt(int cur, const int index) const
{
for(int k = 0; k < index; k++)
cur += GetStep(cur);
return cur;
}
2026-10-08 21:46:49 -05:00
2026-10-08 13:39:44 -05:00
//+------------------------------------------------------------------+
//| Find key pos |
//+------------------------------------------------------------------+
// CUR = APUNTA a la primerma key
int CJsonParser::NodeFindByHashFvn1A64(const ulong kh, int cur, const int end)
{
//---
int k = 0;
while(cur < end)
{
JSONPARSERBYLEO_KEY_WORK(kh)
if(kh == m_cinta[cur + 1])
return cur;
cur += TSN_SBL_KEYSLOT_SIZE; // salta KEY
cur += GetStep(cur);
k++;
}
return -1;
}
//+------------------------------------------------------------------+
int CJsonParser::NodeKeyToPositionSlow(const ulong kh, int cur, const int end)
{
//---
int k = 0;
while(cur < end)
{
JSONPARSERBYLEO_KEY_WORK(kh)
if(kh == m_cinta[cur + 1])
return k;
cur += TSN_SBL_KEYSLOT_SIZE; // salta KEY
cur += GetStep(cur);
k++;
}
return -1;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
__forceinline int CJsonParser::NodeObjAtSlow(int cur, const int index) const
{
for(int k = 0; k < index; k++)
{
cur += TSN_SBL_KEYSLOT_SIZE; // salta KEY
cur += GetStep(cur);
}
return cur;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CJsonParser::NodeGetKeys(int cur, string &keys[], const int end) const
{
//---
const int count = int((m_cinta[cur] >> TSN_SBL_BIT_START_NUM_EL) & TSN_SBL_BIT_MASK_NUM_EL);
ArrayResize(keys, count);
//---
int i = 0;
cur += TSN_SBL_SLOT_OBJ_SIZE;
//---
while(cur < end)
{
if(GetStrAsStr<JPBL_STR_KEY>(cur, s_str))
keys[i++] = s_str;
else
keys[i++] = NULL;
//---
cur += TSN_SBL_KEYSLOT_SIZE; // salta KEY
cur += GetStep(cur); // salta VAL
}
//---
return count;
}
2026-10-06 08:00:55 -05:00
}
2026-10-08 13:39:44 -05:00
#endif // JSONPARSERBYLEO_SRC_JSONNODE_JSONNODEMETHODS_MQH