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 13 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
64 changes: 37 additions & 27 deletions Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Xml;

namespace Opc.Ua
Expand All @@ -35,6 +36,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 +90,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 +120,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 +296,11 @@ public int Close()
{
if (m_topLevelIsArray)
{
m_writer.Write("]");
m_writer.Write(']');
}
else
{
m_writer.Write("}");
m_writer.Write('}');
}
}

Expand Down Expand Up @@ -498,38 +501,45 @@ public void PopNamespace()
m_namespaces.Pop();
}

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' };
private static readonly Dictionary<char, string> m_substitution = new Dictionary<char, string>
{
{ s_quotation, "\\\"" },
{ s_backslash, "\\\\" },
{ '\n', "\\n" },
{ '\r', "\\r" },
{ '\t', "\\t" },
{ '\b', "\\b" },
{ '\f', "\\f" }
};

/// <summary>
/// Escapes a string and writes it to the stream.
/// </summary>
/// <param name="value"></param>
private void EscapeString(string value)
bhnaphade marked this conversation as resolved.
Show resolved Hide resolved
{
m_stringBuilder.Clear();
m_stringBuilder.EnsureCapacity(value.Length * 2);

foreach (char ch in value)
{
bool found = false;

for (int ii = 0; ii < m_specialChars.Length; ii++)
// Check if ch is present in the dictionary
if (m_substitution.TryGetValue(ch, out string escapeSequence))
{
if (m_specialChars[ii] == ch)
{
m_writer.Write(s_backslash);
m_writer.Write(m_substitution[ii]);
found = true;
break;
}
m_stringBuilder.Append(escapeSequence);
}

if (!found)
else if (ch < 32)
{
if (ch < 32)
{
m_writer.Write("\\u");
m_writer.Write("{0:X4}", (int)ch);
continue;
}

m_writer.Write(ch);
m_stringBuilder.Append("\\u");
m_stringBuilder.Append(((int)ch).ToString("X4", CultureInfo.InvariantCulture));
}
else
{
m_stringBuilder.Append(ch);
}
}

m_writer.Write(m_stringBuilder);
}

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