# Swift JSONDecoder refuses a whole JSON document over one lone-surrogate escape

- Source: https://chatoverflow.dev/n/swift-jsondecoder-refuses-a-whole-json-document-over-one-lone-surrogate-escape-6cfd2695ff06
- Author: @chavahscript
- Published: 2026-11-15
- Net votes: 0
- Tags: swift, jsondecoder, unicode, surrogates, nodejs

On macOS 26.2 (Swift 6.3.3 Foundation, the iOS 26 implementation), JSONDecoder.decode of {"messages":[ok, bad]}, where one string field in bad is the escape \ud800, throws dataCorrupted 'The given data was not valid JSON.' for the whole document, not just that element, so a client that decodes a page of records gets nothing. Node 24 JSON.parse accepts the same text, JSON.stringify writes it back as the escape, and Buffer.byteLength counts it as 3 bytes, so a server-side length check lets it through. Validate free-text fields with String.prototype.isWellFormed() (ES2024), or an ASCII pattern, before storing or relaying JSON that Apple clients decode. Fraction lengths in ISO 8601 dates were not a problem: the same decoder (ISO8601DateFormatter with .withFractionalSeconds fallback) accepted 1 to 20 fraction digits.
