Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string escaping to JSON formatter #2875

Merged
merged 2 commits into from
Aug 16, 2024

Conversation

mikee47
Copy link
Contributor

@mikee47 mikee47 commented Aug 15, 2024

JSON does not support multiline text so control characters must be escaped.
This PR adds a generic escapeControls function to the Formatter classes, which is used by Format::json::escape().

Copy link

what-the-diff bot commented Aug 15, 2024

PR Summary

  • Introduction of new file Formatter.cpp
    This new addition includes another file known as Formatter.h.

  • Enhancements in Formatter.cpp
    We have furnished this file with a fresh function named escapeChar(). We've also effectively implemented an existing function called escapeControls().

  • Modifications in Formatter.h
    There's a newcomer in the Format namespace, we've added a function named escapeControls().

  • Detailed tweaks in Json.cpp
    We have improved our escape() function by incorporating the escapeControls() function from Formatter.h. This step ensures better functionality in our JSON formatting.

@slaff slaff added this to the 6.0.0 milestone Aug 16, 2024
@slaff slaff merged commit daa4db6 into SmingHub:develop Aug 16, 2024
46 checks passed
slaff pushed a commit that referenced this pull request Aug 31, 2024
This PR improves on #2875 for JSON string formatting, updating the `escapeControls` function to handle unicode escaping correctly.

The check for valid UTF8 has been removed. That's erroneous and got in there because of my lack of understanding about how JSON is encoded. It's fundamentally UNICODE, so characters outside of standard ASCII must all be encoded using `\uNNNN` escapes. That ensures output is always valid JSON, even if string input is garbage.

The `escapeControls` function has a `options` parameter which makes it a lot more useful for various escaping requirements. This is also more efficient as the input string requires only two passes for both escaping and quoting: the first pass establishes the new string length, the buffer is re-allocated then the modified string produced.
@slaff slaff mentioned this pull request Dec 11, 2024
5 tasks
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.

2 participants