public static void ProcessInput(StreamReader inputReader) { var sumOfNumbers = 0; var filteredSum = 0; JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); string inputLine; while (!string.IsNullOrEmpty(inputLine = inputReader.ReadLine())) { DynamicJsonObject json = jss.Deserialize(inputLine, typeof(object)) as DynamicJsonObject; sumOfNumbers += json.SumIntegerValues(); filteredSum += json.SumIntegersExcludingRedObjects(); } Console.WriteLine("Sum of all integers in JSON hierarchy: " + sumOfNumbers); Console.WriteLine("Sum of all integers on non-red objects: " + filteredSum); }
public override bool TryGetMember(GetMemberBinder binder, out object result) { result = this.Dictionary[binder.Name]; if (result is IDictionary <string, object> ) { result = new DynamicJsonObject(result as IDictionary <string, object>); } else if (result is ArrayList) { var arrayList = result as ArrayList; if (arrayList.Count > 0 && arrayList[0] is IDictionary <string, object> ) { result = new List <DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary <string, object>))); } else { result = new List <object>((result as ArrayList).ToArray()); } } return(this.Dictionary.ContainsKey(binder.Name)); }