Skip to content
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

Improve JsonEncoder.EscapeString and fix test warnings for ToString() #2527

Merged
merged 22 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 69 additions & 9 deletions Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class JsonEncoder : IJsonEncoder
private static readonly char s_rightCurlyBrace = '}';
private static readonly char s_leftSquareBracket = '[';
private static readonly char s_rightSquareBracket = ']';
private StringBuilder m_stringBuilder = new StringBuilder();
private static readonly UTF8Encoding s_utf8Encoding = new UTF8Encoding(false);
private Stream m_stream;
private MemoryStream m_memoryStream;
private StreamWriter m_writer;
Expand Down Expand Up @@ -87,12 +89,12 @@ public JsonEncoder(
if (m_stream == null)
{
m_memoryStream = new MemoryStream();
m_writer = new StreamWriter(m_memoryStream, new UTF8Encoding(false), streamSize, false);
m_writer = new StreamWriter(m_memoryStream, s_utf8Encoding, streamSize, false);
m_leaveOpen = false;
}
else
{
m_writer = new StreamWriter(m_stream, new UTF8Encoding(false), streamSize, m_leaveOpen);
m_writer = new StreamWriter(m_stream, s_utf8Encoding, streamSize, m_leaveOpen);
}

InitializeWriter();
Expand All @@ -117,7 +119,7 @@ public JsonEncoder(
if (m_writer == null)
{
m_stream = new MemoryStream();
m_writer = new StreamWriter(m_stream, new UTF8Encoding(false), kStreamWriterBufferSize);
m_writer = new StreamWriter(m_stream, s_utf8Encoding, kStreamWriterBufferSize);
}

InitializeWriter();
Expand Down Expand Up @@ -293,11 +295,11 @@ public int Close()
{
if (m_topLevelIsArray)
{
m_writer.Write("]");
m_writer.Write(s_rightSquareBracket);
}
else
{
m_writer.Write("}");
m_writer.Write(s_rightCurlyBrace);
}
}

Expand Down Expand Up @@ -499,8 +501,65 @@ public void PopNamespace()
}

private static readonly char[] m_specialChars = new char[] { s_quotation, s_backslash, '\n', '\r', '\t', '\b', '\f', };
private static readonly char[] m_substitution = new char[] { s_quotation, s_backslash, 'n', 'r', 't', 'b', 'f' };
#if NETCOREAPP2_1_OR_GREATER
bhnaphade marked this conversation as resolved.
Show resolved Hide resolved
private static readonly string[] m_substitutionStrings = new string[] { "\\\"", "\\\\", "\\n", "\\r", "\\t", "\\b", "\\f" };

/// <summary>
/// Using a span to escape the string, write strings to stream writer if possible.
/// </summary>
/// <param name="value"></param>
private void EscapeString(string value)
bhnaphade marked this conversation as resolved.
Show resolved Hide resolved
{
ReadOnlySpan<char> charSpan = value.AsSpan();
int lastOffset = 0;

for (int i = 0; i < charSpan.Length; i++)
{
bool found = false;
char ch = charSpan[i];

for (int ii = 0; ii < m_specialChars.Length; ii++)
{
if (m_specialChars[ii] == ch)
{
if (lastOffset < i)
{
m_writer.Write(charSpan.Slice(lastOffset, i - lastOffset));
}
lastOffset = i + 1;
m_writer.Write(m_substitutionStrings[ii]);
found = true;
break;
}
}

if (!found && ch < 32)
{
if (lastOffset < i)
{
m_writer.Write(charSpan.Slice(lastOffset, i - lastOffset));
}
lastOffset = i + 1;
m_writer.Write("\\u");
m_writer.Write(((int)ch).ToString("X4", CultureInfo.InvariantCulture));
}
}
if (lastOffset == 0)
{
m_writer.Write(value);
}
else if (lastOffset < charSpan.Length)
{
m_writer.Write(charSpan.Slice(lastOffset, charSpan.Length - lastOffset));
}
}
#else
private static readonly char[] m_substitution = new char[] { '\"', '\\', 'n', 'r', 't', 'b', 'f' };

/// <summary>
/// Escapes a string and writes it to the stream.
/// </summary>
/// <param name="value"></param>
private void EscapeString(string value)
{
foreach (char ch in value)
Expand All @@ -522,15 +581,16 @@ private void EscapeString(string value)
{
if (ch < 32)
{
m_writer.Write("\\u");
m_writer.Write("{0:X4}", (int)ch);
m_writer.Write(s_backslash);
m_writer.Write('u');
m_writer.Write(((int)ch).ToString("X4", CultureInfo.InvariantCulture));
continue;
}

m_writer.Write(ch);
}
}
}
#endif

private void WriteSimpleField(string fieldName, string value, bool quotes)
{
Expand Down
Loading
Loading