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 all 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
86 changes: 78 additions & 8 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 @@ -501,6 +503,73 @@ 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 NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
/// <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)
{
WriteSpan(ref lastOffset, charSpan, i);
m_writer.Write('\\');
m_writer.Write(m_substitution[ii]);
found = true;
break;
}
}

if (!found && ch < 32)
{
WriteSpan(ref lastOffset, charSpan, i);
m_writer.Write('\\');
m_writer.Write('u');
m_writer.Write(((int)ch).ToString("X4", CultureInfo.InvariantCulture));
}
}

if (lastOffset == 0)
{
m_writer.Write(value);
}
else
{
WriteSpan(ref lastOffset, charSpan, charSpan.Length);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void WriteSpan(ref int lastOffset, ReadOnlySpan<char> valueSpan, int index)
{
if (lastOffset < index - 2)
{
m_writer.Write(valueSpan.Slice(lastOffset, index - lastOffset));
}
else
{
while (lastOffset < index)
{
m_writer.Write(valueSpan[lastOffset++]);
}
}
lastOffset = index + 1;
}
#else
/// <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 +591,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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -116,7 +117,7 @@ public async Task OneTimeSetUpAsync(TextWriter writer = null)

await m_clientFixture.LoadClientConfiguration(m_pkiRoot).ConfigureAwait(false);
m_clientFixture.Config.TransportQuotas.MaxMessageSize = 4 * 1024 * 1024;
m_url = new Uri(m_uriScheme + "://localhost:" + m_serverFixture.Port.ToString());
m_url = new Uri(m_uriScheme + "://localhost:" + m_serverFixture.Port.ToString(CultureInfo.InvariantCulture));
try
{
m_session = await m_clientFixture.ConnectAsync(m_url, SecurityPolicies.Basic256Sha256).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -104,8 +105,8 @@ public class ComplexTypesJsonEncoderTests : ComplexTypesCommon
{ BuiltInType.Int16, (Int16)(-12345), "-12345", null },
{ BuiltInType.UInt32, (UInt32)1234567, "1234567", null },
{ BuiltInType.Int32, (Int32)(-12345678), "-12345678", null },
{ BuiltInType.Int64, kInt64Value, Quotes(kInt64Value.ToString()), null },
{ BuiltInType.UInt64, (UInt64)kUInt64Value, Quotes(kUInt64Value.ToString()), null },
{ BuiltInType.Int64, kInt64Value, Quotes(kInt64Value.ToString(CultureInfo.InvariantCulture)), null },
{ BuiltInType.UInt64, (UInt64)kUInt64Value, Quotes(kUInt64Value.ToString(CultureInfo.InvariantCulture)), null },
{ BuiltInType.Float, (float)3.14, "3.14", "3.14" },
// TODO: why is JToken.DeepEquals failing here?
//{ BuiltInType.Float, float.PositiveInfinity, "Infinity", "Infinity" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
Expand Down Expand Up @@ -670,7 +671,7 @@ private object GetRandom(NodeId valueType)
{
var buildInfo = new BuildInfo() {
BuildDate = DataGenerator.GetRandomDateTime(),
BuildNumber = "1.4." + DataGenerator.GetRandomByte().ToString(),
BuildNumber = "1.4." + DataGenerator.GetRandomByte().ToString(CultureInfo.InvariantCulture),
ManufacturerName = "OPC Foundation",
ProductName = "Complex Type Client",
ProductUri = "http://opcfoundation.org/ComplexTypeClient",
Expand Down
Loading
Loading