Example #1
0
        private static bool ReadProperty(StreamReader rdr, JObject tmp)
        {
            string key = JsonReader.ReadString(rdr);

            if (key != null && !tmp.properties.ContainsKey(key))
            {
                JsonReader.SpinWhiteSpace(rdr);
                if (rdr.Read() == (int)':')
                {
                    JsonReader.SpinWhiteSpace(rdr);
                    JEntity value   = null;
                    bool    wasNull = false;
                    int     c       = rdr.Peek();
                    if (c == (int)'"')
                    {
                        value = JPrimitive.Read(rdr); //its a string!
                    }
                    else if (c == (int)'[')
                    {
                        value = JArray.Read(rdr);
                    }
                    else if (c == (int)'{')
                    {
                        value = JObject.Read(rdr);
                    }
                    else if (c == (int)'-' || (c >= 48 && c <= 57)) //- or ascii for 0-9 -- should be a number
                    {
                        value = JPrimitive.Read(rdr);
                    }
                    else if (c == (int)'t') //should be "true"
                    {
                        value = JPrimitive.Read(rdr);
                    }
                    else if (c == (int)'f') //should be "false"
                    {
                        value = JPrimitive.Read(rdr);
                    }
                    else if (c == (int)'n') //should be "null"
                    {
                        wasNull = true;
                        if (!JsonReader.ReadNull(rdr))
                        {
                            return(false); //looked like a null, but wasn't
                        }
                    }
                    else
                    {
                        return(false);  //unrecognized starting char
                    }
                    if (value != null || wasNull)
                    {
                        tmp.properties.Add(key, value);
                        return(true);
                    }
                    //property wasn't "null", but we didn't get anything read - that's an error
                }
            }

            return(false); //didn't get a key back
        }
Example #2
0
        private static bool ReadElement(StreamReader rdr, JArray tmp)
        {
            JsonReader.SpinWhiteSpace(rdr);
            JEntity element = null;
            int     cur     = rdr.Peek();

            if (cur == (int)'"')
            {
                element = JPrimitive.Read(rdr); //its a string!
            }
            else if (cur == (int)'{')
            {
                element = JObject.Read(rdr);
            }
            else if (cur == (int)'[')
            {
                element = JArray.Read(rdr);
            }
            else if (cur == (int)'-' || (cur >= 48 && cur <= 57)) //- or ascii for 0-9 -- should be a number
            {
                element = JPrimitive.Read(rdr);
            }
            else if (cur == (int)'t') //should be "true"
            {
                element = JPrimitive.Read(rdr);
            }
            else if (cur == (int)'f') //should be "false"
            {
                element = JPrimitive.Read(rdr);
            }
            else if (cur == (int)'n') //should be "null"
            {
                if (JsonReader.ReadNull(rdr))
                {
                    tmp.elements.Add(null);
                    return(true); //one off case here due to a literal null element
                }
                //looked like a null, but wasn't - so we fall through fail
            }

            if (element != null)
            {
                tmp.elements.Add(element);
                return(true);
            }

            return(false);
        }
Example #3
0
 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());
     }
 }
Example #4
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 #5
0
 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);
     }
 }
Example #6
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 #7
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);
        }