예제 #1
0
        /// <summary>
        /// Turns URL encoded content into JSONDataMap
        /// </summary>
        public static JSONDataMap FromURLEncodedString(string content, bool caseSensitive = false)
        {
            var result = new JSONDataMap(caseSensitive);

            if (content.IsNullOrWhiteSpace())
            {
                return(result);
            }

            int queryLen = content.Length;
            int idx      = 0;

            while (idx < queryLen)
            {
                int ampIdx = content.IndexOf('&', idx);
                int kvLen  = (ampIdx != -1) ? ampIdx - idx : queryLen - idx;

                if (kvLen < 1)
                {
                    idx = ampIdx + 1;
                    continue;
                }

                int eqIdx = content.IndexOf('=', idx, kvLen);
                if (eqIdx == -1)
                {
                    var key = Uri.UnescapeDataString(content.Substring(idx, kvLen).Replace('+', ' '));
                    result.Add(key, null);
                }
                else
                {
                    int keyLen = eqIdx - idx;
                    if (keyLen > 0)
                    {
                        var key = Uri.UnescapeDataString(content.Substring(idx, keyLen).Replace('+', ' '));
                        var val = Uri.UnescapeDataString(content.Substring(eqIdx + 1, kvLen - keyLen - 1).Replace('+', ' '));

                        result.Add(key, val);
                    }
                }

                idx += kvLen + 1;
            }

            return(result);
        }