64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Converters;
|
|
using SzCore.DataObjects;
|
|
|
|
namespace SzCore;
|
|
|
|
public class SzParser
|
|
{
|
|
private readonly JsonSerializerSettings _jsonSettings = new()
|
|
{
|
|
Formatting = Formatting.Indented,
|
|
Converters = { new StringEnumConverter() }
|
|
};
|
|
|
|
public string SerializeDatasetToJson(SzDataset dataset)
|
|
{
|
|
try
|
|
{
|
|
return JsonConvert.SerializeObject(dataset, _jsonSettings);
|
|
}
|
|
catch (JsonException e)
|
|
{
|
|
throw new Exception("Dataset Serialization Error: " + e.Message);
|
|
}
|
|
}
|
|
|
|
public SzDataset? DeserializeDataset(string jsonString)
|
|
{
|
|
try
|
|
{
|
|
return JsonConvert.DeserializeObject<SzDataset>(jsonString, _jsonSettings);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SZ.Logger.LogError("Could not deserialize JSON to type SzDataset: " + e.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public string SerializeTemplateToJson(ISzTemplate template)
|
|
{
|
|
try
|
|
{
|
|
return JsonConvert.SerializeObject(template, _jsonSettings);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new Exception("Template Serialization Error: " + e.Message);
|
|
}
|
|
}
|
|
|
|
public T? DeserializeTemplate<T>(string jsonString)
|
|
{
|
|
try
|
|
{
|
|
return JsonConvert.DeserializeObject<T>(jsonString, _jsonSettings);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
SZ.Logger.LogError($"Could not deserialize JSON to type {typeof(T)}: " + e.Message);
|
|
return default;
|
|
}
|
|
}
|
|
} |