| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json.Serialization;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text.RegularExpressions;
- namespace D12._2
- {
- public class PropertyIgnoreSerializerContractResolver : DefaultContractResolver
- {
- protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
- {
- var property = base.CreateProperty(member, memberSerialization);
- if (property.PropertyName == "red")
- {
- property.ShouldSerialize = i => false;
- property.Ignored = true;
- }
- return property;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- if (args.Length < 1) throw new ArgumentException();
- if (File.Exists(args[0]) == false) throw new FileNotFoundException();
- var input = File.ReadAllText(args[0]);
- // Use dynamic to skip the use of specialized functions and a factory pattern
- dynamic n = JsonConvert.DeserializeObject(input);
- var result = SumValues(n);
- Console.WriteLine($"The answer is : {result}");
- }
- private static long SumValues(JValue n) => n.Type == JTokenType.Integer ? (long) n.Value : 0;
- private static long SumValues(JObject n)
- {
- if (n.Properties().Any(p => (p.Value as JValue)?.Value as string == "red"))
- return 0;
- return n.Properties().Sum((dynamic a) => (long)SumValues(a.Value));
- }
- private static long SumValues(JArray n) => n.Sum((dynamic a) => (long)SumValues(a));
- }
- }
|