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
// Install-Package Newtonsoft.Json | |
using Newtonsoft.Json; | |
public static void SerializeJson(this object obj, string filePath) | |
{ | |
string json = JsonConvert.SerializeObject(obj, Formatting.Indented); | |
using (StreamWriter sw = new StreamWriter(filePath)) | |
{ | |
sw.WriteLine(json); | |
} | |
} | |
public static T DeserializeJson<T>(this T obj, string jsonFilePath) | |
{ | |
string json = null; | |
using (StreamReader sr = new StreamReader(jsonFilePath)) | |
{ | |
json = sr.ReadToEnd(); | |
} | |
return JsonConvert.DeserializeObject<T>(json); | |
} |
No comments:
Post a Comment