JsonParserByLeo/Src/JsonBuilder/JsonStrBuilder.mqh

285 lines
8.8 KiB
MQL5

2026-09-19 22:46:26 -05:00
//+------------------------------------------------------------------+
2026-07-13 18:47:16 -05:00
//| JsonStrBuilder.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_DOM_JSONSTRBUILDER_MQH
#define JSONPARSERBYLEO_SRC_DOM_JSONSTRBUILDER_MQH
2026-07-13 18:47:16 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#include "BuilderDefs.mqh"
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
2026-09-19 22:46:26 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#define JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(str, needed) \
const int l = StringLen(str); \
2026-09-20 11:21:36 -05:00
const int ex = JSONESCAPE_CR_ENCODESTR(l) + (needed); \
2026-09-19 22:46:26 -05:00
JSONBUILDER_EXPAND_E(ex)
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
#define JSONBUILDERSTR_CHECK_PUT_ENCODE_STRU(arr, needed) \
if(count==WHOLE_ARRAY) \
count = ArraySize(arr); \
const int ex = JSONESCAPE_CR_ENCODESTR(count) + (needed); \
2026-09-19 22:46:26 -05:00
JSONBUILDER_EXPAND_E(ex)
2026-09-20 11:21:36 -05:00
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
#define JSONBUILDERSTR_GENERIC_VALS(READ, L) \
JSON_BUILDER_COMMA_F \
m_buf[m_pos++] = '\\'; \
m_buf[m_pos++] = '"'; \
CJsonEscape::EncodeStr(v, READ, L, m_buf, m_pos); \
m_buf[m_pos++] = '\\'; \
2026-09-20 11:21:36 -05:00
m_buf[m_pos++] = '"';
//+------------------------------------------------------------------+
#define JSONBUILDERSTR_GENERIC_VALS_WVER \
JSONBUILDER_EXPAND(3) \
JSON_BUILDER_COMMA_F \
m_buf[m_pos++] = '\\'; \
m_buf[m_pos++] = '"'; \
PutRaw(v); \
JSONBUILDER_EXPAND(2) \
m_buf[m_pos++] = '\\'; \
m_buf[m_pos++] = '"';
2026-09-19 22:46:26 -05:00
2026-07-13 18:47:16 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CJsonBuilderStr : public CJsonBuilderBase
{
public:
CJsonBuilderStr(int initial_cap = 1024): CJsonBuilderBase(initial_cap) {}
~CJsonBuilderStr() {}
//--- Key / Values
CJsonBuilderStr* Key(const string& k);
CJsonBuilderStr* KeyWV(const string& k);
// String- Con validacion
CJsonBuilderStr* ValS(const string& v);
2026-10-13 21:28:39 -05:00
CJsonBuilderStr* ValU(const uchar &v[], int read_i = 0, int count = WHOLE_ARRAY);
2026-07-13 18:47:16 -05:00
CJsonBuilderStr* ValSNoRef(const string v);
// String- Sin validacion (witohut validation WV)
CJsonBuilderStr* ValSWV(const string& v);
2026-09-20 11:21:36 -05:00
CJsonBuilderStr* ValUWV(const uchar& v[]);
2026-07-13 18:47:16 -05:00
CJsonBuilderStr* ValSNoRefWV(const string v);
2026-09-20 11:21:36 -05:00
2026-10-13 21:28:39 -05:00
// Valor
using CJsonBuilderBase::Val;
bool Val(CJsonNode& node);
2026-09-20 11:21:36 -05:00
// Valid json
// CJsonBuilderStr* ValidJson(const uchar& v[], int count = WHOLE_ARRAY);
// CJsonBuilderStr* ValidJson(const string& fragment);
// CJsonBuilderStr* ValidJsonNoRef(const string fragment);
2026-07-13 18:47:16 -05:00
};
2026-07-14 10:50:16 -05:00
2026-10-13 21:28:39 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CJsonBuilderStr::Val(CJsonNode& node)
{
switch(node.GetType())
{
case JSON_VTYPE_UINTEGER:
{
ulong v;
if(!node.ToUIntSafe(v))
return false;
Val(v);
break;
}
case JSON_VTYPE_INTEGER:
{
long v;
if(!node.ToIntSafe(v))
return false;
Val(v);
break;
}
case JSON_VTYPE_REAL:
{
const double v = node.ToDouble(double("inf"));
if(v == double("inf"))
return false;
Val(v);
break;
}
case JSON_VTYPE_BOOLEAN:
{
bool v;
if(!node.ToBoolSafe(v))
return false;
Val(v);
break;
}
case JSON_VTYPE_STRING:
{
const int start = node.StrStart();
const int len = node.StrLenRaw();
ValU(node.m_ctx.m_raw, start, len);
break;
}
case JSON_VTYPE_NULL:
{
Null();
break;
}
default:
return false;
}
return true;
}
2026-07-13 18:47:16 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-09-20 11:21:36 -05:00
CJsonBuilderStr* CJsonBuilderStr::Key(const string& k)
2026-07-13 18:47:16 -05:00
{
//---
2026-09-21 12:52:37 -05:00
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(k, 6)
2026-07-13 18:47:16 -05:00
//---
2026-09-20 11:21:36 -05:00
JSON_BUILDER_COMMA_F
2026-07-13 18:47:16 -05:00
m_buf[m_pos++] = '\\';
m_buf[m_pos++] = '"';
2026-10-13 21:28:39 -05:00
CJsonEscape::EncodeStr(k, 0, l, m_buf, m_pos);
2026-07-13 18:47:16 -05:00
m_buf[m_pos++] = '\\';
m_buf[m_pos++] = '"';
2026-09-19 22:46:26 -05:00
m_buf[m_pos++] = ':';
2026-07-13 18:47:16 -05:00
//---
m_stack_first[m_sp] = true;
return &this;
}
2026-09-19 22:46:26 -05:00
2026-07-13 18:47:16 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
CJsonBuilderStr* CJsonBuilderStr::ValU(const uchar &v[], int read_i, int count)
2026-07-13 18:47:16 -05:00
{
2026-09-20 11:21:36 -05:00
JSONBUILDERSTR_CHECK_PUT_ENCODE_STRU(v, 5)
2026-10-13 21:28:39 -05:00
JSONBUILDERSTR_GENERIC_VALS(read_i, count)
2026-07-13 18:47:16 -05:00
return &this;
}
//+------------------------------------------------------------------+
CJsonBuilderStr* CJsonBuilderStr::ValS(const string& v)
{
2026-09-20 11:21:36 -05:00
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(v, 5)
2026-10-13 21:28:39 -05:00
JSONBUILDERSTR_GENERIC_VALS(0, l)
2026-07-13 18:47:16 -05:00
return &this;
}
//+------------------------------------------------------------------+
CJsonBuilderStr* CJsonBuilderStr::ValSNoRef(const string v)
{
2026-09-20 11:21:36 -05:00
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(v, 5)
2026-10-13 21:28:39 -05:00
JSONBUILDERSTR_GENERIC_VALS(0, l)
2026-09-19 22:46:26 -05:00
return &this;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-09-26 11:38:15 -05:00
// WV en key solo acepta ASCCI puro no caracteres de control (no espca) menos emojis (tipico en keys harcodeadas)
// Si reuiqer full use la version completa
2026-09-19 22:46:26 -05:00
CJsonBuilderStr* CJsonBuilderStr::KeyWV(const string &k)
{
//---
2026-09-26 11:38:15 -05:00
const int l = StringLen(k);
const int req = l + 6;
JSONBUILDER_EXPAND(req)
//---
2026-09-19 22:46:26 -05:00
JSON_BUILDER_COMMA_F
m_buf[m_pos++] = '\\';
m_buf[m_pos++] = '"';
//---
2026-09-26 11:38:15 -05:00
for(int i = 0; i < l; i++)
m_buf[m_pos++] = (uchar)k[i];
2026-09-19 22:46:26 -05:00
//---
m_buf[m_pos++] = '\\';
m_buf[m_pos++] = '"';
m_buf[m_pos++] = ':';
//---
m_stack_first[m_sp] = true;
2026-07-13 18:47:16 -05:00
return &this;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CJsonBuilderStr* CJsonBuilderStr::ValSWV(const string& v)
{
2026-09-20 11:21:36 -05:00
JSONBUILDERSTR_GENERIC_VALS_WVER
2026-07-13 18:47:16 -05:00
return &this;
}
//+------------------------------------------------------------------+
CJsonBuilderStr* CJsonBuilderStr::ValSNoRefWV(const string v)
{
2026-09-20 11:21:36 -05:00
JSONBUILDERSTR_GENERIC_VALS_WVER
2026-07-13 18:47:16 -05:00
return &this;
}
//+------------------------------------------------------------------+
2026-09-20 11:21:36 -05:00
CJsonBuilderStr* CJsonBuilderStr::ValUWV(const uchar &v[])
2026-07-13 18:47:16 -05:00
{
2026-09-20 11:21:36 -05:00
JSONBUILDERSTR_GENERIC_VALS_WVER
2026-07-13 18:47:16 -05:00
return &this;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
/*CJsonBuilderStr* CJsonBuilderStr::ValidJson(const uchar& v[], int count = WHOLE_ARRAY)
{
JSON_BUILDER_COMMA
PutRaw(v, count);
return &this;
}
//+------------------------------------------------------------------+
CJsonBuilderStr* CJsonBuilderStr::ValidJson(const string& fragment)
{
JSON_BUILDER_COMMA
PutRaw(fragment);
return &this;
}
//+------------------------------------------------------------------+
CJsonBuilderStr* CJsonBuilderStr::ValidJsonNoRef(const string fragment)
{
JSON_BUILDER_COMMA
PutRaw(fragment);
return &this;
}
*/
//--- end namespace
}
2026-10-08 13:39:44 -05:00
#endif // JSONPARSERBYLEO_SRC_DOM_JSONSTRBUILDER_MQH
2026-07-13 18:47:16 -05:00
//+------------------------------------------------------------------+