Advertisement
hubert17

JSON Generics CRUD Read Write To File

Feb 13th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using Microsoft.AspNetCore.Hosting;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7.    
  8.     public interface IJsonFile
  9.     {
  10.         bool Write<T>(T obj);
  11.         bool WriteAll<T>(List<T> objList);
  12.         bool UpdateAll<T>(List<T> objList, bool confirmed = false);
  13.         IList<T> ReadAll<T>();
  14.         bool DeleteAll<T>(bool confirmed = false);
  15.     }
  16.     public class JsonFile: IJsonFile
  17.     {
  18.         private IHostingEnvironment _env;
  19.         private string folder;
  20.  
  21.         public JsonFile(IHostingEnvironment env)
  22.         {
  23.             _env = env;
  24.             folder = Path.Combine(_env.ContentRootPath, @"Data\");
  25.         }
  26.  
  27.         bool IJsonFile.Write<T>(T obj)
  28.         {
  29.             try
  30.             {
  31.                 string filePath = folder + typeof(T).Name + ".json";
  32.                 string json;
  33.                 using (var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
  34.                 {
  35.                     using (StreamReader sr = new StreamReader(fileStream))
  36.                     {
  37.                         json = sr.ReadToEnd();
  38.                     }
  39.                 }
  40.  
  41.                 var jdata = JsonConvert.DeserializeObject<List<T>>(json);
  42.                 var data = (List<T>)Activator.CreateInstance(typeof(List<T>));
  43.                 if (jdata == null || jdata.Count == 0)
  44.                     data.Add(obj);
  45.                 else if (jdata.Count > 0)
  46.                 {
  47.                     jdata.Add(obj);
  48.                     data = jdata;
  49.                 }
  50.  
  51.                 File.WriteAllText(filePath, JsonConvert.SerializeObject(data));
  52.                 return true;
  53.             }
  54.             catch
  55.             {
  56.                 return false;
  57.             }
  58.         }
  59.  
  60.         bool IJsonFile.WriteAll<T>(List<T> objList)
  61.         {
  62.             try
  63.             {
  64.                 string filePath = folder + typeof(T).Name + ".json";
  65.                 string json;
  66.                 using (var fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
  67.                 {
  68.                     using (StreamReader sr = new StreamReader(fileStream))
  69.                     {
  70.                         json = sr.ReadToEnd();
  71.                     }
  72.                 }
  73.  
  74.                 var jdata = JsonConvert.DeserializeObject<List<T>>(json);
  75.                 var data = (List<T>)Activator.CreateInstance(typeof(List<T>));
  76.                 if (jdata == null || jdata.Count == 0)
  77.                     data.AddRange(objList);
  78.                 else if (jdata.Count > 0)
  79.                     data = jdata.Concat(objList).ToList();
  80.  
  81.                 File.WriteAllText(filePath, JsonConvert.SerializeObject(data));
  82.  
  83.                 return true;
  84.             }
  85.             catch
  86.             {
  87.                 return false;
  88.             }
  89.  
  90.         }
  91.  
  92.         bool IJsonFile.UpdateAll<T>(List<T> objList, bool confirmed)
  93.         {
  94.             if(confirmed)
  95.             {
  96.                 try
  97.                 {
  98.                     string filePath = folder + typeof(T).Name + ".json";
  99.                     File.WriteAllText(filePath, JsonConvert.SerializeObject(objList));
  100.                     return true;
  101.                 }
  102.                 catch
  103.                 { }
  104.             }
  105.             return false;
  106.         }
  107.  
  108.         IList<T> IJsonFile.ReadAll<T>()
  109.         {
  110.             try
  111.             {
  112.                 string filePath = folder + typeof(T).Name + ".json"; ;
  113.                 using (var fileStream = File.Open(filePath, FileMode.OpenOrCreate))
  114.                 {
  115.                     using (StreamReader sr = new StreamReader(fileStream))
  116.                     {
  117.                         var json = sr.ReadToEnd();
  118.                         var data = JsonConvert.DeserializeObject<List<T>>(json);
  119.                         return data;
  120.                     }
  121.                 }
  122.             }
  123.             catch
  124.             {
  125.                 return null;
  126.             }
  127.         }
  128.  
  129.         bool IJsonFile.DeleteAll<T>(bool confirmed)
  130.         {
  131.             if(confirmed)
  132.             {
  133.                 try
  134.                 {
  135.                     string filePath = folder + typeof(T).Name + ".json";
  136.                     if (File.Exists(filePath))
  137.                     {
  138.                         File.Delete(filePath);
  139.                     }
  140.                     return true;
  141.                 }
  142.                 catch { }
  143.             }
  144.  
  145.             return false;
  146.         }
  147.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement