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.
Merge pull request #7 from kzi-nastava/feat/Serializer
Add Repository
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 deletions.
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |