Skip to content

Escape JSON special characters in Serialize.tryToJSONString#214

Merged
turbolent merged 1 commit into
mainfrom
janez/escape-json
Jun 18, 2026
Merged

Escape JSON special characters in Serialize.tryToJSONString#214
turbolent merged 1 commit into
mainfrom
janez/escape-json

Conversation

@janezpodhostnik

@janezpodhostnik janezpodhostnik commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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!

@turbolent turbolent left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment thread cadence/contracts/utils/Serialize.cdc
Comment thread cadence/contracts/utils/Serialize.cdc
Comment thread cadence/contracts/utils/Serialize.cdc
@turbolent

turbolent commented Jun 17, 2026

Copy link
Copy Markdown
Member

Opened a follow-up PR: #215

@turbolent turbolent mentioned this pull request Jun 17, 2026
6 tasks
@turbolent turbolent merged commit addbae1 into main Jun 18, 2026
2 checks passed
@turbolent turbolent deleted the janez/escape-json branch June 18, 2026 20:59
@github-project-automation github-project-automation Bot moved this from 👀 In Review to ✅ Done in 🌊 Flow 4D Jun 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done
Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

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

4 participants