285 lines
8.8 KiB
MQL5
285 lines
8.8 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| JsonStrBuilder.mqh |
| |
//| Copyright 2026, Niquel Mendoza. |
| |
//| https://www.mql5.com/ |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2026, Niquel Mendoza."
| |
#property link "https://www.mql5.com/"
| |
#property strict
| |
| |
#ifndef JSONPARSERBYLEO_SRC_DOM_JSONSTRBUILDER_MQH
| |
#define JSONPARSERBYLEO_SRC_DOM_JSONSTRBUILDER_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "BuilderDefs.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#define JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(str, needed) \
| |
const int l = StringLen(str); \
| |
const int ex = JSONESCAPE_CR_ENCODESTR(l) + (needed); \
| |
JSONBUILDER_EXPAND_E(ex)
| |
| |
//+------------------------------------------------------------------+
| |
#define JSONBUILDERSTR_CHECK_PUT_ENCODE_STRU(arr, needed) \
| |
if(count==WHOLE_ARRAY) \
| |
count = ArraySize(arr); \
| |
const int ex = JSONESCAPE_CR_ENCODESTR(count) + (needed); \
| |
JSONBUILDER_EXPAND_E(ex)
| |
| |
//+------------------------------------------------------------------+
| |
#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++] = '\\'; \
| |
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++] = '"';
| |
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
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);
| |
CJsonBuilderStr* ValU(const uchar &v[], int read_i = 0, int count = WHOLE_ARRAY);
| |
CJsonBuilderStr* ValSNoRef(const string v);
| |
| |
// String- Sin validacion (witohut validation WV)
| |
CJsonBuilderStr* ValSWV(const string& v);
| |
CJsonBuilderStr* ValUWV(const uchar& v[]);
| |
CJsonBuilderStr* ValSNoRefWV(const string v);
| |
| |
// Valor
| |
using CJsonBuilderBase::Val;
| |
bool Val(CJsonNode& node);
| |
| |
// Valid json
| |
// CJsonBuilderStr* ValidJson(const uchar& v[], int count = WHOLE_ARRAY);
| |
// CJsonBuilderStr* ValidJson(const string& fragment);
| |
// CJsonBuilderStr* ValidJsonNoRef(const string fragment);
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
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;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::Key(const string& k)
| |
{
| |
//---
| |
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(k, 6)
| |
| |
//---
| |
JSON_BUILDER_COMMA_F
| |
m_buf[m_pos++] = '\\';
| |
m_buf[m_pos++] = '"';
| |
CJsonEscape::EncodeStr(k, 0, l, m_buf, m_pos);
| |
m_buf[m_pos++] = '\\';
| |
m_buf[m_pos++] = '"';
| |
m_buf[m_pos++] = ':';
| |
| |
//---
| |
m_stack_first[m_sp] = true;
| |
return &this;
| |
}
| |
| |
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValU(const uchar &v[], int read_i, int count)
| |
{
| |
JSONBUILDERSTR_CHECK_PUT_ENCODE_STRU(v, 5)
| |
JSONBUILDERSTR_GENERIC_VALS(read_i, count)
| |
return &this;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValS(const string& v)
| |
{
| |
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(v, 5)
| |
JSONBUILDERSTR_GENERIC_VALS(0, l)
| |
return &this;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValSNoRef(const string v)
| |
{
| |
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(v, 5)
| |
JSONBUILDERSTR_GENERIC_VALS(0, l)
| |
return &this;
| |
}
| |
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
// 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
| |
CJsonBuilderStr* CJsonBuilderStr::KeyWV(const string &k)
| |
{
| |
//---
| |
const int l = StringLen(k);
| |
const int req = l + 6;
| |
JSONBUILDER_EXPAND(req)
| |
| |
//---
| |
JSON_BUILDER_COMMA_F
| |
m_buf[m_pos++] = '\\';
| |
m_buf[m_pos++] = '"';
| |
| |
//---
| |
for(int i = 0; i < l; i++)
| |
m_buf[m_pos++] = (uchar)k[i];
| |
| |
//---
| |
m_buf[m_pos++] = '\\';
| |
m_buf[m_pos++] = '"';
| |
m_buf[m_pos++] = ':';
| |
| |
//---
| |
m_stack_first[m_sp] = true;
| |
return &this;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValSWV(const string& v)
| |
{
| |
JSONBUILDERSTR_GENERIC_VALS_WVER
| |
return &this;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValSNoRefWV(const string v)
| |
{
| |
JSONBUILDERSTR_GENERIC_VALS_WVER
| |
return &this;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValUWV(const uchar &v[])
| |
{
| |
JSONBUILDERSTR_GENERIC_VALS_WVER
| |
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
| |
}
| |
#endif // JSONPARSERBYLEO_SRC_DOM_JSONSTRBUILDER_MQH
| |
//+------------------------------------------------------------------+
|