Exemplo n.º 1
0
 /// <summary>
 /// Deserialize the source string from either json or yaml and populate the rules
 /// object.
 /// </summary>
 /// <param name="source"></param>
 public static JObject PopulateRules(string source)
 {
     // Is it valid json?
     if (InputValidators.IsValidJson(source))
     {
         // Deserialize directly
         return(JsonConvert.DeserializeObject <dynamic>(source));
     }
     // Is it valid yaml?
     else if (InputValidators.IsValidYaml(source))
     {
         // Deserialize yaml
         var deserializer = new Deserializer();
         var yamlObject   = deserializer.Deserialize(new StringReader(source));
         return(PopulateRules(yamlObject));
     }
     else
     {
         throw new Exception("Grammar file doesn't seem to be valid JSON or YAML!");
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Deserialize the source string from either json or yaml and populate the rules
        /// object.
        /// </summary>
        /// <param name="source"></param>
        private void PopulateRules(string source)
        {
            // Is it valid json?
            if (InputValidators.IsValidJson(source))
            {
                // Deserialize directly
                Rules = JsonConvert.DeserializeObject <dynamic>(source);
            }
            // Is it valid yaml?
            else if (InputValidators.IsValidYaml(source))
            {
                // Deserialize yaml
                var deserializer = new Deserializer();
                var yamlObject   = deserializer.Deserialize(new StringReader(source));

                // Reserialize the yaml as json into the Rules object
                var rules = JsonConvert.SerializeObject(yamlObject);
                Rules = JsonConvert.DeserializeObject <dynamic>(rules);
            }
            else
            {
                throw new Exception("Grammar file doesn't seem to be valid JSON or YAML!");
            }
        }