1+ // Copyright 2015 Destructurama Contributors
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ using System . Collections . Generic ;
16+ using System . Diagnostics . CodeAnalysis ;
17+ using System . Linq ;
18+ using Newtonsoft . Json . Linq ;
19+ using Serilog . Core ;
20+ using Serilog . Events ;
21+
22+ namespace SeqCli . Util ;
23+
24+ sealed class JsonNetDestructuringPolicy : IDestructuringPolicy
25+ {
26+ public bool TryDestructure ( object value , ILogEventPropertyValueFactory propertyValueFactory , [ NotNullWhen ( true ) ] out LogEventPropertyValue ? result )
27+ {
28+ switch ( value )
29+ {
30+ case JObject jo :
31+ result = Destructure ( jo , propertyValueFactory ) ;
32+ return true ;
33+ case JArray ja :
34+ result = Destructure ( ja , propertyValueFactory ) ;
35+ return true ;
36+ case JValue jv :
37+ result = Destructure ( jv , propertyValueFactory ) ;
38+ return true ;
39+ }
40+
41+ result = null ;
42+ return false ;
43+ }
44+
45+ static LogEventPropertyValue Destructure ( JValue jv , ILogEventPropertyValueFactory propertyValueFactory )
46+ {
47+ return propertyValueFactory . CreatePropertyValue ( jv . Value ! , destructureObjects : true ) ;
48+ }
49+
50+ static SequenceValue Destructure ( JArray ja , ILogEventPropertyValueFactory propertyValueFactory )
51+ {
52+ var elems = ja . Select ( t => propertyValueFactory . CreatePropertyValue ( t , destructureObjects : true ) ) ;
53+ return new SequenceValue ( elems ) ;
54+ }
55+
56+ static LogEventPropertyValue Destructure ( JObject jo , ILogEventPropertyValueFactory propertyValueFactory )
57+ {
58+ string ? typeTag = null ;
59+ var props = new List < LogEventProperty > ( jo . Count ) ;
60+
61+ foreach ( var prop in jo . Properties ( ) )
62+ {
63+ if ( prop . Name == "$type" )
64+ {
65+ if ( prop . Value is JValue typeVal && typeVal . Value is string v )
66+ {
67+ typeTag = v ;
68+ continue ;
69+ }
70+ }
71+ else if ( ! LogEventProperty . IsValidName ( prop . Name ) )
72+ {
73+ return DestructureToDictionaryValue ( jo , propertyValueFactory ) ;
74+ }
75+
76+ props . Add ( new LogEventProperty ( prop . Name , propertyValueFactory . CreatePropertyValue ( prop . Value , destructureObjects : true ) ) ) ;
77+ }
78+
79+ return new StructureValue ( props , typeTag ) ;
80+ }
81+
82+ static DictionaryValue DestructureToDictionaryValue ( JObject jo , ILogEventPropertyValueFactory propertyValueFactory )
83+ {
84+ var elements = jo . Properties ( ) . Select (
85+ prop => new KeyValuePair < ScalarValue , LogEventPropertyValue > (
86+ new ScalarValue ( prop . Name ) ,
87+ propertyValueFactory . CreatePropertyValue ( prop . Value , destructureObjects : true ) )
88+ ) ;
89+ return new DictionaryValue ( elements ) ;
90+ }
91+ }
0 commit comments