Libraries: MQL4/5-JsonLib

 

MQL4/5-JsonLib:

A JSON library that supports MQL4/MQL5

MQL4/5-JsonLib

Author: hini

 

Forum on trading, automated trading systems and testing trading strategies

Features of the mql5 language, subtleties and tricks

fxsaber, 2025.11.19 17:57

Who knows JSON, please share a ready solution with the functionality of these two buttons.

bool SymbolExport( const string Symb, const string FileName ); // Export symbol's json-data.
bool SymbolImport( const string Symb, const string FileName ); // Import symbol's json-data.

Should create and read exactly the same json as MT5 does through the labelled buttons on the screenshot.


Need for a public torrent with backtest data. Thanks.

 
There are many JSON libraries on this resource. Which one is the fastest?
 
      case JSON_STRING:
         current_node.m_str = GetStringFromSlice(current_token.start, current_token.length);
         break;
      case JSON_DOUBLE:
      {
         string num_str = StringSubstr(m_text, current_token.start, current_token.length);
         if(StringFind(num_str, ".") < 0 && StringFind(num_str, "e") < 0 && StringFind(num_str, "E") < 0)
         {
            current_node.m_type = JSON_INT;
            current_node.m_int = StringToInteger(num_str);
         }
         else
         {
            current_node.m_double = StringToDouble(num_str);
         }
         current_node.m_num_str = m_doc.m_arena_ptr.AllocateString(num_str);
      }
      break;
This condition can be written much faster.
 
fxsaber # :
There are many JSON libraries on this resource. Which one is the fastest?

I haven't compared the speeds of various JSON libraries; the main reason I use this library is that it supports more JSON standards and offers more features.

 
fxsaber # :

MT5 exports JSON in UTF16 format, while this library exports files in UTF8 format. The order may differ, but the content is the same.

if(SymbolImport("XAUUSDm", "XAUUSDm_config.json"))

The JSON file path should be placed in `MQL5/Files/`.

Files:
 
fxsaber # :
This condition can be written much faster.
how?
 
fxsaber #:
This condition can be written much faster.
   void Add(CJsonValue* v)
   {
      if(m_type!=JSON_ARRAY || CheckPointer(v)==POINTER_INVALID) return;
      int s = ArraySize(m_arr);
      if(ArrayResize(m_arr, s + 1) > 0) m_arr[s] = v;
   }
 
hini #:

the main reason I use this library is that it supports more JSON standards and offers more features.

bool JsonStreamParser::ParseValue(JsonError &e)
{
   if(m_depth>=m_options.max_depth) return SetError(e,"Max depth exceeded");
   SkipWhitespaceAndComments();
   if(m_reader.IsEOF()) return SetError(e,"Unexpected end of input, expected a value");
   ushort c=m_reader.Peek();
   switch(c)
   {
   case '{':
      m_depth++;
      return ParseObject(e);
   case '[':
      m_depth++;
      return ParseArray(e);
   case '"':
   {
      string s;
      if(!ParseString(s, e)) return false;
      return m_handler.OnString(s);
   }
   case 't':
   case 'f':
   case 'n':
      return ParseKeyword(e);
   default:
      if(c=='-' || (c>='0' && c<='9')) return ParseNumber(e);
   }
   string error_msg = "Invalid character '" + ShortToString(c) + "' starting a value";
   return SetError(e, error_msg);
}


These valid entries do not fall under this condition.

.123
+123
 
fxsaber # :


These valid entries do not fall under this condition.

These numbers do not belong to the JSON standard.
 
The library's architecture is written in such a way that its parsing performance depends exponentially on the size of the source text. For small texts, this is unnoticeable.

Apparently, efficient implementation wasn't the goal.