1- using System . Collections ;
21using Newtonsoft . Json ;
32using System ;
43using System . Collections . Generic ;
5- using System . ComponentModel ;
64using System . Linq ;
75using System . Reflection ;
86using System . Text ;
9- using Newtonsoft . Json . Converters ;
107using 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 ( "/" ) )
0 commit comments