|
20 | 20 |
|
21 | 21 | namespace Destructurama.JsonNet
|
22 | 22 | {
|
23 |
| - internal class JsonNetDestructuringPolicy : IDestructuringPolicy |
24 |
| - { |
25 |
| - public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result) |
26 |
| - { |
27 |
| - switch (value) |
28 |
| - { |
29 |
| - case JObject jo: |
30 |
| - result = Destructure(jo, propertyValueFactory); |
31 |
| - return true; |
32 |
| - case JArray ja: |
33 |
| - result = Destructure(ja, propertyValueFactory); |
34 |
| - return true; |
35 |
| - case JValue jv: |
36 |
| - result = Destructure(jv, propertyValueFactory); |
37 |
| - return true; |
38 |
| - } |
| 23 | + internal class JsonNetDestructuringPolicy : IDestructuringPolicy |
| 24 | + { |
| 25 | + public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result) |
| 26 | + { |
| 27 | + switch (value) |
| 28 | + { |
| 29 | + case JObject jo: |
| 30 | + result = Destructure(jo, propertyValueFactory); |
| 31 | + return true; |
| 32 | + case JArray ja: |
| 33 | + result = Destructure(ja, propertyValueFactory); |
| 34 | + return true; |
| 35 | + case JValue jv: |
| 36 | + result = Destructure(jv, propertyValueFactory); |
| 37 | + return true; |
| 38 | + } |
39 | 39 |
|
40 |
| - result = null; |
41 |
| - return false; |
42 |
| - } |
| 40 | + result = null; |
| 41 | + return false; |
| 42 | + } |
43 | 43 |
|
44 |
| - LogEventPropertyValue Destructure(JValue jv, ILogEventPropertyValueFactory propertyValueFactory) |
45 |
| - { |
46 |
| - return propertyValueFactory.CreatePropertyValue(jv.Value, true); |
47 |
| - } |
| 44 | + private static LogEventPropertyValue Destructure(JValue jv, ILogEventPropertyValueFactory propertyValueFactory) |
| 45 | + { |
| 46 | + return propertyValueFactory.CreatePropertyValue(jv.Value, true); |
| 47 | + } |
48 | 48 |
|
49 |
| - // ReSharper disable once ParameterTypeCanBeEnumerable.Local |
50 |
| - LogEventPropertyValue Destructure(JArray ja, ILogEventPropertyValueFactory propertyValueFactory) |
51 |
| - { |
52 |
| - var elems = ja.Select(t => propertyValueFactory.CreatePropertyValue(t, true)); |
53 |
| - return new SequenceValue(elems); |
54 |
| - } |
| 49 | + private static LogEventPropertyValue Destructure(JArray ja, ILogEventPropertyValueFactory propertyValueFactory) |
| 50 | + { |
| 51 | + var elems = ja.Select(t => propertyValueFactory.CreatePropertyValue(t, true)); |
| 52 | + return new SequenceValue(elems); |
| 53 | + } |
55 | 54 |
|
56 |
| - LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) |
57 |
| - { |
58 |
| - string typeTag = null; |
59 |
| - var props = new List<LogEventProperty>(jo.Count); |
60 |
| - foreach (var prop in jo.Properties()) |
61 |
| - { |
62 |
| - if (prop.Name == "$type") |
63 |
| - { |
64 |
| - if (prop.Value is JValue typeVal && typeVal.Value is string) |
65 |
| - { |
66 |
| - typeTag = (string)typeVal.Value; |
67 |
| - continue; |
68 |
| - } |
69 |
| - } |
| 55 | + private static LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) |
| 56 | + { |
| 57 | + return jo.Properties().All(prop => LogEventProperty.IsValidName(prop.Name)) ? |
| 58 | + DestructureToStructureValue(jo, propertyValueFactory) : |
| 59 | + DestructureToDictionaryValue(jo, propertyValueFactory); |
| 60 | + } |
70 | 61 |
|
71 |
| - props.Add(new LogEventProperty(prop.Name, propertyValueFactory.CreatePropertyValue(prop.Value, true))); |
72 |
| - } |
| 62 | + private static LogEventPropertyValue DestructureToStructureValue(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) |
| 63 | + { |
| 64 | + string typeTag = null; |
| 65 | + var props = new List<LogEventProperty>(jo.Count); |
73 | 66 |
|
74 |
| - return new StructureValue(props, typeTag); |
75 |
| - } |
76 |
| - } |
| 67 | + foreach (var prop in jo.Properties()) |
| 68 | + { |
| 69 | + if (prop.Name == "$type") |
| 70 | + { |
| 71 | + if (prop.Value is JValue typeVal && typeVal.Value is string) |
| 72 | + { |
| 73 | + typeTag = (string)typeVal.Value; |
| 74 | + continue; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + props.Add(new LogEventProperty(prop.Name, propertyValueFactory.CreatePropertyValue(prop.Value, true))); |
| 79 | + } |
| 80 | + |
| 81 | + return new StructureValue(props, typeTag); |
| 82 | + } |
| 83 | + |
| 84 | + private static LogEventPropertyValue DestructureToDictionaryValue(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) |
| 85 | + { |
| 86 | + var elements = jo.Properties().Select( |
| 87 | + prop => |
| 88 | + new KeyValuePair<ScalarValue, LogEventPropertyValue>( |
| 89 | + new ScalarValue(prop.Name), |
| 90 | + propertyValueFactory.CreatePropertyValue(prop.Value, true) |
| 91 | + ) |
| 92 | + ); |
| 93 | + return new DictionaryValue(elements); |
| 94 | + } |
| 95 | + } |
77 | 96 | }
|
0 commit comments