Skip to content

Commit 2ddeeb2

Browse files
committed
updating prune to return entire data set rather than dropping data. if more fields than allowed are present will return an object that matches platform expectations
1 parent a0ec9ac commit 2ddeeb2

File tree

2 files changed

+39
-50
lines changed

2 files changed

+39
-50
lines changed

Src/StackifyLib/Utils/HelperFunctions.cs

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
using System.Collections;
21
using Newtonsoft.Json;
32
using System;
43
using System.Collections.Generic;
5-
using System.ComponentModel;
64
using System.Linq;
75
using System.Reflection;
86
using System.Text;
9-
using Newtonsoft.Json.Converters;
107
using Newtonsoft.Json.Linq;
118

129

@@ -28,7 +25,6 @@ public class HelperFunctions
2825
public static string SerializeDebugData(object logObject, bool serializeSimpleTypes, Dictionary<string, object> properties = null)
2926
{
3027
Type t = null;
31-
// TypeInfo typeInfo = null;
3228
JObject jObject = null;
3329

3430
try
@@ -86,8 +82,6 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
8682
{
8783
jObject.Add("objectType", type.FullName);
8884
}
89-
90-
PruneJObject(jObject, Config.LoggingJsonMaxFields);
9185
}
9286
else if (token is JArray)
9387
{
@@ -152,8 +146,6 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
152146
}
153147
}
154148
}
155-
156-
PruneJObject(jObject, Config.LoggingJsonMaxFields);
157149
}
158150
else if (token is JValue)
159151
{
@@ -212,51 +204,50 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
212204

213205
if (jObject != null)
214206
{
207+
208+
jObject = GetPrunedObject(jObject, Config.LoggingJsonMaxFields);
209+
215210
return JsonConvert.SerializeObject(jObject, serializerSettings);
216211
}
217212

218213
return null;
219214
}
220215

221-
222-
private static void PruneJObject(JObject obj, int maxFields)
216+
/// <summary>
217+
/// If the <see cref="JObject"/> provided has move fields than maxFields
218+
/// will return a simplified <see cref="JObject"/> with original as an unparsed string message,
219+
/// otherwise will return original <see cref="JObject"/>
220+
/// </summary>
221+
private static JObject GetPrunedObject(JObject obj, int maxFields)
223222
{
224-
var count = 0;
223+
var fieldCount = GetFieldCount(obj);
225224

226-
var itemsToRemove = PruneJTokenRecursive(obj, maxFields, ref count);
227-
228-
foreach (var item in itemsToRemove)
225+
if (fieldCount > maxFields)
229226
{
230-
item.Remove();
227+
return new JObject
228+
{
229+
{ "invalid", true },
230+
{ "message", obj.ToString() }
231+
};
231232
}
233+
234+
return obj;
232235
}
233236

234-
private static List<JToken> PruneJTokenRecursive(JToken obj, int maxFields, ref int count)
237+
private static int GetFieldCount(JToken obj)
235238
{
236-
if (obj is JProperty || obj is JArray)
237-
{
238-
count++;
239-
}
240-
241-
var itemsToRemove = new List<JToken>();
242-
243-
foreach (var item in obj.Children())
239+
switch (obj.Type)
244240
{
245-
if (count >= maxFields)
246-
{
247-
if (item is JProperty)
248-
{
249-
itemsToRemove.Add(item);
250-
}
251-
}
252-
253-
itemsToRemove.AddRange(PruneJTokenRecursive(item, maxFields, ref count));
241+
case JTokenType.Array:
242+
case JTokenType.Object:
243+
return obj.Children().Sum(i => GetFieldCount(i));
244+
case JTokenType.Property:
245+
return GetFieldCount(obj.Value<JProperty>().Value);
246+
default:
247+
return 1;
254248
}
255-
256-
return itemsToRemove;
257249
}
258250

259-
260251
public static bool IsValueType(object obj)
261252
{
262253
if (obj == null)
@@ -270,18 +261,6 @@ public static bool IsValueType(object obj)
270261
#endif
271262
}
272263

273-
274-
//public static dynamic ToDynamic(object value)
275-
//{
276-
// IDictionary<string, object> expando = new ExpandoObject();
277-
278-
// foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(value.GetType()))
279-
// expando.Add(property.Name, property.GetValue(value));
280-
281-
// return expando as ExpandoObject;
282-
//}
283-
284-
285264
public static string CleanPartialUrl(string url)
286265
{
287266
if (string.IsNullOrEmpty(url) || !url.Contains("/"))

test/StackifyLib.UnitTests/JsonSerialization_Tests.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Diagnostics;
44
using System.Text;
55
using FluentAssertions;
6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
68
using Xunit;
79
using Xunit.Abstractions;
810

@@ -27,7 +29,11 @@ public void Should_Prune_Object()
2729

2830
output.WriteLine(result);
2931

30-
result.Should().NotContain("FieldSixty");
32+
var obj = JObject.Parse(result);
33+
34+
Assert.True(obj.TryGetValue("invalid", out _));
35+
Assert.True(obj.TryGetValue("message", out _));
36+
Assert.Equal(2, obj.Count);
3137
}
3238

3339
[Fact]
@@ -45,7 +51,11 @@ public void Should_Prune_Object_Array()
4551

4652
output.WriteLine(result);
4753

48-
result.Should().NotContain("FieldSixty");
54+
var obj = JObject.Parse(result);
55+
56+
Assert.True(obj.TryGetValue("invalid", out _));
57+
Assert.True(obj.TryGetValue("message", out _));
58+
Assert.Equal(2, obj.Count);
4959
}
5060

5161

0 commit comments

Comments
 (0)