Skip to content

Commit

Permalink
Merge pull request #7 from kzi-nastava/feat/Serializer
Browse files Browse the repository at this point in the history
Add Repository
  • Loading branch information
anasinik authored Mar 28, 2024
2 parents ffc2552 + db65ff7 commit 84e3492
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
14 changes: 14 additions & 0 deletions LangLang/Core/Repository/Serialization/Serializable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LangLang.Core.Repository.Serialization
{
public interface ISerializable
{
void FromCSV(string[] values);
string[] ToCSV();
}
}
42 changes: 42 additions & 0 deletions LangLang/Core/Repository/Serialization/Serializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +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
{
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 84e3492

Please sign in to comment.