using System.Text.Json; using System.Text.Json.Nodes; namespace ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared.Drivers; /// /// Preserve-unknown save for the typed driver forms. A form deserialises the stored DriverConfig /// into a typed options object and re-serialises it on save, which erases every key the type does not /// model (a newer option, a hand-edited key, rawTags, a device's extra keys…). This overlays only /// the paths the form OWNS onto the ORIGINAL JSON object: an owned path is replaced by the form's value, /// or removed when the form emits nothing/null ("Default"); every other key in the original survives. /// internal static class DriverConfigJsonOverlay { /// Overlays the of onto . /// The stored config as loaded; blank or non-object input yields unchanged. /// The form's own serialised options. /// Dot-separated, case-insensitive property paths the form edits (e.g. transport, probe.enabled). /// The merged JSON document. /// /// (TimeSpan-string path, integer-millisecond path) pairs. The typed options serialise durations as /// "00:00:05" but the driver factories bind *Ms integers; each pair is rewritten /// in the typed output (and the legacy TimeSpan path becomes owned, so a stale one is removed). /// public static string Apply(string? originalJson, string typedJson, IEnumerable ownedPaths, IReadOnlyList<(string TimeSpanPath, string MsPath)>? timeSpanToMs = null) { if (JsonNode.Parse(typedJson) is not JsonObject typed) return typedJson; var owned = ownedPaths.ToList(); foreach (var (tsPath, msPath) in timeSpanToMs ?? []) { owned.Add(tsPath); owned.Add(msPath); if (Get(typed, tsPath) is JsonValue v && v.TryGetValue(out var text) && TimeSpan.TryParse(text, System.Globalization.CultureInfo.InvariantCulture, out var ts)) { RemovePath(typed, tsPath); Set(typed, msPath, (int)ts.TotalMilliseconds); } } JsonObject? original = null; if (!string.IsNullOrWhiteSpace(originalJson)) { try { original = JsonNode.Parse(originalJson) as JsonObject; } catch (JsonException) { /* unusable original: fall through to the typed output */ } } if (original is null) return typed.ToJsonString(); foreach (var path in owned) { var segments = path.Split('.'); JsonObject? from = typed; JsonObject? to = original; for (var i = 0; i < segments.Length - 1 && to is not null; i++) { from = from is null ? null : Find(from, segments[i]) as JsonObject; var next = Find(to, segments[i]) as JsonObject; if (next is null && from is not null) { next = []; to[segments[i]] = next; } to = next; } if (to is null) continue; var leaf = segments[^1]; var value = from is null ? null : Find(from, leaf); Remove(to, leaf); if (value is not null) to[leaf] = value.DeepClone(); } return original.ToJsonString(); } private static JsonNode? Get(JsonObject root, string path) { JsonNode? cur = root; foreach (var seg in path.Split('.')) cur = cur is JsonObject o ? Find(o, seg) : null; return cur; } private static void RemovePath(JsonObject root, string path) { var segs = path.Split('.'); JsonObject? cur = root; for (var i = 0; i < segs.Length - 1 && cur is not null; i++) cur = Find(cur, segs[i]) as JsonObject; if (cur is not null) Remove(cur, segs[^1]); } private static void Set(JsonObject root, string path, JsonNode value) { var segs = path.Split('.'); var cur = root; for (var i = 0; i < segs.Length - 1; i++) { if (Find(cur, segs[i]) is not JsonObject next) { next = []; cur[segs[i]] = next; } cur = next; } cur[segs[^1]] = value; } private static JsonNode? Find(JsonObject o, string key) { foreach (var kv in o) if (string.Equals(kv.Key, key, StringComparison.OrdinalIgnoreCase)) return kv.Value; return null; } private static void Remove(JsonObject o, string key) { var hit = o.Select(kv => kv.Key).FirstOrDefault(k => string.Equals(k, key, StringComparison.OrdinalIgnoreCase)); if (hit is not null) o.Remove(hit); } }