-
-
Notifications
You must be signed in to change notification settings - Fork 491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix JSON serialization for UTF-32 characters. #998
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ public class Emitter : IEmitter | |
private bool isWhitespace; | ||
private bool isIndentation; | ||
private readonly bool forceIndentLess; | ||
private readonly bool useUtf16SurrogatePair; | ||
private readonly string newLine; | ||
|
||
private bool isDocumentEndWritten; | ||
|
@@ -148,6 +149,7 @@ public Emitter(TextWriter output, EmitterSettings settings) | |
this.maxSimpleKeyLength = settings.MaxSimpleKeyLength; | ||
this.skipAnchorName = settings.SkipAnchorName; | ||
this.forceIndentLess = !settings.IndentSequences; | ||
this.useUtf16SurrogatePair = settings.UseUtf16SurrogatePairs; | ||
this.newLine = settings.NewLine; | ||
|
||
this.output = output; | ||
|
@@ -1189,8 +1191,20 @@ private void WriteDoubleQuotedScalar(string value, bool allowBreaks) | |
{ | ||
if (index + 1 < value.Length && IsLowSurrogate(value[index + 1])) | ||
{ | ||
Write('U'); | ||
Write(char.ConvertToUtf32(character, value[index + 1]).ToString("X08", CultureInfo.InvariantCulture)); | ||
if (useUtf16SurrogatePair) | ||
{ | ||
Write('u'); | ||
Write(code.ToString("X04", CultureInfo.InvariantCulture)); | ||
Write('\\'); | ||
Write('u'); | ||
Write(((ushort)value[index + 1]).ToString("X04", CultureInfo.InvariantCulture)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should you check to make sure this won’t go out of bounds? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure but do you need to advance the index by one to handle the case when utf32 character is in the middle of the string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't all of this already done on lines 1192 and 1208? The previous code worked somehow 🤷 we are just rendering it differently, not reading more or less than what was read before. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not at a computer, just a phone, so reviewing prs can be a bit tricky. If it’s already done then great. Ignore this comment. |
||
} | ||
else | ||
{ | ||
Write('U'); | ||
Write(char.ConvertToUtf32(character, value[index + 1]).ToString("X08", CultureInfo.InvariantCulture)); | ||
} | ||
|
||
index++; | ||
} | ||
else | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently, lobster in UTF32 is different from lobster in UTF16. 🤷
https://www.unicodecharacter.org/U+1F99E