generated from kzi-nastava/dotnet-wpf-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e788d07
commit db65ff7
Showing
1 changed file
with
30 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |