Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- namespace Lightpoint_demo2
- {
- internal class JsonConverter: ISerializer<Office>
- {
- private const string filename = "cars.json";
- private List<Office> Offices { get; set; }
- public JsonConverter(List<Office> offices)
- {
- Offices = offices;
- }
- public void Serialize()
- {
- using (FileStream fs = new FileStream(filename, FileMode.Create))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- {
- List<SerializableOffice> s_office_list = new List<SerializableOffice>();
- string s = "";
- for (int i = 0; i < Offices.Count; i++)
- {
- Offices[i].SerialazibleOffice.Brand = Offices[i].Brand;
- Offices[i].SerialazibleOffice.SerializableCarsList = Offices[i].CarsDataBase.GetItemsList();
- s_office_list.Add(Offices[i].SerialazibleOffice);
- }
- s = JsonSerializer.Serialize(s_office_list);
- sw.Write(s);
- }
- }
- }
- public void Deserialize()
- {
- List<SerializableOffice> s_office_list;
- Office office;
- using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
- {
- byte[] array = new byte[fs.Length];
- fs.Read(array, 0, array.Length);
- string jstring = Encoding.Default.GetString(array);
- try
- {
- s_office_list = JsonSerializer.Deserialize<List<SerializableOffice>>(jstring);
- }
- catch (Exception)
- {
- return;
- }
- for (int i = 0; i < s_office_list.Count; i++)
- {
- office = new Office(s_office_list[i].Brand, "JSON");
- office.CarsDataBase = new LocalDB();
- for (int j = 0; j < s_office_list[i].SerializableCarsList.Count; j++)
- {
- office.CarsDataBase.Create(s_office_list[i].SerializableCarsList[j]);
- }
- Offices.Add(office);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement