コード例 #1
0
        public static Dictionary ToDictionary(string strText)
        {
            strText = strText.Trim();
            Debug.Assert(!string.IsNullOrEmpty(strText));
            if (strText[0] != '{' || strText[strText.Length - 1] != '}')
            {
                return(null);
            }

            Dictionary dict = new Dictionary();

            strText = strText.Substring(1, strText.Length - 2); // remove '{' & '}'
            int begin = 0;
            int end   = strText.Length;

            while (begin < end)
            {
                int        beginKey = begin;
                int        endKey   = beginKey;
                MatchStack stackKey = new MatchStack();
                while (endKey < end && (!stackKey.IsEmpty() || strText[endKey] != ':'))
                {
                    stackKey.Push(strText[endKey]);
                    endKey++;
                }
                Debug.Assert(endKey < end);
                string key = strText.Substring(beginKey, endKey - beginKey).Trim();
                Debug.Assert(key.Length < 2 || (key[0] == '"') == (key[key.Length - 1] == '"'));
                if (key.Length >= 2 && key[0] == '"' && key[key.Length - 1] == '"')
                {
                    key = key.Substring(1, key.Length - 2);
                }

                int        beginValue = endKey + 1;
                int        endValue   = beginValue;
                MatchStack stackValue = new MatchStack();
                while (endValue < end && (!stackValue.IsEmpty() || strText[endValue] != ','))
                {
                    stackValue.Push(strText[endValue]);
                    endValue++;
                }
                string value = strText.Substring(beginValue, endValue - beginValue).Trim();
                Debug.Assert(value.Length < 2 || (value[0] == '"') == (value[value.Length - 1] == '"'));
                if (value.Length >= 2 && value[0] == '"' && value[value.Length - 1] == '"')
                {
                    value = value.Substring(1, value.Length - 2);
                }

                begin = endValue + 1;

                Debug.Assert(!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value));
                dict.Add(key, value);
            }

            return(dict);
        }
コード例 #2
0
        public static Array ToArray(string strText)
        {
            strText = strText.Trim();
            Debug.Assert(!string.IsNullOrEmpty(strText));
            if (strText[0] != '[' || strText[strText.Length - 1] != ']')
            {
                return(null);
            }

            Array array = new Array();

            strText = strText.Substring(1, strText.Length - 2); // remove '[' & ']'
            int begin = 0;
            int end   = strText.Length;

            while (begin < end)
            {
                int        beginItem = begin;
                int        endItem   = beginItem;
                MatchStack stackKey  = new MatchStack();
                while (endItem < end && (!stackKey.IsEmpty() || strText[endItem] != ','))
                {
                    stackKey.Push(strText[endItem]);
                    endItem++;
                }
                string item = strText.Substring(beginItem, endItem - beginItem).Trim();
                Debug.Assert(item.Length < 2 || (item[0] == '"') == (item[item.Length - 1] == '"'));
                if (item.Length >= 2 && item[0] == '"' && item[item.Length - 1] == '"')
                {
                    item = item.Substring(1, item.Length - 2);
                }

                begin = endItem + 1;

                Debug.Assert(!string.IsNullOrEmpty(item));
                array.Add(item);
            }

            return(array);
        }