using System.Text; using System.Text.Json; namespace ZB.MOM.WW.ScadaBridge.CLI; /// /// Shared trigger-config JSON helpers for CLI paths that receive a raw /// --trigger-config JSON blob and need to inject or strip the /// "analysisKind" key based on --trigger-kind. /// Used by alarm update, script add, and script update /// (alarm add uses instead). /// internal static class TriggerConfigJson { /// /// Copies every property from into a new object, /// adding or replacing "analysisKind" when /// is "strict" (case-insensitive), /// or omitting it for any other value (including ). /// Returns when is /// or empty, preserving the caller's null-means-no-config /// semantics. /// /// Raw trigger-config JSON object, or null. /// /// Value of --trigger-kind: "strict" → write /// "analysisKind":"Strict"; anything else → omit the key. /// /// /// The rewritten JSON string, or when /// is null/empty. /// internal static string? InjectAnalysisKind(string? json, string? analysisKind) { if (string.IsNullOrWhiteSpace(json)) return null; var isStrict = string.Equals(analysisKind?.Trim(), "Strict", StringComparison.OrdinalIgnoreCase); using var inputDoc = JsonDocument.Parse(json); using var stream = new MemoryStream(); using (var writer = new Utf8JsonWriter(stream)) { writer.WriteStartObject(); // Copy all existing properties except "analysisKind" (we'll re-add it below if needed) foreach (var prop in inputDoc.RootElement.EnumerateObject()) { if (!prop.NameEquals("analysisKind")) prop.WriteTo(writer); } if (isStrict) writer.WriteString("analysisKind", "Strict"); writer.WriteEndObject(); } return Encoding.UTF8.GetString(stream.ToArray()); } }