Ejemplo n.º 1
0
        public List <RuleFixture> SaveRule(Rule newRule, List <RuleFixture> ruleFixtures, string[] approvedRuleTypes)
        {
            //ruleFixtures.Find(x => x.RuleType == newRule.RuleType).Rules.Add(newRule);
            var foundFixture = ruleFixtures.Find(x => x.RuleType == newRule.RuleType);

            if (foundFixture == null)
            {
                RuleFixture newRuleFixture =
                    new RuleFixture()
                {
                    RuleType = newRule.RuleType,
                    Rules    = new List <Rule>()
                    {
                        newRule
                    }
                };
                ruleFixtures.Add(newRuleFixture);
            }

            //.Rules.Add(newRule);
            var newRuleDataAsJson = JsonConvert.SerializeObject(
                new RuleData()
            {
                ApprovedRuleTypes = approvedRuleTypes,
                RuleFixtures      = ruleFixtures
            });

            File.WriteAllText(_Path, newRuleDataAsJson);

            return(ruleFixtures);
        }
Ejemplo n.º 2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            /// This attempt tries to create objects based on json property RuleType
            JObject jobj = JObject.Load(reader);

            RuleData ruleData = new RuleData();

            ruleData.ApprovedRuleTypes = jobj["ApprovedRuleTypes"].ToObject <string[]>(serializer);
            ruleData.RuleFixtures      = new List <RuleFixture>();

            foreach (JObject joF in jobj["RuleFixtures"])
            {
                RuleFixture ruleFixture = new RuleFixture();
                ruleFixture.RuleType = (string)joF["RuleType"];
                ruleFixture.Rules    = new List <Rule>();

                foreach (JObject joR in joF["Rules"])
                {
                    if (ruleFixture.RuleType == "replace")
                    {
                        ruleFixture.Rules.Add(joR.ToObject <ReplaceRule>(serializer));
                    }
                    else if (ruleFixture.RuleType == "append")
                    {
                        ruleFixture.Rules.Add(joR.ToObject <AppendRule>(serializer));
                    }
                    else
                    {
                        // error
                        Console.WriteLine("The rule database contains rules that are not defined within the core system. \n Please revise the source code.");
                    }
                }

                ruleData.RuleFixtures.Add(ruleFixture);
            }

            return(ruleData);
        }