示例#1
0
        public static ConfigObject ApplyJsonFromFileInfo(FileInfo file, ConfigObject config = null)
        {
            string  overlay_json   = File.ReadAllText(file.FullName);
            dynamic overlay_config = ParseJson(overlay_json);

            return(Merger.Merge(overlay_config, config));
        }
示例#2
0
        // seeks a folder for .conf files
        public static ConfigObject ApplyFromDirectory(string path, ConfigObject config = null, bool recursive = false)
        {
            if (!Directory.Exists(path))
            {
                throw new System.Exception("no folder found in the given path");
            }

            if (config == null)
            {
                config = new ConfigObject();
            }

            var info = new DirectoryInfo(path);

            if (recursive)
            {
                foreach (DirectoryInfo dir in info.GetDirectories())
                {
                    Console.WriteLine("reading in folder {0}", dir);
                    config = ApplyFromDirectoryInfo(dir, config, recursive);
                }
            }

            // find all files
            FileInfo[] files = info.GetFiles("*.conf", SearchOption.TopDirectoryOnly);
            foreach (FileInfo file in files)
            {
                config = ApplyJsonFromFileInfo(file, config);
            }
            return(config);
        }
        public static ConfigObject FromExpando(ExpandoObject e)
        {
            var edict = e as IDictionary <string, object>;
            var c     = new ConfigObject();
            var cdict = (IDictionary <string, object>)c;

            // this is not complete. It will, however work for JsonFX ExpandoObjects
            // which consits only of primitive types, ExpandoObject or ExpandoObject []
            // but won't work for generic ExpandoObjects which might include collections etc.
            foreach (var kvp in edict)
            {
                // recursively convert and add ExpandoObjects
                if (kvp.Value is ExpandoObject)
                {
                    cdict.Add(kvp.Key, FromExpando((ExpandoObject)kvp.Value));
                }
                else if (kvp.Value is ExpandoObject[])
                {
                    var config_objects = new List <ConfigObject>();
                    foreach (ExpandoObject ex in ((ExpandoObject[])kvp.Value))
                    {
                        config_objects.Add(FromExpando(ex));
                    }
                    cdict.Add(kvp.Key, config_objects.ToArray());
                }
                else
                {
                    cdict.Add(kvp.Key, kvp.Value);
                }
            }
            return(c);
        }
        public void ApplyJson(string json)
        {
            ConfigObject result = Config.ApplyJson(json, this);

            // replace myself's members with the new ones
            members = result.members;
        }
示例#5
0
        public static ConfigObject ApplyJson(string json, ConfigObject config = null)
        {
            if (config == null)
            {
                config = new ConfigObject();
            }

            dynamic parsed = ParseJson(json);

            return(Merger.Merge(parsed, config));
        }
示例#6
0
        public static ConfigObject ParseJson(string json)
        {
            string[] lines = json.Split(new[] { '\n' });
            // remove lines that start with a dash # character
            IEnumerable <string> filtered = from l in lines
                                            where !(Regex.IsMatch(l, @"^\s*#(.*)"))
                                            select l;

            string filtered_json = string.Join("\n", filtered);


            dynamic parsed = JsonConvert.DeserializeObject <ExpandoObject>(filtered_json, new ExpandoObjectConverter());

            // convert the ExpandoObject to ConfigObject before returning
            return(ConfigObject.FromExpando(parsed));
        }
示例#7
0
 public static ConfigObject ApplyJsonFromPath(string path, ConfigObject config = null)
 {
     return(ApplyJsonFromFileInfo(new FileInfo(path), config));
 }
示例#8
0
 public static ConfigObject ApplyFromDirectoryInfo(DirectoryInfo info, ConfigObject config = null,
                                                   bool recursive = false)
 {
     return(ApplyFromDirectory(info.FullName, config, recursive));
 }
示例#9
0
        /// <summary>
        ///     Merge the specified obj2 and obj1, where obj1 has precendence and
        ///     overrules obj2 if necessary.
        /// </summary>
        /// <exception cref='TypeMissmatchException'>
        ///     Is thrown when the type missmatch exception.
        /// </exception>
        public static dynamic Merge(dynamic m_obj1, dynamic m_obj2)
        {
            dynamic obj1 = m_obj1;
            dynamic obj2 = m_obj2;

            // make sure we only deal with ConfigObject but not ExpandoObject as currently
            // return from JsonFX
            if (obj1 is ExpandoObject)
            {
                obj1 = ConfigObject.FromExpando(obj1);
            }
            if (obj2 is ExpandoObject)
            {
                obj2 = ConfigObject.FromExpando(obj2);
            }

            // if both objects are NullExceptionPreventer, return a ConfigObject so the
            // user gets an "Empty" ConfigObject
            if (obj1 is NullExceptionPreventer && obj2 is NullExceptionPreventer)
            {
                return(new ConfigObject());
            }

            // if any object is of NullExceptionPreventer, the other object gets precedence / overruling
            if (obj1 is NullExceptionPreventer && obj2 is ConfigObject)
            {
                return(obj2);
            }
            if (obj2 is NullExceptionPreventer && obj1 is ConfigObject)
            {
                return(obj1);
            }

            // handle what happens if one of the args is null
            if (obj1 == null && obj2 == null)
            {
                return(new ConfigObject());
            }

            if (obj2 == null)
            {
                return(obj1);
            }
            if (obj1 == null)
            {
                return(obj2);
            }

            if (obj1.GetType() != obj2.GetType())
            {
                throw new TypeMissmatchException();
            }

            // changes in the dictionary WILL REFLECT back to the object
            var dict1 = (IDictionary <string, object>)(obj1);
            var dict2 = (IDictionary <string, object>)(obj2);

            dynamic result = new ConfigObject();
            var     rdict  = (IDictionary <string, object>)result;

            // first, copy all non colliding keys over
            foreach (var kvp in dict1)
            {
                if (!dict2.Keys.Contains(kvp.Key))
                {
                    rdict.Add(kvp.Key, kvp.Value);
                }
            }
            foreach (var kvp in dict2)
            {
                if (!dict1.Keys.Contains(kvp.Key))
                {
                    rdict.Add(kvp.Key, kvp.Value);
                }
            }

            // now handle the colliding keys
            foreach (var kvp1 in dict1)
            {
                // skip already copied over keys
                if (!dict2.Keys.Contains(kvp1.Key) || dict2[kvp1.Key] == null)
                {
                    continue;
                }

                var kvp2 = new KeyValuePair <string, object>(kvp1.Key, dict2[kvp1.Key]);

                // some shortcut variables to make code more readable
                string key    = kvp1.Key;
                object value1 = kvp1.Value;
                object value2 = kvp2.Value;
                Type   type1  = value1.GetType();
                Type   type2  = value1.GetType();

                // check if both are same type
                if (type1 != type2)
                {
                    throw new TypeMissmatchException();
                }

                if (value1 is ConfigObject[])
                {
                    rdict[key] = CollectionMerge(value1, value2);

                    /*var d1 = val1 as IDictionary<string, object>;
                     *                  var d2 = val2 as IDictionary<string, object>;
                     *                  rdict[key] = CollectionMerge (val1, val2); */
                }
                else if (value1 is ConfigObject)
                {
                    rdict[key] = Merge(value1, value2);
                }
                else if (value1 is string)
                {
                    rdict[key] = value1;
                }
                else if (value1 is IEnumerable)
                {
                    rdict[key] = CollectionMerge(value1, value2);
                }
                else
                {
                    rdict[key] = value1;
                }

                //else if (kvp.Value.GetType ().IsByRef) {
                // recursively merge it
                //}
            }
            return(result);
        }