diff --git a/Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs b/Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs index f2ff438bb..c90e906b6 100644 --- a/Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs +++ b/Stack/Opc.Ua.Core/Types/Encoders/JsonEncoder.cs @@ -1345,6 +1345,7 @@ public void WriteStatusCode(string fieldName, StatusCode value) return; } + // Verbose and NonReversible if (value != StatusCodes.Good) { PushStructure(fieldName); @@ -1355,6 +1356,14 @@ public void WriteStatusCode(string fieldName, StatusCode value) WriteSimpleField("Symbol", symbolicId, EscapeOptions.Quotes | EscapeOptions.NoFieldNameEscape); } PopStructure(); + return; + } + + // Verbose needs empty object + if (EncodingToUse == JsonEncodingType.Verbose) + { + PushStructure(fieldName); + PopStructure(); } } diff --git a/Stack/Opc.Ua.Core/Types/Utils/Utils.cs b/Stack/Opc.Ua.Core/Types/Utils/Utils.cs index dca47d47e..e6236c51e 100644 --- a/Stack/Opc.Ua.Core/Types/Utils/Utils.cs +++ b/Stack/Opc.Ua.Core/Types/Utils/Utils.cs @@ -1265,10 +1265,37 @@ public static string ReplaceDCLocalhost(string subjectName, string hostname = nu /// public static string EscapeUri(string uri) { - if (!String.IsNullOrWhiteSpace(uri)) + if (!string.IsNullOrWhiteSpace(uri)) { - var builder = new UriBuilder(uri.Replace(";", "%3b")); - return builder.Uri.AbsoluteUri; + // back compat: for not well formed Uri, fall back to legacy formatting behavior - see #2793 + if (!Uri.IsWellFormedUriString(uri, UriKind.Absolute) || + !Uri.TryCreate(uri.Replace(";", "%3b"), UriKind.Absolute, out Uri validUri)) + { + var buffer = new StringBuilder(); + foreach (char ch in uri) + { + switch (ch) + { + case ';': + case '%': + { + buffer.AppendFormat(CultureInfo.InvariantCulture, "%{0:X2}", Convert.ToInt16(ch)); + break; + } + + default: + { + buffer.Append(ch); + break; + } + } + } + return buffer.ToString(); + } + else + { + return validUri.AbsoluteUri; + } } return String.Empty; diff --git a/Tests/Opc.Ua.Core.Tests/Types/Encoders/JsonEncoderTests.cs b/Tests/Opc.Ua.Core.Tests/Types/Encoders/JsonEncoderTests.cs index b70368980..e1cb071c9 100644 --- a/Tests/Opc.Ua.Core.Tests/Types/Encoders/JsonEncoderTests.cs +++ b/Tests/Opc.Ua.Core.Tests/Types/Encoders/JsonEncoderTests.cs @@ -438,6 +438,7 @@ public void ForcePropertiesShouldThrow(JsonEncodingType jsonEncodingType) Assert.Throws(() => encoder.ForceNamespaceUriForIndex1 = true); Assert.Throws(() => encoder.IncludeDefaultNumberValues = true); Assert.Throws(() => encoder.IncludeDefaultValues = true); + Assert.Throws(() => encoder.EncodeNodeIdAsString = false); } }