コード例 #1
0
        static private dynamic ParseJsonBasicValue(char[] jsonChar, ref int left, int right)
        {
            JsonBasicValue node = null;
            dynamic        ret;

            SkipWhites(jsonChar, ref left, right);

            char target = jsonChar[left];

            if (target == 'n')
            {
                node = new JsonNull();
            }
            else if (boolCandidates.Contains(target))
            {
                node = new JsonBool();
            }
            else if (target == '"')
            {
                node = new JsonString();
            }
            else if (target == '[')
            {
                int tleft = left + 1;
                ret = ParseJsonList(jsonChar, ref tleft, right);
                if (jsonChar[tleft] != ']')
                {
                    throw new XmlException("Invalid json. (List)");
                }
                left = tleft + 1;

                SkipWhites(jsonChar, ref left, right);
                return(ret);
            }
            else if (target == '{')
            {
                int tleft = left + 1;
                ret = ParseJsonObject(jsonChar, ref tleft, right);
                if (jsonChar[tleft] != '}')
                {
                    throw new XmlException(DumpInvalidJson(jsonChar, tleft, right));
                }
                left = tleft + 1;
                return(ret);
            }
            else
            {
                node = new JsonNumber();
            }
            char[] remainChars = new char[right - left];
            Array.Copy(jsonChar, left, remainChars, 0, right - left);
            int length;

            try
            {
                ret = node.Get(new String(remainChars), out length);
            }
            catch (XmlException)
            {
                throw new XmlException(DumpInvalidJson(jsonChar, left, right));
            }
            catch (InvalidCastException)
            {
                throw new XmlException(DumpInvalidJson(jsonChar, left, right));
            }
            left += length;
            SkipWhites(jsonChar, ref left, right);

            return(ret);
        }
コード例 #2
0
        static private Dictionary <string, dynamic> ParseJsonObject(char[] jsonChar, ref int left, int right)
        {
            JsonString node;
            Dictionary <string, dynamic> ret = new Dictionary <string, dynamic>();

            SkipWhites(jsonChar, ref left, right);

            if (left >= right)
            {
                throw new XmlException(DumpInvalidJson(jsonChar, right, right));
            }

            if (jsonChar[left] == '}')
            {
                return(ret);
            }

            while (left < right)
            {
                char target = jsonChar[left];

                node = new JsonString();
                char[] remainChars = new char[right - left];
                Array.Copy(jsonChar, left, remainChars, 0, right - left);
                int    length;
                string str;
                try
                {
                    str = node.Get(new String(remainChars), out length);
                }
                catch (XmlException)
                {
                    throw new XmlException(DumpInvalidJson(jsonChar, left, right));
                }
                left += length;
                SkipWhites(jsonChar, ref left, right);

                if (left >= right)
                {
                    throw new XmlException(DumpInvalidJson(jsonChar, right, right));
                }

                if (jsonChar[left] != ':')
                {
                    throw new XmlException(DumpInvalidJson(jsonChar, left, right));
                }
                left++;
                SkipWhites(jsonChar, ref left, right);

                if (left >= right)
                {
                    throw new XmlException(DumpInvalidJson(jsonChar, right, right));
                }

                dynamic val = ParseJsonBasicValue(jsonChar, ref left, right);
                ret.Add(str, val);

                SkipWhites(jsonChar, ref left, right);

                if (left >= right)
                {
                    throw new XmlException(DumpInvalidJson(jsonChar, right, right));
                }

                target = jsonChar[left];
                if (target == '}')
                {
                    break;
                }
                if (target != ',')
                {
                    throw new XmlException(DumpInvalidJson(jsonChar, left, right));
                }
                left++;
            }
            return(ret);
        }