DinexSpace JSON Writer Library
- librerie
- Versione: 1.0
DinexSpace JSON Writer Library is a library for MetaTrader 5 (MT5) that builds a valid JSON text piece by piece, for your indicator or Expert Advisor. You begin an object or an array, add keys and values, end it and read the text. The commas, colons, quotes and the escaping of special characters are done for you, and a call that would make the text invalid is refused with a code.
How it works
- Call DxJwCreate with the number of levels, whether to write letters above the basic set as codes, and the indent (0 for a compact text). You get a handle.
- Call DxJwBeginObject or DxJwBeginArray, then DxJwKeyString, DxJwKeyNumber, DxJwKeyInt, DxJwKeyBool, DxJwKeyNull or DxJwKey followed by a value call, and close with DxJwEndObject or DxJwEndArray.
- When one value is complete, DxJwComplete is true and DxJwText gives the text. Before that DxJwText returns an empty string with a status code.
- DxJwEscape escapes a single string.
What you get
- Complete text: objects, arrays, strings, numbers, true, false and null, with the commas, colons and quotes put in for you.
- Escaping done: quotes, backslashes, control characters, non-Latin letters and emoji are written the way the JSON rules ask.
- Compact or indented: no spaces at all, or one element on each line with the indent you choose; letters above the basic set can be written as \uXXXX codes.
- Misuse is refused: a value without a key in an object, a key outside an object, a closing bracket that does not match, a second top-level value, too many levels or a number that is not finite returns a code and a plain-English text, and the text written so far stays valid.
- Checked: the example EA writes every bar as a JSON object and compares the text with one put together by hand; 2000 random documents in four layouts are read back by a separate Python program with json.loads.
- Light: each program has its own writers. No DLLs, no web requests, no files. The library only builds a string.
How to use it
- Copy DinexJsonWriter.ex5 to MQL5\Libraries.
- Add the import block with the functions you use (shown in the images and in the example EA).
- Open a writer, write the members, read the text.
Rules of the text (RFC 8259)
- String: between double quotes. Escaped: a double quote and a backslash, the control characters 0x00 to 0x1F (as \b \f \n \r \t or \u00XX). The slash is not escaped. A surrogate code unit with no partner is written as \uXXXX.
- Integer: the whole number. Number: a double with the number of decimals you give, 0 to 16; not-a-number and infinity are refused.
- Layout: indent 0 has no spaces; indent n has one element on each line, n spaces for each level and a space after each colon; an empty object or array stays {} or [].
Example EA (included in the description images)
On every new bar the example writes the last closed bar as a JSON object, in compact form and with an indent of two spaces, and compares the text character by character with a text put together by hand. It also runs results worked out by hand, the error codes, and writes 200 random documents to a file for a separate check. It prints the number of checks and the number of failed checks.
Notes
- The library only builds text. It does not read JSON, reads no market data, gives no entries or exits and does not predict any move.
- Numbers are written with the number of decimals you give, not with the shortest form. Duplicate keys are not checked.
- Up to 256 writers, 128 levels.
Requirements
- MetaTrader 5, any symbol and timeframe.
