Skip to content

Escape JSON special characters in Serialize.tryToJSONString#214

Open
janezpodhostnik wants to merge 1 commit into
mainfrom
janez/escape-json
Open

Escape JSON special characters in Serialize.tryToJSONString#214
janezpodhostnik wants to merge 1 commit into
mainfrom
janez/escape-json

Conversation

@janezpodhostnik

@janezpodhostnik janezpodhostnik commented Jun 11, 2026

Copy link
Copy Markdown

Closes: #213

Description

Adds an escapeJSONString helper to Serialize.cdc that escapes \, ", and control characters (U+0000–U+001F) per RFC 8259 §7, and applies it in the String, String?, and Character cases of tryToJSONString. Dictionary keys in dictToJSONString are serialized through the same String case, so they are covered as well.

Escaping is a single pass over UTF-8 bytes with a fast path that returns the input unchanged when nothing needs escaping; multi-byte UTF-8 sequences pass through untouched.

Includes unit tests for all escape classes (quotes, backslash, \n/\r/\t, \b/\f, \u00XX control chars, multi-byte passthrough, dict keys) and a regression test serializing an NFT with quotes, a newline, and a tab in its name/description to a parseable JSON data URI.


For contributor use:

  • Targeted PR against main branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@janezpodhostnik janezpodhostnik requested a review from a team as a code owner June 11, 2026 16:30
@github-project-automation github-project-automation Bot moved this to 👀 In Review in 🌊 Flow 4D Jun 11, 2026
@janezpodhostnik janezpodhostnik self-assigned this Jun 11, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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

Labels

None yet

Projects

Status: No status
Status: 👀 In Review

Development

Successfully merging this pull request may close these issues.

Serialize.tryToJSONString does not escape JSON special characters, producing invalid metadata JSON

2 participants