9
9
using System . Text . Json ;
10
10
using System . IO ;
11
11
using System . Diagnostics . CodeAnalysis ;
12
+ using LightJson ;
13
+ using System . Collections ;
12
14
13
15
namespace cascadiumtool ;
14
16
15
- [ JsonSerializable ( typeof ( ICollection < string > ) ) ]
16
- [ JsonSerializable ( typeof ( string ) ) ]
17
- [ JsonSerializable ( typeof ( bool ) ) ]
18
- [ JsonSerializable ( typeof ( StaticCSSConverter ) ) ]
19
- [ JsonSerializable ( typeof ( Dictionary < string , string > ) ) ]
20
- [ JsonSerializable ( typeof ( JsonCssCompilerOptions ) ) ]
21
- internal partial class JsonCssCompilerOptionsSerializerContext : JsonSerializerContext
22
- {
23
- }
24
-
25
17
internal class JsonCssCompilerOptions
26
18
{
27
19
public ICollection < string > InputFiles { get ; set ; } = Array . Empty < string > ( ) ;
@@ -34,9 +26,9 @@ internal class JsonCssCompilerOptions
34
26
public string ? MergeOrderPriority { get ; set ; }
35
27
36
28
public IEnumerable < StaticCSSConverter > Converters { get ; set ; } = Array . Empty < StaticCSSConverter > ( ) ;
37
- public Dictionary < string , string > AtRulesRewrites { get ; set ; } = new Dictionary < string , string > ( ) ;
29
+ public IDictionary < string , string > AtRulesRewrites { get ; set ; } = new Dictionary < string , string > ( ) ;
38
30
public IEnumerable < string > Extensions { get ; set ; } = Array . Empty < string > ( ) ;
39
- public ICollection < string > ExcludePatterns { get ; set ; } = Array . Empty < string > ( ) ;
31
+ public IEnumerable < string > ExcludePatterns { get ; set ; } = Array . Empty < string > ( ) ;
40
32
41
33
public static JsonCssCompilerOptions Create ( string configFile )
42
34
{
@@ -49,16 +41,28 @@ public static JsonCssCompilerOptions Create(string configFile)
49
41
50
42
string contents = File . ReadAllText ( pathToConfigFile ) ;
51
43
52
- JsonCssCompilerOptions ? jsonConfig = JsonSerializer . Deserialize (
53
- contents ,
54
- typeof ( JsonCssCompilerOptions ) ,
55
- new JsonCssCompilerOptionsSerializerContext ( new JsonSerializerOptions ( )
56
- {
57
- PropertyNameCaseInsensitive = true ,
58
- ReadCommentHandling = JsonCommentHandling . Skip
59
- } ) ) as JsonCssCompilerOptions ;
44
+ JsonOptions . ThrowOnInvalidCast = true ;
45
+ JsonOptions . PropertyNameCaseInsensitive = true ;
46
+ JsonOptions . Mappers . Add ( new StaticCSSConverterMapper ( ) ) ;
47
+ JsonOptions . Mappers . Add ( new DictionaryMapper ( ) ) ;
48
+
49
+ JsonObject jsonObj = JsonValue . Parse ( contents ) . AsJsonObject ! ;
50
+ JsonCssCompilerOptions compilerConfig = new JsonCssCompilerOptions ( ) ;
60
51
61
- return jsonConfig ! ;
52
+ compilerConfig . InputFiles = new List < string > ( jsonObj [ "InputFiles" ] . MaybeNull ( ) ? . AsJsonArray ! . Select ( i => i . AsString ! ) ?? Array . Empty < string > ( ) ) ;
53
+ compilerConfig . InputDirectories = new List < string > ( jsonObj [ "InputDirectories" ] . MaybeNull ( ) ? . AsJsonArray ! . Select ( i => i . AsString ! ) ?? Array . Empty < string > ( ) ) ;
54
+ compilerConfig . OutputFile = jsonObj [ "OutputFile" ] . MaybeNull ( ) ? . AsString ;
55
+ compilerConfig . KeepNestingSpace = jsonObj [ "KeepNestingSpace" ] . MaybeNull ( ) ? . AsBoolean ;
56
+ compilerConfig . Pretty = jsonObj [ "pretty" ] . MaybeNull ( ) ? . AsBoolean ;
57
+ compilerConfig . UseVarShortcut = jsonObj [ "useVarShortcut" ] . MaybeNull ( ) ? . AsBoolean ;
58
+ compilerConfig . Merge = jsonObj [ "merge" ] . MaybeNull ( ) ? . AsString ;
59
+ compilerConfig . MergeOrderPriority = jsonObj [ "MergeOrderPriority" ] . MaybeNull ( ) ? . AsString ;
60
+ compilerConfig . Converters = jsonObj [ "Converters" ] . MaybeNull ( ) ? . AsJsonArray ! . EveryAs < StaticCSSConverter > ( ) ?? Array . Empty < StaticCSSConverter > ( ) ;
61
+ compilerConfig . AtRulesRewrites = jsonObj [ "AtRulesRewrites" ] . MaybeNull ( ) ? . As < IDictionary < string , string > > ( ) ?? new Dictionary < string , string > ( ) ;
62
+ compilerConfig . Extensions = jsonObj [ "Extensions" ] . MaybeNull ( ) ? . AsJsonArray ! . Select ( s => s . AsString ! ) ?? Array . Empty < string > ( ) ;
63
+ compilerConfig . ExcludePatterns = jsonObj [ "ExcludePatterns" ] . MaybeNull ( ) ? . AsJsonArray ! . Select ( s => s . AsString ! ) ?? Array . Empty < string > ( ) ;
64
+
65
+ return compilerConfig ;
62
66
}
63
67
64
68
[ DynamicDependency ( "MergeOption" ) ]
@@ -80,3 +84,54 @@ public void ApplyConfiguration(CascadiumOptions compilerOptions)
80
84
}
81
85
}
82
86
}
87
+
88
+ public class DictionaryMapper : JsonSerializerMapper
89
+ {
90
+ public override Boolean CanSerialize ( Type obj )
91
+ {
92
+ return obj == typeof ( IDictionary < string , string > ) ;
93
+ }
94
+
95
+ public override Object Deserialize ( JsonValue value )
96
+ {
97
+ var dict = new Dictionary < string , string > ( ) ;
98
+
99
+ value . EnsureType ( JsonValueType . Object ) ;
100
+ var obj = value . AsJsonObject ! ;
101
+
102
+ foreach ( var kvp in obj . Properties )
103
+ {
104
+ dict . Add ( kvp . Key , kvp . Value . AsString ! ) ;
105
+ }
106
+
107
+ return dict ;
108
+ }
109
+
110
+ public override JsonValue Serialize ( Object value )
111
+ {
112
+ throw new NotImplementedException ( ) ;
113
+ }
114
+ }
115
+
116
+ public class StaticCSSConverterMapper : JsonSerializerMapper
117
+ {
118
+ public override Boolean CanSerialize ( Type obj )
119
+ {
120
+ return obj == typeof ( StaticCSSConverter ) ;
121
+ }
122
+
123
+ public override Object Deserialize ( JsonValue value )
124
+ {
125
+ return new StaticCSSConverter ( )
126
+ {
127
+ ArgumentCount = ( int ) value [ "ArgumentCount" ] . AsNumber ,
128
+ MatchProperty = value [ "MatchProperty" ] . AsString ,
129
+ Output = value [ "Output" ] . As < IDictionary < string , string > > ( )
130
+ } ;
131
+ }
132
+
133
+ public override JsonValue Serialize ( Object value )
134
+ {
135
+ throw new NotImplementedException ( ) ;
136
+ }
137
+ }
0 commit comments