@@ -35,18 +35,30 @@ internal static bool EnableDevMode
35
35
36
36
private static Dictionary < string , object > Cfg = new Dictionary < string , object > ( ) ;
37
37
private static bool Loaded = false ;
38
+ private static readonly JsonSerializer serializer = new JsonSerializer
39
+ {
40
+ NullValueHandling = NullValueHandling . Ignore ,
41
+ MissingMemberHandling = MissingMemberHandling . Ignore ,
42
+ DefaultValueHandling = DefaultValueHandling . Ignore
43
+ } ;
38
44
39
- private static void Load ( )
45
+ private static void Load ( )
40
46
{
41
47
try
42
48
{
43
- if ( ! File . Exists ( ConfigPath ) ) File . WriteAllText ( ConfigPath , "{}" ) ;
44
- string text = File . ReadAllText ( ConfigPath ) ;
45
- Cfg = JsonConvert . DeserializeObject < Dictionary < string , object > > ( text ) ;
49
+ if ( ! File . Exists ( ConfigPath ) )
50
+ {
51
+ Save ( ) ;
52
+ }
53
+
54
+ using StreamReader sr = new StreamReader ( ConfigPath ) ;
55
+ using JsonReader reader = new JsonTextReader ( sr ) ;
56
+ Cfg = serializer . Deserialize < Dictionary < string , object > > ( reader ) ;
57
+
46
58
if ( Cfg == null )
47
59
{
48
- File . WriteAllText ( ConfigPath , "{}" ) ;
49
60
Cfg = new Dictionary < string , object > ( ) ;
61
+ Save ( ) ;
50
62
}
51
63
52
64
Loaded = true ;
@@ -62,8 +74,9 @@ private static void Save()
62
74
{
63
75
try
64
76
{
65
- string text = JsonConvert . SerializeObject ( Cfg , Formatting . Indented ) ;
66
- File . WriteAllText ( ConfigPath , text ) ;
77
+ using StreamWriter sw = new StreamWriter ( ConfigPath ) ;
78
+ using JsonWriter writer = new JsonTextWriter ( sw ) ;
79
+ serializer . Serialize ( writer , Cfg ) ;
67
80
}
68
81
catch ( Exception e )
69
82
{
@@ -74,7 +87,10 @@ private static void Save()
74
87
75
88
private static T Get < T > ( string field , T def = default )
76
89
{
77
- if ( ! Loaded ) Load ( ) ;
90
+ if ( ! Loaded )
91
+ {
92
+ Load ( ) ;
93
+ }
78
94
79
95
if ( ! Cfg . TryGetValue ( field , out object value ) )
80
96
return def ;
0 commit comments