JSONEncoder ASCII encoding issue

Hi, I discovered a weird issue with iOS 17. Everything is working fine on iOS 16. Encoding an ASCII string with JSONEncoder is not working from iOS 17.

For example: I have an image, convert it to a string using ASCII encoding, and then, add this string into an object. I take that object and using JSONEncoder, I try to encode it to send It to the server.

Result:

On iOS16: Everything works as expected. Example of my image after encoding:

ÿØÿà\0\u{10}JFIF\0\u{01}\u{02}\u{01}\0H\0H\0\0ÿá\u{0C}\u{10}Exif\0\0MM\0*\0\0\0\u{08}\0\u{06}\u{01}\u{12}\0\u{03}\0\0\0\u{01}\0\u{03}\0\0\u{01}\u{1A}\0\u{05}\0\0\0\u{01}\0\0\0V\u{01}\u{1B}\0\u{05}\0\0\0\u{01}\0\0\0^\u{01}(\0\u{03}\0\0\0\u{01}\0\u{02}\0\0\u{02}\u{13}\0\u{03}\0\0\0\u{01}\0\u{01}\0\0‡i\0\u{04}\0\0\0\u{01}\0\0\0f\0\0\0À\0\0\0H\0\0\0\u{01}\0\0\0H\0\0\0\u{01}\0\u{07}\0\0\u{07}\0\0\0\u{04}................

on iOS17: I received a cut string Example of my image after encoding:

ÿØÿà

Just that. It's stops at \0

Am I missing something?

You'll have to be explicit about what API you're using at each step of the process you described.

The essence of your problem is that the fifth character of your "ASCII" data is a null (0) character. Depending on which API you're using, this might be interpreted as an end-of-string marker, or it might be treated as an error when converted to Unicode.

My guess here is that the APIs used to replace the invalid \0 character with a Unicode invalid-input marker character, but now produces an error instead. Again, though, the behavior depends on what APIs you are using.

I can confirm your finding on iOS 17 and macOS Sonoma: JSONEncoder’s encoding of strings now stops at the first zero byte. It appears to be this bug which was introduced in the Swift rewrite of JSON encoding: JSONEncoder: Fixed escaping of certain strings.

Since there’s no good workaround, is it feasible to change your blob encoding? Your current encoding scheme is pretty unusual. If you directly encode your image to JSON as a Data object, it uses base64 and just works.

JSONEncoder ASCII encoding issue
 
 
Q