I want to use Unicode escape sequence for emoji character in my JSON text. But it appears JSONDecoder cannot handle escape sequence of more than 4 hex digits. Of all the examples I see online they are all limited to 4 hex digits or less. But emoji can have scalar value that's five hex digits.
Is the JSONDecoder bad or is JSON unicode code escape just not allow more than 4 hex digits?
let anEmoji: Character = "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}"
print(anEmoji) // prints: 👨🏻❤️👨🏻
struct Emoji: Decodable {
let character: String
}
let json = """
{ "character": "\\u1F468\\u1F3FB\\u200D\\u2764\\uFE0F\\u200D\\u1F468\\u1F3FB"}
"""
let decoder = JSONDecoder()
if let data = json.data(using: .utf8), let result = try? decoder.decode(Emoji.self, from: data) {
print(result) // expect: 👨🏻❤️👨🏻, actual: Emoji(character: "8ἿB❤️8ἿB")
} else {
print("No dice!")
}
Hm, looks like splitting the character into utf-16 codepoints and escaping those works, but I'm not sure whether that should work everywhere or it's just Foundation:
import Foundation
let anEmoji: Character = "\u{1F468}\u{1F3FB}\u{200D}\u{2764}\u{FE0F}\u{200D}\u{1F468}\u{1F3FB}"
let str = "\"" + anEmoji.utf16.map { "\\u" + String($0, radix: 16) }.joined() + "\""
let decoder = JSONDecoder()
if let data = str.data(using: .utf8), let result = try? decoder.decode(String.self, from: data) {
print(result) // works
} else {
print("No dice!")
}
Plain ascii, I see. Then if there's other user content in that JSON, like "René François", or, perhaps "“quotes”", you'll have to convert those to ASCII accordingly: "Ren\u00e9 Fran\u00e7ois", "\u201Cquotes\u201D"
Xcode is fine displaying emojis or any Unicode characters. Except it’s not able to display one giant string of emoji json text. I think the very long line is the problem. Same with Vim.
I link my json file in Xcode, click on it and it just a shade of grey. Then I have to use VSCode.
I see what you are talking about, looks like Xcode bug. I removed line breaks - and indeed I can see some symbols rendered incorrectly - then I insert a single line break before the sequence of incorrect symbols - and they start rendering correctly.
Perhaps you can split your giant line into a fragments of some reasonable length.
Hey, surprise! iPad Playground open my json file just fine, I cannot edit the file, maybe it’s because it’s a resource. But text selection, scroll is smooth and fast.
Maybe it’s built with new infrastructure. I hope iPad playground will grow into the new Xcode with no old baggage.