Sort and use JsonSerializerContext
This commit is contained in:
parent
4bbcf04c1d
commit
23e45671c3
@ -1,4 +1,5 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace View_by_Distance.Shared.Models;
|
namespace View_by_Distance.Shared.Models;
|
||||||
|
|
||||||
@ -17,8 +18,20 @@ public record GenealogicalDataCommunication(DateTime? Birth,
|
|||||||
|
|
||||||
public override string ToString()
|
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;
|
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
|
||||||
|
{
|
||||||
}
|
}
|
@ -14,6 +14,7 @@ internal abstract class MarkDown
|
|||||||
{
|
{
|
||||||
List<string> results = new();
|
List<string> results = new();
|
||||||
string afterTrim;
|
string afterTrim;
|
||||||
|
string[] segments;
|
||||||
string[] jsonLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine);
|
string[] jsonLines = genealogicalDataCommunication.ToString().Split(Environment.NewLine);
|
||||||
foreach (string jsonLine in jsonLines)
|
foreach (string jsonLine in jsonLines)
|
||||||
{
|
{
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
namespace View_by_Distance.Shared.Models.Stateless.Methods;
|
||||||
@ -394,25 +393,57 @@ internal abstract class PersonContainer
|
|||||||
return new(results);
|
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)
|
internal static void MaybeWriteMarkDownFiles(string mappingDefaultName, string personBirthdayFormat, long ticks, List<GenealogicalDataCommunicationRelation> genealogicalDataCommunicationRelations, List<Models.PersonContainer> personContainers, string a2PeopleContentDirectory)
|
||||||
{
|
{
|
||||||
bool? male;
|
bool? male;
|
||||||
bool? first;
|
bool? first;
|
||||||
string json;
|
|
||||||
string fullName;
|
string fullName;
|
||||||
string[] matches;
|
string[] matches;
|
||||||
string[] segments;
|
|
||||||
string? directory;
|
string? directory;
|
||||||
bool isDefaultName;
|
bool isDefaultName;
|
||||||
const int zero = 0;
|
const int zero = 0;
|
||||||
string personKeyFormatted;
|
string personKeyFormatted;
|
||||||
string frontMatterLastLine;
|
|
||||||
string lowerHyphenFullName;
|
string lowerHyphenFullName;
|
||||||
List<string> frontMatterLines;
|
List<string> frontMatterLines;
|
||||||
string pattern = "[^a-z0-9-]";
|
string pattern = "[^a-z0-9-]";
|
||||||
DateTime dateTime = new(ticks);
|
DateTime dateTime = new(ticks);
|
||||||
List<string> allFrontMatterLines = new();
|
|
||||||
Calendar calendar = new CultureInfo("en-US").Calendar;
|
Calendar calendar = new CultureInfo("en-US").Calendar;
|
||||||
|
List<(string? Id, string[] FrontMatterLines)> collection = new();
|
||||||
Models.GenealogicalDataCommunication genealogicalDataCommunication;
|
Models.GenealogicalDataCommunication genealogicalDataCommunication;
|
||||||
GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines;
|
GenealogicalDataCommunicationLines? genealogicalDataCommunicationLines;
|
||||||
string weekOfYear = calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
|
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);
|
frontMatterLines = MarkDown.GetFrontMatterLines(ticks, fullName, lowerHyphenFullName, genealogicalDataCommunication);
|
||||||
if (!frontMatterLines.Any())
|
if (!frontMatterLines.Any())
|
||||||
continue;
|
continue;
|
||||||
allFrontMatterLines.Add("{");
|
collection.Add((genealogicalDataCommunication.Id, frontMatterLines.ToArray()));
|
||||||
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("},");
|
|
||||||
MarkDown.WriteFile(personKeyFormatted, ticks, genealogicalDataCommunicationRelations, a2PeopleContentDirectory, calendar, pattern, personKeyFormattedToPersonFullName, familyIndexToCollection, genealogicalDataCommunication, fullName, lowerHyphenFullName, frontMatterLines);
|
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());
|
List<string[]> frontMatterLinesCollections = (from l in collection orderby l.Id.Length, l.Id where l.Id is not null select l.FrontMatterLines).ToList();
|
||||||
if (!Directory.Exists(directory))
|
WriteAll(ticks, a2PeopleContentDirectory, frontMatterLinesCollections, weekOfYear);
|
||||||
_ = 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user