Sort and use JsonSerializerContext

This commit is contained in:
Mike Phares 2023-07-21 15:56:04 -07:00
parent 4bbcf04c1d
commit 23e45671c3
3 changed files with 55 additions and 29 deletions

View File

@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace View_by_Distance.Shared.Models;
@ -17,8 +18,20 @@ public record GenealogicalDataCommunication(DateTime? Birth,
public override string ToString()
{
string result = JsonSerializer.Serialize(this, new JsonSerializerOptions() { WriteIndented = true, DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull });
string result = JsonSerializer.Serialize(this, GenealogicalDataCommunicationSourceGenerationContext.Default.GenealogicalDataCommunication);
return result;
}
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(GenealogicalDataCommunication))]
internal partial class GenealogicalDataCommunicationSourceGenerationContext : JsonSerializerContext
{
}
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(GenealogicalDataCommunication[]))]
internal partial class GenealogicalDataCommunicationSourceCollectionGenerationContext : JsonSerializerContext
{
}

View File

@ -14,6 +14,7 @@ internal abstract class MarkDown
{
List<string> results = new();
string afterTrim;
string[] segments;
string[] jsonLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine);
foreach (string jsonLine in jsonLines)
{

View File

@ -1,7 +1,6 @@
using System.Collections.ObjectModel;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
namespace View_by_Distance.Shared.Models.Stateless.Methods;
@ -394,25 +393,57 @@ internal abstract class PersonContainer
return new(results);
}
private static void WriteAll(long ticks, string a2PeopleContentDirectory, List<string[]> frontMatterLinesCollections, string weekOfYear)
{
string json;
string[] segments;
string? directory;
string frontMatterLastLine;
DateTime dateTime = new(ticks);
List<string> allFrontMatterLines = new();
foreach (string[] frontMatterLines in frontMatterLinesCollections)
{
allFrontMatterLines.Add("{");
frontMatterLastLine = frontMatterLines.Last();
foreach (string frontMatterLine in frontMatterLines)
{
segments = frontMatterLine.Split(": ");
if (segments.Length != 2)
continue;
if (frontMatterLine == frontMatterLastLine)
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)}");
else
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)},");
}
allFrontMatterLines.Add("},");
}
directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString());
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
json = $"[{string.Join(Environment.NewLine, allFrontMatterLines.ToArray(), 0, allFrontMatterLines.Count - 1)}{allFrontMatterLines.Last()[..^1]}]";
Models.GenealogicalDataCommunication[]? genealogicalDataCommunications = JsonSerializer.Deserialize(json, GenealogicalDataCommunicationSourceCollectionGenerationContext.Default.GenealogicalDataCommunicationArray);
if (genealogicalDataCommunications is null)
throw new NullReferenceException(nameof(genealogicalDataCommunications));
json = JsonSerializer.Serialize(genealogicalDataCommunications, GenealogicalDataCommunicationSourceCollectionGenerationContext.Default.GenealogicalDataCommunicationArray);
_ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true);
}
internal static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory)
{
bool? male;
bool? first;
string json;
string fullName;
string[] matches;
string[] segments;
string? directory;
bool isDefaultName;
const int zero = 0;
string personKeyFormatted;
string frontMatterLastLine;
string lowerHyphenFullName;
List<string> frontMatterLines;
string pattern = "[^a-z0-9-]";
DateTime dateTime = new(ticks);
List<string> allFrontMatterLines = new();
Calendar calendar = new CultureInfo("en-US").Calendar;
List<(string? Id, string[] FrontMatterLines)> collection = new();
Models.GenealogicalDataCommunication genealogicalDataCommunication;
GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines;
string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
@ -449,32 +480,13 @@ internal abstract class PersonContainer
frontMatterLines = MarkDown.GetFrontMatterLines(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication);
if (!frontMatterLines.Any())
continue;
allFrontMatterLines.Add("{");
frontMatterLastLine = frontMatterLines.Last();
foreach (string frontMatterLine in frontMatterLines)
{
segments = frontMatterLine.Split(": ");
if (segments.Length != 2)
continue;
if (frontMatterLine == frontMatterLastLine)
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)}");
else
allFrontMatterLines.Add($"\"{string.Join("\": ", segments)},");
}
allFrontMatterLines.Add("},");
collection.Add((genealogicalDataCommunication.Id, frontMatterLines.ToArray()));
MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatterLines);
}
if (allFrontMatterLines.Any())
if (collection.Any())
{
directory = Path.Combine(a2PeopleContentDirectory, $"{dateTime.Year}-Markdown", $"{dateTime.Year}-Week-{weekOfYear}", ticks.ToString());
if (!Directory.Exists(directory))
_ = Directory.CreateDirectory(directory);
json = $"[{string.Join(Environment.NewLine, allFrontMatterLines.ToArray(), 0, allFrontMatterLines.Count - 1)}{allFrontMatterLines.Last()[..^1]}]";
Models.GenealogicalDataCommunication[]? genealogicalDataCommunications = JsonSerializer.Deserialize<Models.GenealogicalDataCommunication[]>(json);
if (genealogicalDataCommunications is null)
throw new NullReferenceException(nameof(genealogicalDataCommunications));
json = JsonSerializer.Serialize(genealogicalDataCommunications, new JsonSerializerOptions { WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
_ = IPath.WriteAllText(Path.Combine(directory, $"{ticks}.json"), json, updateDateWhenMatches: false, compareBeforeWrite: true);
List<string[]> frontMatterLinesCollections = (from l in collection orderby l.Id.Length, l.Id where l.Id is not null select l.FrontMatterLines).ToList();
WriteAll(ticks, a2PeopleContentDirectory, frontMatterLinesCollections, weekOfYear);
}
}