Datasets now serialize/save and deserialize/load properly. Created a test in the CLI project
This commit is contained in:
@@ -2,7 +2,7 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzDataObject
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public Dictionary<string, SzField> Fields = [];
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public Dictionary<string, SzField> Fields {get; set;} = [];
|
||||
}
|
||||
@@ -2,9 +2,9 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzDataObjectTemplate
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public Guid? Uuid;
|
||||
public required string DataObjectType;
|
||||
public Dictionary<string, SzTemplateField> TemplateFields = [];
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public Guid? Uuid {get; set;}
|
||||
public required string DataObjectType {get; set;}
|
||||
public Dictionary<string, SzTemplateField> TemplateFields {get; set;} = [];
|
||||
}
|
||||
@@ -2,11 +2,11 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzDataset
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public Guid? Uuid;
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public Guid? Uuid {get; set;}
|
||||
|
||||
public required string DataObjectType;
|
||||
public required string DataObjectTemplateId;
|
||||
public Dictionary<string, SzDataObject> DataObjects = [];
|
||||
public required string DataObjectType {get; set;}
|
||||
public required string DataObjectTemplateId {get; set;}
|
||||
public Dictionary<string, SzDataObject> DataObjects {get; set;} = [];
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzField
|
||||
{
|
||||
public required string Id;
|
||||
public required SzFieldType FieldType;
|
||||
public bool IsList = false;
|
||||
public string Value = "";
|
||||
public required string Id {get; set;}
|
||||
public required SzFieldType FieldType {get; set;}
|
||||
public bool IsList {get; set;} = false;
|
||||
public string Value {get; set;} = "";
|
||||
}
|
||||
@@ -2,10 +2,10 @@ namespace SzLib.DataObjects;
|
||||
|
||||
public class SzTemplateField
|
||||
{
|
||||
public required string Name;
|
||||
public required string Id;
|
||||
public bool IsList = false;
|
||||
public string DefaultValue = "";
|
||||
public bool IsSpecialType = false;
|
||||
public string SpecialTypeValue = "";
|
||||
public required string Name {get; set;}
|
||||
public required string Id {get; set;}
|
||||
public bool IsList {get; set;} = false;
|
||||
public string DefaultValue {get; set;} = "";
|
||||
public bool IsSpecialType {get; set;} = false;
|
||||
public string SpecialTypeValue {get; set;} = "";
|
||||
}
|
||||
@@ -2,5 +2,7 @@ namespace SzLib;
|
||||
|
||||
public interface ISzFileManager
|
||||
{
|
||||
public string DataPath {get; set;}
|
||||
public string DataPath {get;}
|
||||
|
||||
public bool SaveFile(string path, string fileContent);
|
||||
}
|
||||
@@ -3,37 +3,71 @@ using SzLib.DataObjects;
|
||||
|
||||
namespace SzLib;
|
||||
|
||||
public class SzParser
|
||||
public class SzParser(ISzFileManager szFileManager)
|
||||
{
|
||||
public readonly ISzFileManager FileManager;
|
||||
private readonly JsonSerializerOptions _jsonOptions = new() { WriteIndented = true };
|
||||
|
||||
private JsonSerializerOptions _jsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
};
|
||||
|
||||
public SzParser(ISzFileManager szFileManager)
|
||||
{
|
||||
FileManager = szFileManager;
|
||||
}
|
||||
|
||||
public (string result, SzParseError error) SerializeDatasetToJson(SzDataset dataset)
|
||||
public string SerializeDatasetToJson(SzDataset dataset)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jsonString = JsonSerializer.Serialize(dataset, _jsonOptions);
|
||||
return (jsonString, SzParseError.None);
|
||||
return jsonString;
|
||||
}
|
||||
catch (JsonException e)
|
||||
{
|
||||
return ("", SzParseError.CouldNotSerialize);
|
||||
throw new Exception("Parse Error: " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? DeserializeDataset(string jsonString)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = JsonSerializer.Deserialize<SzDataset>(jsonString, _jsonOptions);
|
||||
return result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception("Could not deserialize JSON to type SzDataset: " + e.Message, e);
|
||||
}
|
||||
}
|
||||
|
||||
public bool SaveDataset(SzDataset dataset)
|
||||
{
|
||||
var datasetDir = Path.Combine(szFileManager.DataPath, "datasets", dataset.Id);
|
||||
Directory.CreateDirectory(datasetDir);
|
||||
try
|
||||
{
|
||||
var json = SerializeDatasetToJson(dataset);
|
||||
File.WriteAllText(Path.Combine(datasetDir, "dataset.json"), json);
|
||||
return true;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("Error saving dataset: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? LoadDataset(string datasetId)
|
||||
{
|
||||
var datasetPath = Path.Combine(szFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||
if (!File.Exists(datasetPath))
|
||||
{
|
||||
Console.WriteLine("Dataset not found: " + datasetId);
|
||||
return null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return DeserializeDataset(File.ReadAllText(datasetPath));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Error loading dataset: {e.Message}");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum SzParseError
|
||||
{
|
||||
None,
|
||||
CouldNotSerialize,
|
||||
GenericError,
|
||||
}
|
||||
Reference in New Issue
Block a user