using System.Globalization; using System.Text.Json; using ZB.MOM.WW.ScadaBridge.Commons.Types.DataConnections; namespace ZB.MOM.WW.ScadaBridge.Commons.Serialization; /// /// Serializes to/from the typed JSON stored in /// DataConnection.PrimaryConfiguration / BackupConfiguration, and flattens /// it to the IDictionary<string,string> shape IDataConnection.ConnectAsync /// expects. MxGateway is net-new, so there is no legacy shape to recover — a row that /// fails to parse yields a default config. /// public static class MxGatewayEndpointConfigSerializer { private static readonly JsonSerializerOptions JsonOpts = new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, WriteIndented = false, }; /// Serializes a config to the typed JSON shape. /// The endpoint configuration to serialize. /// A camelCase JSON string representing the endpoint configuration. public static string Serialize(MxGatewayEndpointConfig config) => JsonSerializer.Serialize(config, JsonOpts); /// Parses stored config JSON; null/blank/malformed yields a default config. /// The stored JSON string. /// The deserialized , or a default instance if the input is null, blank, or malformed. public static MxGatewayEndpointConfig Deserialize(string? json) { if (string.IsNullOrWhiteSpace(json)) return new MxGatewayEndpointConfig(); try { return JsonSerializer.Deserialize(json, JsonOpts) ?? new MxGatewayEndpointConfig(); } catch (JsonException) { return new MxGatewayEndpointConfig(); } } /// Flattens the typed config to the key-value shape the adapter consumes. /// The endpoint configuration to flatten. /// A string-keyed dictionary containing all endpoint configuration properties. public static IDictionary ToFlatDict(MxGatewayEndpointConfig c) => new Dictionary { ["Endpoint"] = c.Endpoint, ["ApiKey"] = c.ApiKey, ["ClientName"] = c.ClientName, ["WriteUserId"] = c.WriteUserId.ToString(CultureInfo.InvariantCulture), ["UseTls"] = c.UseTls.ToString(), ["CaFile"] = c.CaFile, ["ServerName"] = c.ServerName, ["ReadTimeoutMs"] = c.ReadTimeoutMs.ToString(CultureInfo.InvariantCulture), }; /// Reconstructs a config from the flat key-value shape; invalid numerics fall back to defaults. /// The flat dictionary. /// A populated from the dictionary; missing or invalid entries use default values. public static MxGatewayEndpointConfig FromFlatDict(IDictionary d) { var c = new MxGatewayEndpointConfig(); if (d.TryGetValue("Endpoint", out var ep) && !string.IsNullOrWhiteSpace(ep)) c.Endpoint = ep; if (d.TryGetValue("ApiKey", out var ak)) c.ApiKey = ak; if (d.TryGetValue("ClientName", out var cn)) c.ClientName = cn; if (d.TryGetValue("WriteUserId", out var wu) && int.TryParse(wu, out var wuv)) c.WriteUserId = wuv; if (d.TryGetValue("UseTls", out var tls) && bool.TryParse(tls, out var tlsv)) c.UseTls = tlsv; if (d.TryGetValue("CaFile", out var ca)) c.CaFile = ca; if (d.TryGetValue("ServerName", out var sn)) c.ServerName = sn; if (d.TryGetValue("ReadTimeoutMs", out var rt) && int.TryParse(rt, out var rtv)) c.ReadTimeoutMs = rtv; return c; } }