Implemented the JSON export
This commit is contained in:
6 files changed
+151
-2
No files matched your search
@@ -1,4 +1,6 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DataModel.Database.Common;
|
||||
|
||||
@@ -63,4 +65,97 @@ public sealed class DataContext : DbContext, IDataContext
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region Export and import data structures
|
||||
|
||||
private readonly record struct JsonData(
|
||||
IEnumerable<JsonSetting> Settings,
|
||||
IEnumerable<JsonSection> Sections,
|
||||
IEnumerable<JsonTextElement> TextElements,
|
||||
IEnumerable<JsonTranslation> Translations
|
||||
);
|
||||
|
||||
internal readonly record struct JsonUniqueId(string Code, Guid UniqueId, string Prefix = "")
|
||||
{
|
||||
public override string ToString() => string.IsNullOrWhiteSpace(this.Prefix) ? $"{this.Code}::{this.UniqueId}" : $"{this.Prefix}::{this.Code}::{this.UniqueId}";
|
||||
|
||||
public static implicit operator string(JsonUniqueId id) => id.ToString();
|
||||
}
|
||||
|
||||
private sealed class JsonUniqueIdConverter : JsonConverter<JsonUniqueId>
|
||||
{
|
||||
public override JsonUniqueId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
var json = reader.GetString();
|
||||
var parts = json?.Split("::");
|
||||
return parts?.Length switch
|
||||
{
|
||||
2 => new JsonUniqueId(parts[0], Guid.Parse(parts[1])),
|
||||
3 => new JsonUniqueId(parts[1], Guid.Parse(parts[2]), parts[0]),
|
||||
|
||||
_ => throw new JsonException($"Invalid format of JsonUniqueId: {json}")
|
||||
};
|
||||
}
|
||||
|
||||
public override void Write(Utf8JsonWriter writer, JsonUniqueId value, JsonSerializerOptions options)
|
||||
{
|
||||
writer.WriteStringValue(value);
|
||||
}
|
||||
}
|
||||
|
||||
internal readonly record struct JsonSetting(
|
||||
JsonUniqueId UniqueId,
|
||||
string Code,
|
||||
string TextValue,
|
||||
int IntegerValue,
|
||||
bool BoolValue,
|
||||
Guid GuidValue
|
||||
);
|
||||
|
||||
internal readonly record struct JsonSection(
|
||||
JsonUniqueId UniqueId,
|
||||
string Name,
|
||||
string DataKey,
|
||||
int Depth,
|
||||
JsonUniqueId ParentUniqueId,
|
||||
List<JsonUniqueId> TextElements
|
||||
);
|
||||
|
||||
internal readonly record struct JsonTextElement(
|
||||
JsonUniqueId UniqueId,
|
||||
string Code,
|
||||
string Name,
|
||||
bool IsMultiLine,
|
||||
JsonUniqueId SectionUniqueId,
|
||||
List<JsonUniqueId> Translations
|
||||
);
|
||||
|
||||
internal readonly record struct JsonTranslation(
|
||||
JsonUniqueId UniqueId,
|
||||
string Culture,
|
||||
string Text,
|
||||
bool TranslateManual,
|
||||
JsonUniqueId TextElementUniqueId
|
||||
);
|
||||
|
||||
#endregion
|
||||
|
||||
public async Task ExportAsync(string path)
|
||||
{
|
||||
var jsonSettings = new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
Converters = { new JsonUniqueIdConverter() },
|
||||
};
|
||||
|
||||
await using var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
|
||||
await JsonSerializer.SerializeAsync(fileStream,
|
||||
new JsonData
|
||||
{
|
||||
Settings = this.Settings.Select(n => n.ToJsonSetting()),
|
||||
Sections = this.Sections.Select(n => n.ToJsonSection()),
|
||||
TextElements = this.TextElements.Select(n => n.ToJsonTextElement()),
|
||||
Translations = this.Translations.Select(n => n.ToJsonTranslation()),
|
||||
}, jsonSettings);
|
||||
}
|
||||
}
|
||||
@@ -11,4 +11,6 @@ public interface IDataContext
|
||||
public DbSet<TextElement> TextElements { get; set; }
|
||||
|
||||
public DbSet<Translation> Translations { get; set; }
|
||||
|
||||
public Task ExportAsync(string path);
|
||||
}
|
||||
Reference in new issue
Block a user