コード例 #1
0
 public static JValue ValueOf(object obj)
 {
     if (obj is string)
     {
         return(JString.ValueOf(obj as string));
     }
     if (obj is int || obj is long)
     {
         return(JInteger.ValueOf(obj));
     }
     if (obj is float || obj is double)
     {
         return(JDouble.ValueOf(obj));
     }
     if (obj is bool)
     {
         return(JBoolean.ValueOf(obj));
     }
     if (obj is IList <object> )
     {
         return(JArray.ValueOf(obj as IList <object>));
     }
     if (obj is IList <Dictionary <string, object> > )
     {
         return(JArray.ValueOf(ToObjectList(obj as IList <Dictionary <string, object> >)));
     }
     if (obj is IDictionary <string, object> )
     {
         return(JObject.ValueOf(obj as IDictionary <string, object>));
     }
     return(null);
 }
コード例 #2
0
        /**
         * Get JValue from string
         * @param value string
         * @return JValue
         */
        public static JValue ValueOfString(string val)
        {
            char c = val [0];

            if ('1' == c || '2' == c || '3' == c || '4' == c || '5' == c ||
                '6' == c || '7' == c || '8' == c || '9' == c || '0' == c)
            {
                return(val.IndexOf('.') < 0 ? (JValue)JInteger.ValueOf(val) : (JValue)JDouble.ValueOf(val));
            }
            else if ("true" == val || "false" == val)
            {
                return(JBoolean.ValueOf(val));
            }
            return(JString.ValueOf(val));
        }
コード例 #3
0
ファイル: JArray.cs プロジェクト: demdxx/JsonLight
        /**
         * Decode json string to JArray
         * @param content string
         * @param ref index int
         * @param red line int
         * @param ref simbol int
         * @return new JArray instance
         */
        public static JArray DecodeArray(string content, ref int index, ref int line, ref int simbol)
        {
            if ('[' != content [index])
            {
                throw new ExceptionSyntaxError(line, simbol);
            }

            JArray arr   = new JArray();
            bool   comma = true;

            index++;
            simbol++;

            for (; index < content.Length; index++, simbol++)
            {
                char c = content [index];
                if (']' == c)
                {
                    return(arr);
                }
                if (' ' == c || '\n' == c || '\t' == c)
                {
                    if ('\n' == c)
                    {
                        line++;
                        simbol = -1;
                    }
                    continue;
                }
                else if (!comma)
                {
                    if (',' != c)
                    {
                        throw new ExceptionSyntaxError(line, simbol);
                    }
                    comma = true;
                }
                else
                {
                    if ('"' == c || '\'' == c)
                    {
                        // Decode string value "String" or 'String'
                        try {
                            var pIndex = index;
                            index++;
                            arr.Add(JString.ValueOf(JUtils.DecodeEscapeString(content, ref index, c)));
                            simbol += index - pIndex;
                        } catch (FormatException e) {
                            throw new ExceptionSyntaxError(line, simbol);
                        }
                    }
                    else if ('[' == c)
                    {
                        // Parse Array
                        arr.Add(JArray.DecodeArray(content, ref index, ref line, ref simbol));
                    }
                    else if ('{' == c)
                    {
                        // Parse Dictionary
                        arr.Add(JObject.DecodeObject(content, ref index, ref line, ref simbol));
                    }
                    else
                    {
                        // Decode word value WORD or W0r6 ...
                        var pIndex = index;
                        var val    = JUtils.DecodeWord(content, ref index);
                        if (null == val || val.Length < 1)
                        {
                            throw new ExceptionSyntaxError(line, simbol);
                        }
                        if ("null" == val)
                        {
                            arr.Add(null);
                        }
                        else
                        {
                            arr.Add(JUtils.ValueOfString(val));
                        }
                        simbol += index - pIndex;
                    }
                    comma = false;
                }
            }
            throw new ExceptionSyntaxError(line, simbol);
        }
コード例 #4
0
        /**
         * Decode json string to JObject
         * @param content string
         * @param ref index int
         * @param red line int
         * @param ref simbol int
         * @return new JObject instance
         */
        public static JObject DecodeObject(string content, ref int index, ref int line, ref int simbol)
        {
            if ('{' != content [index])
            {
                throw new ExceptionSyntaxError(0, 0);
            }

            JObject obj = new JObject();

            bool   comma   = true;
            bool   colon   = false;
            bool   isValue = false;
            string key     = "";

            index++;
            simbol++;

            for (; index < content.Length; index++, simbol++)
            {
                char c = content [index];
                if ('}' == c)
                {
                    if (colon)
                    {
                        throw new ExceptionSyntaxError(line, simbol);
                    }
                    return(obj);
                }
                else if (' ' == c || '\n' == c || '\t' == c)
                {
                    if ('\n' == c)
                    {
                        line++;
                        simbol = -1;
                    }
                    continue;
                }
                else if (!comma)
                {
                    if (',' != c)
                    {
                        throw new ExceptionSyntaxError(line, simbol);
                    }
                    comma = true;
                }
                else
                {
                    if (isValue && !colon)
                    {
                        if (':' != c)
                        {
                            throw new ExceptionSyntaxError(line, simbol);
                        }
                        colon = true;
                    }
                    else
                    {
                        if ('"' == c || '\'' == c)
                        {
                            // Decode string value "String" or 'String'
                            try {
                                var pIndex = index;
                                index++;
                                if (isValue)
                                {
                                    obj.Add(key, JString.ValueOf(JUtils.DecodeEscapeString(content, ref index, c)));
                                }
                                else
                                {
                                    key = JUtils.DecodeEscapeString(content, ref index, c);
                                    if (null == key || key.Length < 1)
                                    {
                                        throw new ExceptionSyntaxError(line, simbol);
                                    }
                                }
                                simbol += index - pIndex;
                            } catch (FormatException e) {
                                throw new ExceptionSyntaxError(line, simbol);
                            }
                        }
                        else if ('[' == c)
                        {
                            // Parse Array
                            if (!isValue)
                            {
                                throw new ExceptionSyntaxError(line, simbol);
                            }
                            obj.Add(key, JArray.DecodeArray(content, ref index, ref line, ref simbol));
                        }
                        else if ('{' == c)
                        {
                            // Parse Dictionary
                            if (!isValue)
                            {
                                throw new ExceptionSyntaxError(line, simbol);
                            }
                            obj.Add(key, JObject.DecodeObject(content, ref index, ref line, ref simbol));
                        }
                        else
                        {
                            // Decode word value WORD or W0r6 ...
                            var pIndex = index;
                            if (isValue)
                            {
                                var val = JUtils.DecodeWord(content, ref index);
                                if (null == val || val.Length < 1)
                                {
                                    throw new ExceptionSyntaxError(line, simbol);
                                }
                                if ("null" == val)
                                {
                                    obj.Add(key, null);
                                }
                                else
                                {
                                    obj.Add(key, JUtils.ValueOfString(val));
                                }
                            }
                            else
                            {
                                key = JUtils.DecodeWord(content, ref index);
                                if (null == key || key.Length < 1)
                                {
                                    throw new ExceptionSyntaxError(line, simbol);
                                }
                            }
                            simbol += index - pIndex;
                        }
                        if (isValue)
                        {
                            comma = false;
                            colon = false;
                        }
                        isValue = !isValue;
                    }
                }
            }
            throw new ExceptionSyntaxError(line, simbol);
        }