- MQL5 86.8%
- MQL4 13.2%
| Src | ||
| Test | ||
| dependencies.json | ||
| JsonParserByLeo.mqproj | ||
| LICENSE | ||
| README.md | ||
A high-performance, memory-free JSON parser for MQL5, based on a flat tape architecture.
Single-pass iterative state machine (as is, without extra loops, "pure" o(n)): no recursion, no dynamic memory fragmentation, no overhead from function calls.
Main Features
- Tape-based zero-alloc model: the entire JSON is parsed into a single contiguous
long[]array - Single flat loop: one
switchover a token enum, no helper function calls during parsing, strictly O(n) - FNV-1a key hashing: key lookup in objects without string comparison
- Handle-based navigation:
CJsonNodeis a lightweight struct (pointer + two ints), copying is free - In-place mutation:
SetInt,SetDbl,SetBoolmodify the tape directly without re-parsing - Iterator support:
CJsonIteratorObjandCJsonIteratorArrayfor clean traversal
Parse and navigate
#include "Src\\JsonNode.mqh"
TSN::CJsonParser parser;
parser.Assing("{\"symbol\":\"EURUSD\",\"bid\":1.2345,\"ask\":1.2347,\"active\":true}");
parser.Parse();
TSN::CJsonNode root = parser.GetRoot();
string symbol = root["symbol"].ToString();
double bid = root["bid"].ToDouble(0.0);
bool active = root["active"].ToBool(false);
Iterate an object
TSN::CJsonIteratorObj it = root.BeginObj();
while(it.IsValid())
{
PrintFormat("%s : %s", it.Key(), it.Val().ToString());
it.Next();
}
Iterate an array
TSN::CJsonNode arr = root["prices"];
TSN::CJsonIteratorArray it = arr.BeginArr();
while(it.IsValid())
{
Print(it.Val().ToDouble(0.0));
it.Next();
}
Parse from file
TSN::CJsonParser parser;
parser.AssingFile("data.json", false);
parser.Parse();
Performance
Benchmark: twitter.json (616 KB), 1000 iterations, MetaTrader 5 x64 build 5836 started for MetaQuotes Ltd. (Laptop Lenovo (approximately from 2017), Terminal Windows 10 build 19045, 8 x Intel Core i5-8250U @ 1.60GHz, AVX2, 3 / 7 Gb memory, 34 / 222 Gb disk SSD, UAC, GMT-5)
| Parser | Time (ms) |
|---|---|
| JsonParserByLeo | 1094 |
| fast_json v3.4 (One copy of array) | 1265 |
The gap comes from architecture, not micro-optimizations: one flat loop vs. a state machine with separate helper calls and per-iteration reserve checks.
Repository Structure
JsonParserByLeo/
├── Src/ # Full code (Defines, Node, Parser)
└── Test/ # Test and Benchmarks (vs)
License
Read Full License
By downloading or using this repository, you accept the license terms.
Requirements
See dependencies.json for the full list.
Installation
cd "C:\Users\YOUR_USER\AppData\Roaming\MetaQuotes\Terminal\YOUR_ID\MQL5\Shared Projects"
tsndep install "https://forge.mql5.io/nique_372/JsonParserByLeo.git"
Requires the tsndep package, available on PyPI. It automatically downloads and installs all declared dependencies.
Quick Start
1. Include the library:
#include "..\\JsonParserByLeo\\Src\\JsonNode.mqh"
2. Parse and access:
TSN::CJsonParser parser;
parser.Assing(my_json_string);
if(parser.Parse())
{
TSN::CJsonNode root = parser.GetRoot();
double price = root["price"].ToDouble(0.0);
}
3. Re-parse without re-copying (benchmark pattern):
parser.Assing(raw_string); // copy once
for(int i = 0; i < 1000; i++)
parser.Parse(); // re-parse in-place
Contact
- Platform: MQL5 Community
- Profile: https://www.mql5.com/es/users/nique_372
- Articles: https://www.mql5.com/es/users/nique_372/publications