7
7
// File name: JsonConfigParser.cs
8
8
// Repository: https://github.com/sisk-http/core
9
9
10
+ using System . Text . Json ;
10
11
using System . Text . Json . Nodes ;
11
12
using System . Text . Json . Serialization ;
12
13
using Sisk . Core . Http ;
13
14
using Sisk . Core . Http . Hosting ;
14
15
15
16
namespace Sisk . Core . Internal . ServiceProvider {
16
17
internal sealed class JsonConfigParser : IConfigurationReader {
18
+
17
19
public void ReadConfiguration ( ConfigurationContext prov ) {
18
20
string filename = prov . ConfigurationFile ;
19
-
20
21
string fileContents = File . ReadAllText ( filename ) ;
21
22
22
- if ( System . Text . Json . JsonSerializer . Deserialize ( fileContents , typeof ( ConfigStructureFile ) ,
23
- new SourceGenerationContext ( new System . Text . Json . JsonSerializerOptions ( ) {
24
- AllowTrailingCommas = true ,
25
- PropertyNameCaseInsensitive = true ,
26
- ReadCommentHandling = System . Text . Json . JsonCommentHandling . Skip
27
- } ) ) is not ConfigStructureFile config ) {
23
+ if ( JsonSerializer . Deserialize < ConfigStructureFile > ( fileContents , JsonConfigJsonSerializer . Default . ConfigStructureFile )
24
+ is not { } config ) {
28
25
29
26
throw new InvalidOperationException ( SR . Provider_ConfigParser_ConfigFileInvalid ) ;
30
27
}
@@ -57,7 +54,7 @@ public void ReadConfiguration ( ConfigurationContext prov ) {
57
54
58
55
if ( config . Parameters != null ) {
59
56
foreach ( var prop in config . Parameters ) {
60
- prov . Host . Parameters . Add ( prop . Key , prop . Value ? . AsValue ( ) . GetValue < object > ( ) . ToString ( ) ) ;
57
+ prov . Host . Parameters . Add ( prop . Key , prop . Value ) ;
61
58
}
62
59
prov . Host . Parameters . MakeReadonly ( ) ;
63
60
}
@@ -90,25 +87,32 @@ public void ReadConfiguration ( ConfigurationContext prov ) {
90
87
}
91
88
}
92
89
90
+ [ JsonSourceGenerationOptions ( AllowTrailingCommas = true ,
91
+ ReadCommentHandling = System . Text . Json . JsonCommentHandling . Skip ,
92
+ DictionaryKeyPolicy = JsonKnownNamingPolicy . Unspecified ) ]
93
93
[ JsonSerializable ( typeof ( JsonObject ) ) ]
94
+ [ JsonSerializable ( typeof ( ConfigStructureFile ) ) ]
94
95
[ JsonSerializable ( typeof ( ConfigStructureFile__ServerConfiguration ) ) ]
95
96
[ JsonSerializable ( typeof ( ConfigStructureFile__ListeningHost ) ) ]
96
- internal partial class ConfigStructureFile : JsonSerializerContext {
97
+ [ JsonSerializable ( typeof ( ConfigStructureFile__ListeningHost__CrossOriginResourceSharingPolicy ) ) ]
98
+ internal sealed partial class JsonConfigJsonSerializer : JsonSerializerContext {
99
+ }
100
+
101
+ internal sealed class ConfigStructureFile {
97
102
public ConfigStructureFile__ServerConfiguration ? Server { get ; set ; } = null ! ;
98
103
public ConfigStructureFile__ListeningHost ? ListeningHost { get ; set ; } = null ! ;
99
- public JsonObject ? Parameters { get ; set ; }
104
+ public Dictionary < string , string > ? Parameters { get ; set ; }
100
105
}
101
106
102
- internal class ConfigStructureFile__ServerConfiguration {
107
+ internal sealed class ConfigStructureFile__ServerConfiguration {
103
108
public string ? AccessLogsStream { get ; set ; } = "console" ;
104
109
public string ? ErrorsLogsStream { get ; set ; }
105
110
public int MaximumContentLength { get ; set ; }
106
111
public bool IncludeRequestIdHeader { get ; set ; }
107
112
public bool ThrowExceptions { get ; set ; } = true ;
108
113
}
109
114
110
- [ JsonSerializable ( typeof ( string [ ] ) ) ]
111
- internal sealed partial class ConfigStructureFile__ListeningHost__CrossOriginResourceSharingPolicy : JsonSerializerContext {
115
+ internal sealed class ConfigStructureFile__ListeningHost__CrossOriginResourceSharingPolicy {
112
116
public bool ? AllowCredentials { get ; set ; }
113
117
public string [ ] ? ExposeHeaders { get ; set ; }
114
118
public string ? AllowOrigin { get ; set ; }
@@ -118,16 +122,10 @@ internal sealed partial class ConfigStructureFile__ListeningHost__CrossOriginRes
118
122
public int ? MaxAge { get ; set ; }
119
123
}
120
124
121
- [ JsonSerializable ( typeof ( ConfigStructureFile__ListeningHost__CrossOriginResourceSharingPolicy ) ) ]
122
- internal sealed partial class ConfigStructureFile__ListeningHost : JsonSerializerContext {
125
+ internal sealed class ConfigStructureFile__ListeningHost {
123
126
public string ? Label { get ; set ; }
124
127
public string [ ] ? Ports { get ; set ; }
125
128
126
129
public ConfigStructureFile__ListeningHost__CrossOriginResourceSharingPolicy ? CrossOriginResourceSharingPolicy { get ; set ; }
127
130
}
128
-
129
- [ JsonSourceGenerationOptions ( WriteIndented = true ) ]
130
- [ JsonSerializable ( typeof ( ConfigStructureFile ) ) ]
131
- internal sealed partial class SourceGenerationContext : JsonSerializerContext {
132
- }
133
131
}
0 commit comments