Advertisement
MaksNew

Untitled

Jan 19th, 2022
1,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7.  
  8. namespace Lightpoint_demo2
  9. {
  10.     internal class JsonConverter: ISerializer<Office>
  11.     {
  12.         private const string filename = "cars.json";
  13.         private List<Office> Offices { get; set; }
  14.         public JsonConverter(List<Office> offices)
  15.         {
  16.             Offices = offices;
  17.         }
  18.         public void Serialize()
  19.         {
  20.             using (FileStream fs = new FileStream(filename, FileMode.Create))
  21.             {
  22.                 using (StreamWriter sw = new StreamWriter(fs))
  23.                 {
  24.                     List<SerializableOffice> s_office_list = new List<SerializableOffice>();
  25.                     string s = "";
  26.                     for (int i = 0; i < Offices.Count; i++)
  27.                     {
  28.                         Offices[i].SerialazibleOffice.Brand = Offices[i].Brand;
  29.                         Offices[i].SerialazibleOffice.SerializableCarsList = Offices[i].CarsDataBase.GetItemsList();
  30.                         s_office_list.Add(Offices[i].SerialazibleOffice);
  31.                     }
  32.                     s = JsonSerializer.Serialize(s_office_list);
  33.                     sw.Write(s);
  34.                 }
  35.             }
  36.         }
  37.  
  38.         public void Deserialize()
  39.         {
  40.             List<SerializableOffice> s_office_list;
  41.             Office office;    
  42.             using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
  43.             {
  44.                 byte[] array = new byte[fs.Length];
  45.                 fs.Read(array, 0, array.Length);
  46.                 string jstring = Encoding.Default.GetString(array);
  47.                 try
  48.                 {
  49.                     s_office_list = JsonSerializer.Deserialize<List<SerializableOffice>>(jstring);
  50.                 }
  51.                 catch (Exception)
  52.                 {
  53.                     return;
  54.                 }
  55.                 for (int i = 0; i < s_office_list.Count; i++)
  56.                 {
  57.                     office = new Office(s_office_list[i].Brand, "JSON");
  58.                     office.CarsDataBase = new LocalDB();
  59.                     for (int j = 0; j < s_office_list[i].SerializableCarsList.Count; j++)
  60.                     {
  61.                         office.CarsDataBase.Create(s_office_list[i].SerializableCarsList[j]);
  62.                     }
  63.                     Offices.Add(office);
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement