annotate MetroWpf/Stocks.Common/Core/FileSerializer.cs @ 63:536498832a79

Latest version before changing bindings to Listbox
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sun, 22 Apr 2012 13:33:42 +0100
parents 6109bc268b90
children
rev   line source
20
adminsh@apollo
parents:
diff changeset
1 using System;
adminsh@apollo
parents:
diff changeset
2 using System.Collections.Generic;
adminsh@apollo
parents:
diff changeset
3 using System.IO;
adminsh@apollo
parents:
diff changeset
4 using System.Runtime.Serialization;
adminsh@apollo
parents:
diff changeset
5 using System.Text;
adminsh@apollo
parents:
diff changeset
6 using Newtonsoft.Json;
adminsh@apollo
parents:
diff changeset
7
adminsh@apollo
parents:
diff changeset
8 namespace Stocks.Common.Core
adminsh@apollo
parents:
diff changeset
9 {
adminsh@apollo
parents:
diff changeset
10 public class FileSerializer
adminsh@apollo
parents:
diff changeset
11 {
adminsh@apollo
parents:
diff changeset
12 public void SerializeJson(string filename, object obj)
adminsh@apollo
parents:
diff changeset
13 {
adminsh@apollo
parents:
diff changeset
14 string json = JsonConvert.SerializeObject(obj);
adminsh@apollo
parents:
diff changeset
15 Serialize(filename, json);
adminsh@apollo
parents:
diff changeset
16 }
adminsh@apollo
parents:
diff changeset
17
adminsh@apollo
parents:
diff changeset
18 public void Serialize(string filename, string text)
adminsh@apollo
parents:
diff changeset
19 {
adminsh@apollo
parents:
diff changeset
20 using (StreamWriter writer = new StreamWriter(filename))
adminsh@apollo
parents:
diff changeset
21 { writer.Write(text); }
adminsh@apollo
parents:
diff changeset
22 }
adminsh@apollo
parents:
diff changeset
23
adminsh@apollo
parents:
diff changeset
24 public void Serialize(string filename, IFormatter formatter, object objectToSerialize)
adminsh@apollo
parents:
diff changeset
25 {
adminsh@apollo
parents:
diff changeset
26 using (Stream stream = File.Open(filename, FileMode.Create))
adminsh@apollo
parents:
diff changeset
27 formatter.Serialize(stream, objectToSerialize);
adminsh@apollo
parents:
diff changeset
28 }
adminsh@apollo
parents:
diff changeset
29
adminsh@apollo
parents:
diff changeset
30 public string Deserialize(string filename)
adminsh@apollo
parents:
diff changeset
31 {
adminsh@apollo
parents:
diff changeset
32 StringBuilder sb = new StringBuilder();
adminsh@apollo
parents:
diff changeset
33 using (StreamReader reader = new StreamReader(filename))
adminsh@apollo
parents:
diff changeset
34 if (reader != null)
adminsh@apollo
parents:
diff changeset
35 sb.AppendLine(reader.ReadToEnd());
adminsh@apollo
parents:
diff changeset
36
adminsh@apollo
parents:
diff changeset
37 return sb.ToString();
adminsh@apollo
parents:
diff changeset
38 }
adminsh@apollo
parents:
diff changeset
39
adminsh@apollo
parents:
diff changeset
40 public List<T> DeserializeJson<T>(string filename)
adminsh@apollo
parents:
diff changeset
41 {
adminsh@apollo
parents:
diff changeset
42 var json = Deserialize(filename);
adminsh@apollo
parents:
diff changeset
43 return JsonConvert.DeserializeObject<List<T>>(json);
adminsh@apollo
parents:
diff changeset
44 }
adminsh@apollo
parents:
diff changeset
45
adminsh@apollo
parents:
diff changeset
46 public T[] Deserialize<T>(string filename, IFormatter formatter, Type type)
adminsh@apollo
parents:
diff changeset
47 {
adminsh@apollo
parents:
diff changeset
48 using (Stream stream = File.Open(filename, FileMode.Open))
adminsh@apollo
parents:
diff changeset
49 {
adminsh@apollo
parents:
diff changeset
50 var items = (T[])formatter.Deserialize(stream);
adminsh@apollo
parents:
diff changeset
51 return items;
adminsh@apollo
parents:
diff changeset
52 }
adminsh@apollo
parents:
diff changeset
53 }
adminsh@apollo
parents:
diff changeset
54 }
adminsh@apollo
parents:
diff changeset
55 }