Beispiel #1
0
 /**
  * Add values from list
  * @param list
  * @return self
  */
 public JArray AddList(IList <object> list)
 {
     foreach (var it in list)
     {
         Add(JUtils.ValueOf(it));
     }
     return(this);
 }
Beispiel #2
0
 /**
  * Add values from dict
  * @param dict {key: value, ...}
  * @return self
  */
 public JObject AddFromDict(IDictionary <string, object> dict)
 {
     foreach (var it in dict)
     {
         Add(it.Key, JUtils.ValueOf(it.Value));
     }
     return(this);
 }
Beispiel #3
0
        /**
         * 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);
        }
Beispiel #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);
        }
Beispiel #5
0
 public JString(string val)
 {
     _Value = JUtils.DecodeEscapeUnicodeString(val);
 }