Exemple #1
0
        // ----------------------------------------------------------
        private static XTJsonData Parse(XTJsonReader reader, int first, bool isNegative)
        {
            if (first == '.')
            {
                return(ParseDouble(reader, "0", isNegative));
            }

            int chr;

            if (first == '0')
            {
                chr = reader.CurrChar();
                if (chr == 'x' || chr == 'X')
                {
                    reader.SkipChar();
                    return(ParseHIntLong(reader, isNegative));
                }
            }

            // 浮点型或(长)整型
            StringBuilder nums = new StringBuilder(((char)first).ToString());

            do
            {
                chr = reader.CurrChar();
                if (chr >= 48 && chr <= 57)
                {
                    reader.SkipChar();
                }
                else
                {
                    break;
                }
                nums.Append((char)chr);
            } while (chr > 0);

            // 浮点型判断
            if (chr == '.')
            {
                reader.SkipChar();
                return(ParseDouble(reader, nums.ToString(), isNegative));
            }

            // 整型
            long value = long.Parse(nums.ToString());

            if (value > int.MinValue && value < int.MaxValue)
            {
                return(new XTJsonInt(isNegative ? -(int)value : (int)value));
            }
            return(new XTJsonLong(isNegative ? -value : value));
        }
Exemple #2
0
        public static XTJsonData Parse(XTJsonReader reader)
        {
            int chr = reader.CurrUnemptyChar();

            if (chr != '[')
            {
                return(null);
            }
            reader.SkipChar();
            List <XTJsonData> items = new List <XTJsonData>();
            XTJsonData        item  = null;

            do
            {
                chr = reader.CurrUnemptyChar();                                         // 尝试性探测下一个字符
                if (chr == ']')                                                         // 字典结束
                {
                    break;
                }
                else if (chr == ',')                                                                    // 元素结束
                {
                    reader.SkipChar();
                    if (item == null)                                                                   // 错误:{,}
                    {
                        reader.RaiseInvalidException();
                    }
                    chr = reader.CurrUnemptyChar();
                    if (chr == ',')                                                                     // 双逗号分隔错误:[xx, , yy}
                    {
                        reader.RaiseInvalidException();
                    }
                    if (chr == ']')                                                                     // 逗号加括号结束:[xx, yy, ]
                    {
                        break;                                                                          // JSON 本身是不允许的,我这里允许
                    }
                }
                item = reader.ParsePart();
                if (item == null)
                {
                    reader.RaiseInvalidException();
                }
                items.Add(item);
            } while (chr > 0);
            if (reader.NextUnemptyChar() != ']')
            {
                reader.RaiseInvalidException();
            }
            return(new XTJsonList(items));
        }
Exemple #3
0
        public static bool ParsePart(XTJsonReader reader, List <XTJsonComment> doc)
        {
            int first = reader.CurrUnemptyChar();

            if (first <= 0)
            {
                return(false);                                                  // 文档结束
            }
            if (first != '/')
            {
                return(false);                                                  // 非注释区
            }
            reader.SkipChar();

            int snd = reader.NextChar();                                        // 第二个字符

            if (snd == '/')                                                     // 单行注释
            {
                ParseSingleLine(reader, doc);
            }
            else if (snd == '*')                                                        // 多行注释
            {
                ParseMultiLine(reader, doc);
            }
            else
            {
                reader.RaiseInvalidException();                                 // JSON 格式错误
            }
            return(true);
        }
Exemple #4
0
        private static XTJsonData ParseDouble(XTJsonReader reader, string strInt, bool isNegative)
        {
            int           chr;
            StringBuilder nums = new StringBuilder(strInt.ToString());

            nums.Append(".");
            do
            {
                chr = reader.CurrChar();
                if (chr >= 48 && chr <= 57)
                {
                    reader.SkipChar();
                }
                else
                {
                    break;
                }
                nums.Append((char)chr);
            } while (chr > 0);
            string strValue = nums.ToString();

            if (isNegative)
            {
                strValue = "-" + strValue;
            }
            return(new XTJsonDouble(double.Parse(strValue)));
        }
Exemple #5
0
        // 多行注释
        private static void ParseMultiLine(XTJsonReader reader, List <XTJsonComment> doc)
        {
            StringBuilder comment = new StringBuilder();
            int           chr     = reader.NextChar();
            bool          hasEnd  = false;

            while (chr > 0)
            {
                if (chr == '*')
                {
                    if (reader.CurrChar() == '/')
                    {
                        reader.SkipChar();
                        hasEnd = true;
                        break;
                    }
                }
                comment.Append((char)chr);
                chr = reader.NextChar();
            }
            if (!hasEnd)
            {
                reader.RaiseInvalidException();                                 // JSON 格式错误,多行注释没有结束符
            }
            if (doc != null)
            {
                doc.Add(new XTJsonComment(XTJsonCommentType.MultiLine, comment.ToString()));
            }
        }
Exemple #6
0
        public static XTJsonData Parse(XTJsonReader reader)
        {
            int chr = reader.CurrUnemptyChar();

            if (chr != '\"')
            {
                return(null);
            }
            reader.SkipChar();
            StringBuilder sb = new StringBuilder();

            do
            {
                chr = reader.NextChar();
                if (chr == '\"')                                                                                // 字符串结束
                {
                    return(new XTJsonString(sb.ToString()));
                }
                else if (chr == '\\')                                                                   // 转移符
                {
                    sb.Append(TakeESC(reader));
                }
                else
                {
                    sb.Append((char)chr);
                }
            } while (chr > 0);
            reader.RaiseInvalidException();
            return(null);
        }
Exemple #7
0
        public static XTJsonData Parse(XTJsonReader reader)
        {
            int chr = reader.CurrUnemptyChar();

            if (chr != '{')
            {
                return(null);
            }
            reader.SkipChar();                                                                  // 去掉左括号
            Dict     dict    = new Dict();
            bool     isEmpty = true;
            DictItem item;

            do
            {
                chr = reader.CurrUnemptyChar();                                 // 尝试性探测下一个字符
                if (chr == '}')                                                 // 字典结束
                {
                    break;
                }
                else if (chr == ',')                                                            // 元素结束
                {
                    reader.SkipChar();
                    if (isEmpty)                                                                // 错误:{,}
                    {
                        reader.RaiseInvalidException();
                    }
                    chr = reader.CurrUnemptyChar();
                    if (chr == ',')                                                             // 双逗号分隔错误:{xx: yy, , uu: vv}
                    {
                        reader.RaiseInvalidException();
                    }
                    if (chr == '}')                                                             // 逗号加括号结束:{xx: yy, uu: vv, }
                    {
                        break;                                                                  // JSON 本身是不允许的,我这里允许
                    }
                }
                item           = ParseItem(reader);
                dict[item.Key] = item.Value;                                    // 允许添加重复键(dict.Add(...),将不许添加重复键)
                isEmpty        = false;
            }while(chr > 0);
            if (reader.NextUnemptyChar() != '}')
            {
                reader.RaiseInvalidException();
            }
            return(new XTJsonDict(dict));
        }
Exemple #8
0
        public static XTJsonData Parse(XTJsonReader reader)
        {
            int chr = reader.CurrUnemptyChar();

            switch (chr)
            {
            case '-':
                reader.SkipChar();
                return(Parse(reader, reader.NextChar(), true));

            case '+':
                reader.SkipChar();
                return(Parse(reader, reader.NextChar(), false));

            case '.':
                reader.SkipChar();
                return(ParseDouble(reader, "0", false));

            case '0':
                reader.SkipChar();
                chr = reader.CurrChar();
                if (chr == 'x' || chr == 'X')
                {
                    reader.SkipChar();
                    return(ParseHIntLong(reader, false));
                }
                else
                {
                    return(Parse(reader, '0', false));
                }

            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return(Parse(reader, reader.NextChar(), false));
            }
            return(null);
        }
Exemple #9
0
        private static XTJsonData ParseHIntLong(XTJsonReader reader, bool isNegative)
        {
            int           chr;
            StringBuilder nums = new StringBuilder();

            do
            {
                chr = reader.CurrChar();
                if (chr >= 48 && chr <= 57 ||
                    chr >= 'A' && chr <= 'F' ||
                    chr >= 'a' && chr <= 'f')
                {
                    reader.SkipChar();
                }
                else
                {
                    break;
                }
                nums.Append((char)chr);
            } while (chr > 0);


            string strValue = nums.ToString();

            if (strValue == "")
            {
                reader.RaiseInvalidException();
            }
            long value = long.Parse(strValue, NumberStyles.HexNumber);

            if (value > int.MinValue && value < int.MaxValue)
            {
                return(new XTJsonHexInt(isNegative ? -(int)value : (int)value));
            }
            return(new XTJsonHexLong(isNegative ? -value : value));
        }