mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-07-02 10:38:16 +00:00
fix: add escape handling for byte arrays and strings in DeviceJsonApi
This commit is contained in:
parent
aa050121ae
commit
77c700c565
1 changed files with 41 additions and 1 deletions
|
|
@ -7,6 +7,7 @@ using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PepperDash.Essentials.Core
|
namespace PepperDash.Essentials.Core
|
||||||
|
|
@ -176,7 +177,20 @@ namespace PepperDash.Essentials.Core
|
||||||
{
|
{
|
||||||
if (!conversionType.IsEnum)
|
if (!conversionType.IsEnum)
|
||||||
{
|
{
|
||||||
return Convert.ChangeType(value, conversionType, System.Globalization.CultureInfo.InvariantCulture);
|
if (conversionType == typeof(byte[]) && value is string byteString)
|
||||||
|
{
|
||||||
|
var unescaped = UnescapeString(byteString);
|
||||||
|
return System.Text.Encoding.GetEncoding(28591).GetBytes(unescaped);
|
||||||
|
}
|
||||||
|
|
||||||
|
var converted = Convert.ChangeType(value, conversionType, System.Globalization.CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
if (conversionType == typeof(string) && converted is string s)
|
||||||
|
{
|
||||||
|
return UnescapeString(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
return converted;
|
||||||
}
|
}
|
||||||
|
|
||||||
var stringValue = Convert.ToString(value);
|
var stringValue = Convert.ToString(value);
|
||||||
|
|
@ -189,6 +203,32 @@ namespace PepperDash.Essentials.Core
|
||||||
return Enum.Parse(conversionType, stringValue, true);
|
return Enum.Parse(conversionType, stringValue, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Processes escape sequences in a string, converting sequences like \r, \n, \t, \xHH
|
||||||
|
/// to their corresponding non-printable ASCII characters.
|
||||||
|
/// </summary>
|
||||||
|
private static string UnescapeString(string input)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(input))
|
||||||
|
return input;
|
||||||
|
|
||||||
|
return Regex.Replace(input, @"\\(r|n|t|\\|x[0-9A-Fa-f]{2})", match =>
|
||||||
|
{
|
||||||
|
var seq = match.Groups[1].Value;
|
||||||
|
switch (seq)
|
||||||
|
{
|
||||||
|
case "r": return "\r";
|
||||||
|
case "n": return "\n";
|
||||||
|
case "t": return "\t";
|
||||||
|
case "\\": return "\\";
|
||||||
|
default:
|
||||||
|
// \xHH hex escape
|
||||||
|
var hex = seq.Substring(1);
|
||||||
|
return ((char)Convert.ToInt32(hex, 16)).ToString();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the properties on a device
|
/// Gets the properties on a device
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue