Example #1
0
        private object BuildDictionary(JObject o)
        {
            JType lookType = JType.JString;

            foreach (string s in o.Keys)
            {
                JEntity e = o[s];
                if (e != null)
                {
                    if (e is JPrimitive) //only thing currently supported
                    {
                        JPrimitive p = (JPrimitive)e;
                        lookType = p.Type;
                    }
                    else
                    {
                        return(null); //fail fast
                    }
                    break;            //end the loop anyway -- only needed to see the type of the first element
                }
                return(null);         //got a null thing - which shouldn't be in the config
            }

            if (lookType == JType.JString)
            {
                Dictionary <string, string> sVals = new Dictionary <string, string>();
                foreach (string key in o.Keys)
                {
                    JEntity e = o[key];
                    if (e != null && e is JPrimitive)
                    {
                        JPrimitive p = (JPrimitive)e;
                        if (p.Type == JType.JString)
                        {
                            sVals.Add(key, p.ToString());
                        }
                        else
                        {
                            return(null); //short circuit early
                        }
                    }
                    else
                    {
                        return(null); //short circuit early
                    }
                }
                return(sVals);
            }
            else if (lookType == JType.JBool)
            {
                Dictionary <string, bool> sVals = new Dictionary <string, bool>();
                foreach (string key in o.Keys)
                {
                    JEntity e = o[key];
                    if (e != null && e is JPrimitive)
                    {
                        JPrimitive p = (JPrimitive)e;
                        if (p.Type == JType.JBool)
                        {
                            sVals.Add(key, p.ToBool());
                        }
                        else
                        {
                            return(null); //short circuit early
                        }
                    }
                    else
                    {
                        return(null); //short circuit early
                    }
                }
                return(sVals);
            }
            else //number
            {
                object res = BuildDictionary(o, false);
                if (res == null)
                {
                    res = BuildDictionary(o, true);
                }
                if (res == null)
                {
                    return(null);
                }
            }

            return(null);
        }
Example #2
0
        private object BuildArray(JArray o)
        {
            JType lookType = JType.JString;

            if (o.Count > 0)
            {
                if (o[0] is JPrimitive)
                {
                    JPrimitive p = (JPrimitive)o[0];
                    lookType = p.Type;
                }
            }

            if (lookType == JType.JString)
            {
                List <string> sVals = new List <string>();
                for (int i = 0; i < o.Count; i++)
                {
                    JEntity e = o[i];
                    if (e != null && e is JPrimitive)
                    {
                        JPrimitive p = (JPrimitive)e;
                        if (p.Type == JType.JString)
                        {
                            sVals.Add(p.ToString());
                        }
                        else
                        {
                            return(null); //short circuit early
                        }
                    }
                    else
                    {
                        return(null); //short circuit early
                    }
                }
                return(sVals.ToArray());
            }
            else if (lookType == JType.JBool)
            {
                List <bool> sVals = new List <bool>();
                for (int i = 0; i < o.Count; i++)
                {
                    JEntity e = o[i];
                    if (e != null && e is JPrimitive)
                    {
                        JPrimitive p = (JPrimitive)e;
                        if (p.Type == JType.JString)
                        {
                            sVals.Add(p.ToBool());
                        }
                        else
                        {
                            return(null); //short circuit early
                        }
                    }
                    else
                    {
                        return(null); //short circuit early
                    }
                }
                return(sVals.ToArray());
            }
            else //number
            {
                object res = BuildArray(o, false);
                if (res == null)
                {
                    res = BuildArray(o, true);
                }
                if (res == null)
                {
                    return(null);
                }
            }

            return(null);
        }
Example #3
0
        internal bool Init(string filename)
        {
            //format of file is specific for config params
            JsonReader r = new JsonReader();
            JObject    o = r.Read(filename);

            if (o != null)
            {
                foreach (string s in o.Keys) //types
                {
                    JEntity e = o[s];
                    if (e != null && e is JObject)
                    {
                        Dictionary <string, object> parms = new Dictionary <string, object>();
                        if (!this.configs.ContainsKey(s))
                        {
                            this.configs.Add(s, parms);
                        }
                        else
                        {
                            parms = this.configs[s];
                        }

                        JObject cur = (JObject)e;
                        foreach (string k in cur.Keys) //param names
                        {
                            e = cur[k];                //now it's a param
                            object paramData = null;
                            if (e != null)
                            {
                                if (e is JObject) //treat as dictionary
                                {
                                    JObject param = (JObject)e;
                                    paramData = BuildDictionary(param);
                                    if (paramData != null)
                                    {
                                        parms[k] = paramData;
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                else if (e is JArray) //as array
                                {
                                    JArray param = (JArray)e;
                                    paramData = BuildArray(param);
                                    if (paramData != null)
                                    {
                                        parms[k] = paramData;
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                                else //JPrimitive
                                {
                                    JPrimitive param = (JPrimitive)e;
                                    if (param != null)
                                    {
                                        if (param.Type == JType.JString)
                                        {
                                            parms[k] = param.ToString();
                                        }
                                        else if (param.Type == JType.JNumber)
                                        {
                                            try
                                            {
                                                int i = param.ToInt();
                                                parms[k] = i;
                                            }
                                            catch
                                            {
                                                try
                                                {
                                                    long l = param.ToLong();
                                                    parms[k] = l;
                                                }
                                                catch
                                                {
                                                    try
                                                    {
                                                        float f = param.ToFloat();
                                                        parms[k] = f;
                                                    }
                                                    catch
                                                    {
                                                        try
                                                        {
                                                            double d = param.ToDouble();
                                                            parms[k] = d;
                                                        }
                                                        catch
                                                        {
                                                            return(false);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else if (param.Type == JType.JBool)
                                        {
                                            parms[k] = param.ToBool();
                                        }
                                    }
                                    else
                                    {
                                        return(false);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        return(false); //we read it all, or none
                    }
                }
                return(true);
            }

            return(false);
        }