What's the easiest way to serialize and de-serialize an List<(string,string)>? , something that can be stored into an string column in an database table.
You can use JSON. Firstly you need to download the Newtonsoft.Json package. Visual studio > Tools > NuGet Package Manager > Package Manager Console. Next, paste Install-Package Newtonsoft.Json -Version 13.0.1 to console. Next, create FileHelper class and use this code.
Package link: https://www.nuget.org/packages/Newtonsoft.Json/
using Newtonsoft.Json;
using System.IO;
namespace Json
{
static class FileHelper
{
public static void JsonSerialize(object obj, string fileName)
{
using (var sw = new StreamWriter($"{fileName}.json"))
{
using (var jw = new JsonTextWriter(sw))
{
var serializer = new JsonSerializer();
jw.Formatting = Newtonsoft.Json.Formatting.Indented;
serializer.Serialize(jw, obj);
}
}
}
public static void JsonDeserialize(out object obj, string fileName)
{
using (var sr = new StreamReader($"{fileName}.json"))
{
using (var jr = new JsonTextReader(sr))
{
var serializer = new JsonSerializer();
try
{
obj = serializer.Deserialize<object>(jr);
}
catch
{
obj = null;
}
}
}
}
}
}