60 lines
1.8 KiB
C#
60 lines
1.8 KiB
C#
using NetDocsForLLM.Models;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.IO;
|
|
|
|
namespace NetDocsForLLM.Helpers
|
|
{
|
|
public static class JsonHelper
|
|
{
|
|
public static string SerializeToJson(DocumentationModel documentation, bool indented = true)
|
|
{
|
|
try
|
|
{
|
|
var settings = new JsonSerializerSettings
|
|
{
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
};
|
|
|
|
return JsonConvert.SerializeObject(documentation,
|
|
indented ? Formatting.Indented : Formatting.None,
|
|
settings);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new InvalidOperationException($"Error al serializar a JSON: {ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
public static void SaveToJsonFile(DocumentationModel documentation, string filePath)
|
|
{
|
|
try
|
|
{
|
|
var json = SerializeToJson(documentation);
|
|
File.WriteAllText(filePath, json);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new InvalidOperationException($"Error al guardar archivo JSON: {ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
public static DocumentationModel DeserializeFromJson(string json)
|
|
{
|
|
try
|
|
{
|
|
return JsonConvert.DeserializeObject<DocumentationModel>(json,
|
|
new JsonSerializerSettings
|
|
{
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new InvalidOperationException($"Error al deserializar desde JSON: {ex.Message}", ex);
|
|
}
|
|
}
|
|
}
|
|
}
|