fix(commons): LoadLegacy handles mixed-type JSON values (number/bool/string)
This commit is contained in:
@@ -118,8 +118,27 @@ public static class OpcUaEndpointConfigSerializer
|
||||
|
||||
private static OpcUaEndpointConfig LoadLegacy(string json)
|
||||
{
|
||||
var dict = JsonSerializer.Deserialize<Dictionary<string, string>>(json)
|
||||
?? new Dictionary<string, string>();
|
||||
using var doc = JsonDocument.Parse(json);
|
||||
if (doc.RootElement.ValueKind != JsonValueKind.Object)
|
||||
throw new JsonException("Legacy JSON must be a flat object.");
|
||||
|
||||
var dict = new Dictionary<string, string>();
|
||||
foreach (var prop in doc.RootElement.EnumerateObject())
|
||||
{
|
||||
// JsonElement.ToString() returns the raw value for primitives:
|
||||
// "1000" for numbers, "true"/"false" for booleans, the string content
|
||||
// for strings. Nested objects/arrays serialize to their JSON; we don't
|
||||
// expect those in the legacy flat-dict shape, but FromFlatDict will
|
||||
// simply ignore unknown keys.
|
||||
dict[prop.Name] = prop.Value.ValueKind switch
|
||||
{
|
||||
JsonValueKind.String => prop.Value.GetString() ?? "",
|
||||
JsonValueKind.Number => prop.Value.GetRawText(),
|
||||
JsonValueKind.True => "True",
|
||||
JsonValueKind.False => "False",
|
||||
_ => prop.Value.ToString()
|
||||
};
|
||||
}
|
||||
return FromFlatDict(dict);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user