using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; namespace Adaptation.FileHandlers.pcl; #nullable enable internal class SummarySegment { public SummarySegment(string nAvg, string nsl, string vd, string flatZ, string rhoAvg, string rhosl, string phase, string grade, string rs) { NAvg = nAvg; Nsl = nsl; Vd = vd; FlatZ = flatZ; RhoAvg = rhoAvg; Rhosl = rhosl; Phase = phase; Grade = grade; Rs = rs; } [JsonPropertyName("Navg")] public string NAvg { get; } [JsonPropertyName("Nsl")] public string Nsl { get; } [JsonPropertyName("Vd")] public string Vd { get; } [JsonPropertyName("Flat Z")] public string FlatZ { get; } [JsonPropertyName("Rhoavg")] public string RhoAvg { get; } [JsonPropertyName("Rhosl")] public string Rhosl { get; } [JsonPropertyName("Phase")] public string Phase { get; } [JsonPropertyName("Grade")] public string Grade { get; } [JsonPropertyName("Rs")] public string Rs { get; } public static SummarySegment Get() => new(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty); private static ReadOnlyCollection GetJsonProperties() { JsonProperty[] results; string json; SummarySegment summarySegment = Get(); json = JsonSerializer.Serialize(summarySegment); JsonElement jsonElement = JsonSerializer.Deserialize(json); results = jsonElement.EnumerateObject().ToArray(); return new(results); } public static Summary? Get(Constant constant, ReadOnlyCollection lines) { Summary? result; string json; string[] segments; bool found = false; string[] segmentsB; List names = new(); Dictionary keyValuePairs = new(); Dictionary keyValuePairsB = new(); Dictionary keyValuePairsC = new(); ReadOnlyCollection jsonProperties = GetJsonProperties(); foreach (string line in lines) { if (line == constant.SummaryLine) found = true; if (!found) continue; if (line.Contains(constant.Site)) break; if (line.Contains(constant.LastUnits)) break; foreach (JsonProperty jsonProperty in jsonProperties) { segments = line.Split(new string[] { $"{jsonProperty.Name}:", $"{jsonProperty.Name} :" }, StringSplitOptions.None); if (segments.Length < 2 || !line.StartsWith(jsonProperty.Name)) continue; segmentsB = segments[1].Trim().Split(' '); if (segmentsB.Length < 3) continue; if (names.Contains(jsonProperty.Name)) continue; names.Add(jsonProperty.Name); keyValuePairs.Add(jsonProperty.Name, segmentsB[0]); keyValuePairsB.Add(jsonProperty.Name, segmentsB[1]); keyValuePairsC.Add(jsonProperty.Name, segmentsB[2]); } } if (keyValuePairs.Count != jsonProperties.Count || keyValuePairsB.Count != jsonProperties.Count || keyValuePairsC.Count != jsonProperties.Count) result = null; else { json = JsonSerializer.Serialize(keyValuePairs); SummarySegment? mean = JsonSerializer.Deserialize(json, SummarySegmentSourceGenerationContext.Default.SummarySegment); json = JsonSerializer.Serialize(keyValuePairsB); SummarySegment? standardDeviationPercentage = JsonSerializer.Deserialize(json, SummarySegmentSourceGenerationContext.Default.SummarySegment); json = JsonSerializer.Serialize(keyValuePairsC); SummarySegment? radialGradient = JsonSerializer.Deserialize(json, SummarySegmentSourceGenerationContext.Default.SummarySegment); result = new(mean, standardDeviationPercentage, radialGradient); } return result; } } [JsonSourceGenerationOptions(WriteIndented = true)] [JsonSerializable(typeof(SummarySegment))] internal partial class SummarySegmentSourceGenerationContext : JsonSerializerContext { }