Skip to content

Fix invalid JSON from unescaped backslashes in MQL5 string serialization#294

Open
biohazardxxx wants to merge 11 commits intovdemydiuk:devfrom
biohazardxxx:fix/mql5-json-string-escaping
Open

Fix invalid JSON from unescaped backslashes in MQL5 string serialization#294
biohazardxxx wants to merge 11 commits intovdemydiuk:devfrom
biohazardxxx:fix/mql5-json-string-escaping

Conversation

@biohazardxxx
Copy link

Summary

  • JSONString.toString() in mq5/json.mqh emitted raw strings without escaping special characters. When MQL5 functions returned Windows paths (e.g. TerminalInfoString), unescaped backslashes produced invalid JSON, causing JsonReaderException: Bad JSON escape sequence on the C# client side.
  • Added proper JSON escaping for backslash, double-quote, newline, carriage return, and tab characters during serialization.
  • The MQL4 json.mqh has the same latent bug but is worked around by a manual StringReplace in Execute_TerminalInfoString; this fix addresses it at the serialization layer for MQL5.

Test plan

  • Compile MtApi5.mq5 with MetaEditor to verify MQL5 compilation succeeds
  • Connect a .NET client and call TerminalInfoString with path-returning properties (e.g. TERMINAL_PATH, TERMINAL_DATA_PATH) — verify valid JSON is returned and parsed without exceptions
  • Verify strings containing quotes, newlines, and tabs are also correctly escaped in JSON output

🤖 Generated with Claude Code

vdemydiuk and others added 11 commits July 28, 2025 23:29
JSONString.toString() in mq5/json.mqh emitted raw strings without
escaping special characters. When MQL5 functions returned Windows paths
(e.g. TerminalInfoString), backslashes produced invalid JSON causing
JsonReaderException on the C# client side.

Escape backslash, double-quote, newline, carriage return, and tab
characters during serialization, matching the JSON spec.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@tudstudent
Copy link

Fix looks correct — we verified it against a live MT5 terminal (TerminalInfoString with TERMINAL_PATH now returns valid JSON with escaped backslashes).

Note: mq4/json.mqh has the same toString() bug but is worked around by a manual StringReplace in Execute_TerminalInfoString. This PR fixes it at the serialization layer for MQL5, which is the right approach. Consider applying the same fix to mq4/json.mqh for consistency — any new MQL4 function returning strings with backslashes/quotes/newlines would hit the same issue.

@tudstudent
Copy link

For reference: our PR #297 includes this JSON escaping fix for MQL5 plus the equivalent fix needed in MQL4's json.mqh, along with 6 other bug fixes. If you'd prefer a single merge that covers everything, #297 is the combined option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants