Skip to content

Commit

Permalink
[Add] Implementation of Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
natasakasikovic committed Mar 28, 2024
1 parent e788d07 commit db65ff7
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion LangLang/Core/Repository/Serialization/Serializer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace LangLang.Core.Repository.Serialization
{
internal class Serializer
class Serializer<T> where T : ISerializable, new()
{
private const char Delimiter = '|';

public string ToCSV(List<T> objects)
{
StringBuilder sb = new StringBuilder();

foreach (ISerializable obj in objects)
{
string line = string.Join(Delimiter.ToString(), obj.ToCSV());
sb.AppendLine(line);
}

return sb.ToString();
}

public List<T> FromCSV(IEnumerable<string> lines)
{
List<T> objects = new List<T>();

foreach (string line in lines)
{
string[] csvValues = line.Split(Delimiter);
T obj = new T();
obj.FromCSV(csvValues);
objects.Add(obj);
}

return objects;
}
}
}

0 comments on commit db65ff7

Please sign in to comment.