Beispiel #1
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()));
            }
        }
Beispiel #2
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)));
        }
Beispiel #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);
        }
Beispiel #4
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);
        }
Beispiel #5
0
        // 单行注释
        private static void ParseSingleLine(XTJsonReader reader, List <XTJsonComment> doc)
        {
            string line = reader.NextLine();

            if (doc != null)
            {
                doc.Add(new XTJsonComment(XTJsonCommentType.SingleLine, line));
            }
        }
Beispiel #6
0
        public static bool Parse(XTJsonReader reader, List <XTJsonComment> doc = null)
        {
            bool hasComment = false;

            while (ParsePart(reader, doc))
            {
                hasComment = true;
            }
            return(hasComment);
        }
Beispiel #7
0
        private static string TakeESC(XTJsonReader reader)
        {
            int chr = reader.NextChar();

            if (!sm_escs.Contains(chr))
            {
                reader.RaiseInvalidException();
            }
            return("\\" + (char)chr);
        }
Beispiel #8
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));
        }
Beispiel #9
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));
        }
Beispiel #10
0
        public static XTJsonData Parse(XTJsonReader reader)
        {
            int chr = reader.CurrUnemptyChar();

            if (chr != 'n')
            {
                return(null);
            }
            string text = reader.NextBlock(4);

            if (text != "null")
            {
                reader.RaiseInvalidException();
            }
            return(XTJsonNone.Inst);
        }
Beispiel #11
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));
        }
Beispiel #12
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);
        }
Beispiel #13
0
        private static DictItem ParseItem(XTJsonReader reader)
        {
            XTJsonData key = reader.ParsePart();

            if (!(key is XTJsonKeyable))                                        // 改数据不能作为字典的 key
            {
                reader.RaiseInvalidException();
            }
            int sp = reader.NextUnemptyChar();

            if (sp != ':')
            {
                reader.RaiseInvalidException();
            }
            XTJsonData value = reader.ParsePart();

            if (value == null)
            {
                reader.RaiseInvalidException();
            }
            return(new DictItem((XTJsonKeyable)key, value));
        }
Beispiel #14
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));
        }
Beispiel #15
0
        public static XTJsonData Parse(XTJsonReader reader)
        {
            int chr = reader.CurrUnemptyChar();

            if (chr == 't')
            {
                string text = reader.NextBlock(4);
                if (text == "true")
                {
                    return(new XTJsonBool(true));
                }
                reader.RaiseInvalidException();
            }
            else if (chr == 'f')
            {
                string text = reader.NextBlock(5);
                if (text == "false")
                {
                    return(new XTJsonBool(false));
                }
                reader.RaiseInvalidException();
            }
            return(null);
        }