You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider adding a JavaScriptEncoder implementation that doesn't encode the block list or surrogate pairs. #42847
There is no built-in mechanism for allowing this. You'd have to subclass the JavaScriptEncoder type and set the JsonSerializerOptions.Encoder property to an instance of your custom type.
@layomia - We could consider allowing the unsafe relaxed escaper to allow known-good supplementary characters. This isn't trivial work, though.
Could you describe why this is a blocker? Those values are correctly deserialized; it just means that if you look a the raw JSON payload you'll seem the escaped.
@terrajobst sure. Our mobile apps display the escaped text as is instead of the emojis.
That sounds like a problem with your decoding app not correctly handling valid JSON.
The JSON specification says: ' To escape an extended character that is not in the Basic Multilingual Plane, the character is represented as a twelve-character sequence, encoding the UTF-16 surrogate pair. So, for example, a string containing only the G clef character (U+1D11E) may be represented as "\uD834\uDD1E". '
Technically only the quotation mark and reverse solidus require escaping (it is the only way to represent them), so maybe they could add switches to control if other character do or do not get encoded (tabs, line feeds, control characters, values outside ASCII, values outside BMP). e.g. the UTF-8 byte sequence will be shorted than the 12-byte escaped surrogate pair, so save a few bytes.
But the decoding app should still handle all valid encodings.
You are asking for a work around to compensate for a bug in your mobile app.
I'm honestly surprised more people aren't having this issue considering how prevalent emojis are in user generated content.
Because as mentioned previously, this appears to be a bug in the component that is deserializing the JSON payload. All JSON deserializers in wide use that we're aware of follow the RFC and properly turn the \uXXXX\uYYYY pair back into the correct emoji.
Would subclassing the escaper (see my comment from a few months ago) work for your scenario? This puts you in full control over how the payload is generated, which should give you sufficient berth to work around other bugs in the deserialization component you use. I suspect if that component isn't properly decoding \uXXXX substrings, it probably has other bugs that you may need to work around.
The decision that was made here conflicts with major browser's behavior including Microsoft Edge.
Please reconsider the issue as it really hurts.
!Text.Json serializer converts emoji icons to encoded codepoints and the browsers do not deserialize them correctly! (This statement is incorrect. Problem was in db serialization but not browsers.)
Demonstration of JSON serialization and deserialization that contains an emoji. var jsObjectWithEmoji = { text: '😀' }; JSON.stringify(jsObjectWithEmoji); JSON.parse(JSON.stringify(jsObjectWithEmoji));
Can you provide an example of a System.Text.Json-serialized payload that the browser API JSON.parse does not properly decode? The screenshot you provided above does not demonstrate such an example.
Hi there!
This turns out a serialization issue on the graph database I used. Thank you for the pointers.
Browsers deserialize these unicode encodings correctly! That statement was clearly false.
On the other hand, browsers by default serialize and deserialize the emojis as is as seen in the screen shots.
It is a good idea to add that support to .NET.
changed the title [-]JsonSerializer.Serialize support for emojis defined with surrogate pairs[/-][+]Consider adding a JavaScriptEncoder implementation that doesn't encode the block list or surrogate pairs.[/+]on Jun 5, 2023
Activity
GrabYourPitchforks commentedon Sep 29, 2020
There is no built-in mechanism for allowing this. You'd have to subclass the
JavaScriptEncodertype and set theJsonSerializerOptions.Encoderproperty to an instance of your custom type.@layomia - We could consider allowing the unsafe relaxed escaper to allow known-good supplementary characters. This isn't trivial work, though.
dan655t commentedon Sep 30, 2020
Thank you for the reply. This is blocking us from migrating off of Newtonsoft.
terrajobst commentedon Mar 8, 2021
@dan655
Could you describe why this is a blocker? Those values are correctly deserialized; it just means that if you look a the raw JSON payload you'll seem the escaped.
dan655t commentedon Mar 8, 2021
@terrajobst sure. Our mobile apps display the escaped text as is instead of the emojis.
sgryphon commentedon Mar 13, 2021
That sounds like a problem with your decoding app not correctly handling valid JSON.
The JSON specification says: ' To escape an extended character that is not in the Basic Multilingual Plane, the character is represented as a twelve-character sequence, encoding the UTF-16 surrogate pair. So, for example, a string containing only the G clef character (U+1D11E) may be represented as "\uD834\uDD1E". '
Technically only the quotation mark and reverse solidus require escaping (it is the only way to represent them), so maybe they could add switches to control if other character do or do not get encoded (tabs, line feeds, control characters, values outside ASCII, values outside BMP). e.g. the UTF-8 byte sequence will be shorted than the 12-byte escaped surrogate pair, so save a few bytes.
But the decoding app should still handle all valid encodings.
You are asking for a work around to compensate for a bug in your mobile app.
dan655t commentedon Mar 13, 2021
I disagree with this take.
I'm honestly surprised more people aren't having this issue considering how prevalent emojis are in user generated content.
GrabYourPitchforks commentedon Mar 13, 2021
Because as mentioned previously, this appears to be a bug in the component that is deserializing the JSON payload. All JSON deserializers in wide use that we're aware of follow the RFC and properly turn the \uXXXX\uYYYY pair back into the correct emoji.
Would subclassing the escaper (see my comment from a few months ago) work for your scenario? This puts you in full control over how the payload is generated, which should give you sufficient berth to work around other bugs in the deserialization component you use. I suspect if that component isn't properly decoding \uXXXX substrings, it probably has other bugs that you may need to work around.
dan655t commentedon Mar 13, 2021
I will look into a workaround. Thank you all for the responses. Will close this issue.
koculu commentedon Mar 30, 2021
The decision that was made here conflicts with major browser's behavior including Microsoft Edge.
Please reconsider the issue as it really hurts.
!Text.Json serializer converts emoji icons to encoded codepoints and the browsers do not deserialize them correctly!(This statement is incorrect. Problem was in db serialization but not browsers.)Demonstration of JSON serialization and deserialization that contains an emoji.
var jsObjectWithEmoji = { text: '😀' }; JSON.stringify(jsObjectWithEmoji); JSON.parse(JSON.stringify(jsObjectWithEmoji));Microsoft Edge Console Demo:

GrabYourPitchforks commentedon Mar 30, 2021
Can you provide an example of a System.Text.Json-serialized payload that the browser API
JSON.parsedoes not properly decode? The screenshot you provided above does not demonstrate such an example.koculu commentedon Mar 30, 2021
Hi there!
This turns out a serialization issue on the graph database I used. Thank you for the pointers.
Browsers deserialize these unicode encodings correctly! That statement was clearly false.
On the other hand, browsers by default serialize and deserialize the emojis as is as seen in the screen shots.
It is a good idea to add that support to .NET.
koculu commentedon Mar 30, 2021
I switched to Newtonsoft Json after spending several hours on this.
Emojis are very common and .Net 5 should handle them out of the box.
eiriktsarpalis commentedon May 26, 2023
Reopening for future consideration.
[-]JsonSerializer.Serialize support for emojis defined with surrogate pairs[/-][+]Consider adding a JavaScriptEncoder implementation that doesn't encode the block list or surrogate pairs.[/+]eiriktsarpalis commentedon Jun 6, 2023
Closing in favor of #87153.