-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadJsonFromFile.cs
executable file
·113 lines (78 loc) · 2.92 KB
/
ReadJsonFromFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
////////////////////////////////////////////////////////////////////////////
// Copyright 2017 : Vladimir Novick https://www.linkedin.com/in/vladimirnovick/
//
// https://github.com/Vladimir-Novick/CSharp-JSON
//
// NO WARRANTIES ARE EXTENDED. USE AT YOUR OWN RISK.
//
// To contact the author with suggestions or comments, use :vlad.novick@gmail.com
//
////////////////////////////////////////////////////////////////////////////
namespace SGcombo.JsonUtils
{
public class CorrectionData
{
public string source { get; private set; }
public string destination { get; private set; }
public CorrectionData(string _source,string _destination)
{
source = _source;
destination = _destination;
}
}
public T ReadObjectFromJsonFile(String Filepath, List<CorrectionData> dataCorrection = null, Boolean debug = false)
{
string json = string.Empty;
T value;
using (StreamReader read = new StreamReader(Filepath))
{
json = read.ReadToEnd();
if (dataCorrection != null)
{
foreach (CorrectionData item in dataCorrection)
{
json = Regex.Replace(json, item.source, item.destination);
}
}
if (debug)
{
File.WriteAllText(Filepath + ".debug", json);
}
value = (T)JsonConvert.DeserializeObject(json, typeof(T));
}
return value;
}
public class ReadJsonFromFile<T>
{
public List<T> ReadListObjectJsonFile(String Filepath, List<CorrectionData> dataCorrection = null, Boolean debug = false)
{
string json = string.Empty;
List<T> value = new List<T>();
Type dataType = value.GetType();
using (StreamReader read = new StreamReader(Filepath))
{
json = read.ReadToEnd();
if (dataCorrection != null)
{
foreach (CorrectionData item in dataCorrection)
{
json = Regex.Replace(json,item.source, item.destination);
}
}
if (debug)
{
File.WriteAllText(Filepath + ".debug", json);
}
value = (List<T>)JsonConvert.DeserializeObject(json, dataType);
}
return value;
}
}
}