Отслеживать
1
0
Ответвление
У вас уже есть ответвление JsonParserByLeo
0
ответвлён от nique_372/JsonParserByLeo
JsonParserByLeo/Src/JsonBuilder/JsonBuilder.mqh

286 строки
9,3 КиБ
MQL5
ИсходныйПостоянная ссылкаОбычный видИстория

2026-06-03 14:16:03 -05:00
//+------------------------------------------------------------------+
//| JsonBuilder.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_JSONBUILDER_MQH
#define JSONPARSERBYLEO_SRC_DOM_JSONBUILDER_MQH
2026-06-03 14:16:03 -05:00
//+------------------------------------------------------------------+
2026-06-30 08:24:44 -05:00
//| |
2026-06-03 14:16:03 -05:00
//+------------------------------------------------------------------+
2026-07-13 18:47:16 -05:00
#include "BuilderDefs.mqh"
2026-06-03 14:16:03 -05:00
2026-06-30 08:24:44 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
2026-09-19 22:46:26 -05:00
//+------------------------------------------------------------------+
2026-09-20 11:21:36 -05:00
//| Macros |
2026-09-19 22:46:26 -05:00
//+------------------------------------------------------------------+
#define JSONBUILDER_CHECK_PUT_ENCODE_STR(str, needed) \
const int l = StringLen(str); \
2026-09-20 11:21:36 -05:00
const int ex = JSONESCAPE_CR_ENCODE(l) + (needed); \
2026-09-19 22:46:26 -05:00
JSONBUILDER_EXPAND_E(ex)
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
#define JSONBUILDER_CHECK_PUT_ENCODE_STRU(arr, needed) \
if(count==WHOLE_ARRAY) \
count= ArraySize(arr); \
const int ex = JSONESCAPE_CR_ENCODE(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 JSONBUILDER_GENERIC_VALS(READ, L) \
JSON_BUILDER_COMMA_F \
m_buf[m_pos++] = '"'; \
CJsonEscape::Encode(v, READ, L, m_buf, m_pos); \
m_buf[m_pos++] = '"'; \
2026-09-20 11:21:36 -05:00
//+------------------------------------------------------------------+
2026-09-25 22:38:05 -05:00
#define JSONBUILDER_GENERIC_VALSTR_WVER \
JSONBUILDER_EXPAND_E(2) \
JSON_BUILDER_COMMA_F \
m_buf[m_pos++] = '"'; \
PutRaw(v); \
JSONBUILDER_EXPAND_E(1) \
2026-09-20 11:21:36 -05:00
m_buf[m_pos++] = '"';
2026-09-19 22:46:26 -05:00
2026-06-03 14:16:03 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-07-13 18:47:16 -05:00
class CJsonBuilder : public CJsonBuilderBase
2026-06-03 14:16:03 -05:00
{
public:
2026-07-13 18:47:16 -05:00
CJsonBuilder(int initial_cap = 1024) : CJsonBuilderBase(initial_cap) {}
2026-06-03 14:16:03 -05:00
~CJsonBuilder() {}
//--- Key / Values
CJsonBuilder* Key(const string& k);
2026-07-10 07:17:28 -05:00
CJsonBuilder* KeyWV(const string& k);
// String- Con validacion
CJsonBuilder* ValS(const string& v);
2026-10-13 21:28:39 -05:00
CJsonBuilder* ValU(const uchar& v[], int read_i = 0, int count = WHOLE_ARRAY);
2026-07-10 07:17:28 -05:00
CJsonBuilder* ValSNoRef(const string v);
// String- Sin validacion (witohut validation WV)
CJsonBuilder* ValSWV(const string& v);
2026-09-25 22:38:05 -05:00
CJsonBuilder* ValUWV(const uchar& v[], int count = WHOLE_ARRAY); // buffer fijo sobredimensionado (ej. CStringFixed): solo copia count bytes
2026-07-10 07:17:28 -05:00
CJsonBuilder* ValSNoRefWV(const string v);
2026-06-03 14:16:03 -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
CJsonBuilder* ValidJson(const uchar& v[], int count = WHOLE_ARRAY);
CJsonBuilder* ValidJson(const string& fragment);
CJsonBuilder* ValidJsonNoRef(const string fragment);
};
2026-06-03 14:16:03 -05:00
2026-10-13 21:28:39 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CJsonBuilder::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-06-03 14:16:03 -05:00
//+------------------------------------------------------------------+
CJsonBuilder* CJsonBuilder::Key(const string& k)
{
2026-09-19 22:46:26 -05:00
//---
2026-09-20 11:21:36 -05:00
JSONBUILDER_CHECK_PUT_ENCODE_STR(k, 4)
2026-06-30 08:24:44 -05:00
//---
2026-09-19 22:46:26 -05:00
JSON_BUILDER_COMMA_F
2026-09-20 11:21:36 -05:00
m_buf[m_pos++] = '"';
2026-10-13 21:28:39 -05:00
CJsonEscape::Encode(k, 0, l, m_buf, m_pos);
2026-09-20 11:21:36 -05:00
m_buf[m_pos++] = '"';
2026-09-19 22:46:26 -05:00
m_buf[m_pos++] = ':';
//---
m_stack_first[m_sp] = true;
return &this;
}
//+------------------------------------------------------------------+
//| |
2026-06-30 08:24:44 -05:00
//+------------------------------------------------------------------+
2026-10-13 21:28:39 -05:00
CJsonBuilder* CJsonBuilder::ValU(const uchar &v[], int read_i, int count)
2026-06-30 08:24:44 -05:00
{
2026-09-20 11:21:36 -05:00
JSONBUILDER_CHECK_PUT_ENCODE_STRU(v, 3)
2026-10-13 21:28:39 -05:00
JSONBUILDER_GENERIC_VALS(read_i, count)
2026-06-30 08:24:44 -05:00
return &this;
2026-06-03 14:16:03 -05:00
}
//+------------------------------------------------------------------+
CJsonBuilder* CJsonBuilder::ValS(const string& v)
{
2026-09-20 11:21:36 -05:00
JSONBUILDER_CHECK_PUT_ENCODE_STR(v, 3)
2026-10-13 21:28:39 -05:00
JSONBUILDER_GENERIC_VALS(0, l)
2026-06-03 14:16:03 -05:00
return &this;
}
2026-06-03 22:08:03 -05:00
//+------------------------------------------------------------------+
CJsonBuilder* CJsonBuilder::ValSNoRef(const string v)
{
2026-09-20 11:21:36 -05:00
JSONBUILDER_CHECK_PUT_ENCODE_STR(v, 3)
2026-10-13 21:28:39 -05:00
JSONBUILDER_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
CJsonBuilder* CJsonBuilder::KeyWV(const string &k)
{
//---
2026-09-26 11:38:15 -05:00
const int l = StringLen(k);
const int req = l + 4;
JSONBUILDER_EXPAND_E(req)
//---
2026-09-19 22:46:26 -05:00
JSON_BUILDER_COMMA_F
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_stack_first[m_sp] = true;
2026-06-03 22:08:03 -05:00
return &this;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-09-26 11:38:15 -05:00
// WV aqui implcia no escape pero UTF si se soporta (en string en uchar NO)
CJsonBuilder* CJsonBuilder::ValSWV(const string& v)
{
2026-09-25 22:38:05 -05:00
JSONBUILDER_GENERIC_VALSTR_WVER
return &this;
}
//+------------------------------------------------------------------+
CJsonBuilder* CJsonBuilder::ValSNoRefWV(const string v)
{
2026-09-25 22:38:05 -05:00
JSONBUILDER_GENERIC_VALSTR_WVER
return &this;
}
//+------------------------------------------------------------------+
2026-09-25 22:38:05 -05:00
CJsonBuilder* CJsonBuilder::ValUWV(const uchar &v[], int count)
{
2026-09-25 22:38:05 -05:00
//---
JSONBUILDER_EXPAND_E(2)
JSON_BUILDER_COMMA_F
m_buf[m_pos++] = '"';
PutRaw(v, count);
JSONBUILDER_EXPAND_E(1)
m_buf[m_pos++] = '"';
return &this;
}
2026-06-03 18:53:05 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
CJsonBuilder* CJsonBuilder::ValidJson(const uchar& v[], int count = WHOLE_ARRAY)
2026-06-03 18:53:05 -05:00
{
JSON_BUILDER_COMMA
PutRaw(v, count);
2026-06-03 18:53:05 -05:00
return &this;
}
2026-06-03 14:16:03 -05:00
//+------------------------------------------------------------------+
CJsonBuilder* CJsonBuilder::ValidJson(const string& fragment)
{
JSON_BUILDER_COMMA
PutRaw(fragment);
return &this;
}
2026-06-03 16:57:42 -05:00
//+------------------------------------------------------------------+
CJsonBuilder* CJsonBuilder::ValidJsonNoRef(const string fragment)
{
JSON_BUILDER_COMMA
PutRaw(fragment);
return &this;
}
//---
2026-09-23 18:12:17 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
// Nota deje esto pero tengan cuidado con esto... dado que no hay garantias de uso lo puse mas que todo para evitar tener que instancia un segundo builder
// ahora el uso adecuado es digmoas en una funcion usarlo todo de golpe (sin llamar a funciones intermedias o aseguramente que esa funcion intermedia no lo llame) y listo
// osea uso y desecho... algo asi... no tatno como "si fuera mio" si requieres cache o algo asi.. lo mejors es tener miembro CJsonBuilder...
2026-06-03 16:57:42 -05:00
CJsonBuilder g_json_builder;
2026-06-03 14:16:03 -05:00
} // namespace TSN
//+------------------------------------------------------------------+
2026-10-08 13:39:44 -05:00
#endif // JSONPARSERBYLEO_SRC_DOM_JSONBUILDER_MQH