private object BuildDictionary(JObject o, bool hasDecimal)
 {
     if (hasDecimal) //try float, then double, then give up
     {
         Dictionary <string, float> sVals = new Dictionary <string, float>();
         foreach (string key in o.Keys)
         {
             JEntity e = o[key];
             if (e != null && e is JPrimitive)
             {
                 JPrimitive p = (JPrimitive)e;
                 if (p.Type == JType.JNumber)
                 {
                     try
                     {
                         sVals.Add(key, p.ToFloat());
                     }
                     catch
                     {
                         sVals.Clear();
                         sVals = null; //just in case a GC run comes along
                         Dictionary <string, double> kVals = new Dictionary <string, double>();
                         foreach (string key2 in o.Keys)
                         {
                             e = o[key2];
                             if (e != null && e is JPrimitive)
                             {
                                 p = (JPrimitive)e;
                                 if (p.Type == JType.JNumber)
                                 {
                                     try
                                     {
                                         kVals.Add(key, p.ToDouble());
                                     }
                                     catch
                                     {
                                         return(null); //not an integer type we can handle
                                     }
                                 }
                                 else
                                 {
                                     return(null); //short circuit early
                                 }
                             }
                             else
                             {
                                 return(null); //short circuit early
                             }
                         }
                         return(kVals);
                     }
                 }
                 else
                 {
                     return(null); //short circuit early
                 }
             }
             else
             {
                 return(null); //short circuit early
             }
         }
         return(sVals);
     }
     else //try int, then long, then give up
     {
         //try ints first, then widen if needed
         Dictionary <string, int> sVals = new Dictionary <string, int>();
         foreach (string key in o.Keys)
         {
             JEntity e = o[key];
             if (e != null && e is JPrimitive)
             {
                 JPrimitive p = (JPrimitive)e;
                 if (p.Type == JType.JNumber)
                 {
                     try
                     {
                         sVals.Add(key, p.ToInt());
                     }
                     catch
                     {
                         if (p.ToString().IndexOf('.') >= 0)
                         {
                             return(null); //not an integer type
                         }
                         else //widen and try longs
                         {
                             sVals.Clear();
                             sVals = null; //just in case a GC run comes along
                             Dictionary <string, long> kVals = new Dictionary <string, long>();
                             foreach (string key2 in o.Keys)
                             {
                                 e = o[key2];
                                 if (e != null && e is JPrimitive)
                                 {
                                     p = (JPrimitive)e;
                                     if (p.Type == JType.JNumber)
                                     {
                                         try
                                         {
                                             kVals.Add(key, p.ToLong());
                                         }
                                         catch
                                         {
                                             return(null); //not an integer type we can handle
                                         }
                                     }
                                     else
                                     {
                                         return(null); //short circuit early
                                     }
                                 }
                                 else
                                 {
                                     return(null); //short circuit early
                                 }
                             }
                             return(kVals);
                         }
                     }
                 }
                 else
                 {
                     return(null); //short circuit early
                 }
             }
             else
             {
                 return(null); //short circuit early
             }
         }
         return(sVals);
     }
 }
 private object BuildArray(JArray o, bool hasDecimal)
 {
     if (hasDecimal) //try float, then double, then give up
     {
         List <float> sVals = new List <float>();
         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.JNumber)
                 {
                     try
                     {
                         sVals.Add(p.ToFloat());
                     }
                     catch
                     {
                         sVals.Clear();
                         sVals = null; //just in case a GC run comes along
                         List <double> kVals = new List <double>();
                         for (int j = 0; j < o.Count; j++)
                         {
                             e = o[j];
                             if (e != null && e is JPrimitive)
                             {
                                 p = (JPrimitive)e;
                                 if (p.Type == JType.JNumber)
                                 {
                                     try
                                     {
                                         kVals.Add(p.ToDouble());
                                     }
                                     catch
                                     {
                                         return(null); //not an integer type we can handle
                                     }
                                 }
                                 else
                                 {
                                     return(null); //short circuit early
                                 }
                             }
                             else
                             {
                                 return(null); //short circuit early
                             }
                         }
                         return(kVals.ToArray());
                     }
                 }
                 else
                 {
                     return(null); //short circuit early
                 }
             }
             else
             {
                 return(null); //short circuit early
             }
         }
         return(sVals.ToArray());
     }
     else //try int, then long, then give up
     {
         //try ints first, then widen if needed
         List <int> sVals = new List <int>();
         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.JNumber)
                 {
                     try
                     {
                         sVals.Add(p.ToInt());
                     }
                     catch
                     {
                         if (p.ToString().IndexOf('.') >= 0)
                         {
                             return(null); //not an integer type
                         }
                         else //widen and try longs
                         {
                             sVals.Clear();
                             sVals = null; //just in case a GC run comes along
                             List <long> kVals = new List <long>();
                             for (int j = 0; j < o.Count; j++)
                             {
                                 e = o[j];
                                 if (e != null && e is JPrimitive)
                                 {
                                     p = (JPrimitive)e;
                                     if (p.Type == JType.JNumber)
                                     {
                                         try
                                         {
                                             kVals.Add(p.ToLong());
                                         }
                                         catch
                                         {
                                             return(null); //not an integer type we can handle
                                         }
                                     }
                                     else
                                     {
                                         return(null); //short circuit early
                                     }
                                 }
                                 else
                                 {
                                     return(null); //short circuit early
                                 }
                             }
                             return(kVals.ToArray());
                         }
                     }
                 }
                 else
                 {
                     return(null); //short circuit early
                 }
             }
             else
             {
                 return(null); //short circuit early
             }
         }
         return(sVals.ToArray());
     }
 }
        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);
        }