Skip to content

Commit 42c0070

Browse files
authored
Merge pull request #8 from andrewslavin/Log-objects-with-whitespace-prop-names-as-DictionaryValue
Log objects with whitespace prop names as dictionary value
2 parents 525b98f + 479cb18 commit 42c0070

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/Destructurama.JsonNet/JsonNet/JsonNetDestructuringPolicy.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyV
4141
return false;
4242
}
4343

44-
LogEventPropertyValue Destructure(JValue jv, ILogEventPropertyValueFactory propertyValueFactory)
44+
private static LogEventPropertyValue Destructure(JValue jv, ILogEventPropertyValueFactory propertyValueFactory)
4545
{
4646
return propertyValueFactory.CreatePropertyValue(jv.Value, true);
4747
}
4848

49-
// ReSharper disable once ParameterTypeCanBeEnumerable.Local
50-
LogEventPropertyValue Destructure(JArray ja, ILogEventPropertyValueFactory propertyValueFactory)
49+
private static LogEventPropertyValue Destructure(JArray ja, ILogEventPropertyValueFactory propertyValueFactory)
5150
{
5251
var elems = ja.Select(t => propertyValueFactory.CreatePropertyValue(t, true));
5352
return new SequenceValue(elems);
5453
}
5554

56-
LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory propertyValueFactory)
55+
private static LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory propertyValueFactory)
5756
{
5857
string typeTag = null;
5958
var props = new List<LogEventProperty>(jo.Count);
59+
6060
foreach (var prop in jo.Properties())
6161
{
6262
if (prop.Name == "$type")
@@ -67,11 +67,27 @@ LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory prop
6767
continue;
6868
}
6969
}
70+
else if (!LogEventProperty.IsValidName(prop.Name))
71+
{
72+
return DestructureToDictionaryValue(jo, propertyValueFactory);
73+
}
7074

7175
props.Add(new LogEventProperty(prop.Name, propertyValueFactory.CreatePropertyValue(prop.Value, true)));
7276
}
7377

7478
return new StructureValue(props, typeTag);
7579
}
80+
81+
private static LogEventPropertyValue DestructureToDictionaryValue(JObject jo, ILogEventPropertyValueFactory propertyValueFactory)
82+
{
83+
var elements = jo.Properties().Select(
84+
prop =>
85+
new KeyValuePair<ScalarValue, LogEventPropertyValue>(
86+
new ScalarValue(prop.Name),
87+
propertyValueFactory.CreatePropertyValue(prop.Value, true)
88+
)
89+
);
90+
return new DictionaryValue(elements);
91+
}
7692
}
7793
}

test/Destructurama.JsonNet.Tests/JsonNetTypesDestructuringTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public void AttributesAreConsultedWhenDestructuring()
3131
Arr = new[] { 1, 2, 3 },
3232
S = "Some string",
3333
D = new Dictionary<int, string> { { 1, "One" }, { 2, "Two" } },
34-
E = (object)null
34+
E = (object)null,
35+
ESPN = JsonConvert.DeserializeObject("{\"\":\"Empty string property name\"}"),
36+
WSPN = JsonConvert.DeserializeObject("{\"\r\n\":\"Whitespace property name\"}")
3537
};
3638

3739
var ser = JsonConvert.SerializeObject(test, new JsonSerializerSettings
@@ -48,10 +50,10 @@ public void AttributesAreConsultedWhenDestructuring()
4850
Assert.IsType<StructureValue>(props["HN"]);
4951
Assert.IsType<SequenceValue>(props["Arr"]);
5052
Assert.IsType<string>(props["S"].LiteralValue());
53+
Assert.IsType<StructureValue>(props["D"]);
5154
Assert.Null(props["E"].LiteralValue());
52-
53-
// Not currently handled correctly - will serialize as a structure
54-
// Assert.IsInstanceOf<DictionaryValue>(props["D"]);
55+
Assert.IsType<DictionaryValue>(props["ESPN"]);
56+
Assert.IsType<DictionaryValue>(props["WSPN"]);
5557
}
5658
}
5759
}

0 commit comments

Comments
 (0)