223 lines
7.3 KiB
MQL5
223 lines
7.3 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_JSONSTRBUILDER_MQH
| |
#define JSONPARSERBYLEO_SRC_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) \
| |
const int l = ArraySize(arr); \
| |
const int ex = JSONESCAPE_CR_ENCODESTR(l) + (needed); \
| |
JSONBUILDER_EXPAND_E(ex)
| |
| |
//+------------------------------------------------------------------+
| |
#define JSONBUILDERSTR_GENERIC_VALS \
| |
JSON_BUILDER_COMMA_F \
| |
m_buf[m_pos++] = '\\'; \
| |
m_buf[m_pos++] = '"'; \
| |
CJsonEscape::EncodeStr(v, 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[]);
| |
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);
| |
| |
// Valid json
| |
// CJsonBuilderStr* ValidJson(const uchar& v[], int count = WHOLE_ARRAY);
| |
// CJsonBuilderStr* ValidJson(const string& fragment);
| |
// CJsonBuilderStr* ValidJsonNoRef(const string fragment);
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
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, 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[])
| |
{
| |
JSONBUILDERSTR_CHECK_PUT_ENCODE_STRU(v, 5)
| |
JSONBUILDERSTR_GENERIC_VALS
| |
return &this;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValS(const string& v)
| |
{
| |
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(v, 5)
| |
JSONBUILDERSTR_GENERIC_VALS
| |
return &this;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
CJsonBuilderStr* CJsonBuilderStr::ValSNoRef(const string v)
| |
{
| |
JSONBUILDERSTR_CHECK_PUT_ENCODE_STR(v, 5)
| |
JSONBUILDERSTR_GENERIC_VALS
| |
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_JSONSTRBUILDER_MQH
| |
//+------------------------------------------------------------------+
|