Esempio n. 1
0
        private CBORObject NextJSONNegativeNumber(
            int[] nextChar)
        {
            // Assumes the last character read was '-'
            // DebugUtility.Log("js=" + (jstring));
            CBORObject obj;
            int        numberStartIndex = this.index - 1;
            int        c = this.index < this.endPos ? ((int)this.jstring[this.index++]) &
                           0xffff : -1;

            if (c < '0' || c > '9')
            {
                this.RaiseError("JSON number can't be parsed.");
            }
            if (this.index < this.endPos && c != '0')
            {
                // Check for negative single-digit
                int c2 = ((int)this.jstring[this.index]) & 0xffff;
                if (c2 == ',' || c2 == ']' || c2 == '}')
                {
                    ++this.index;
                    obj = CBORDataUtilities.ParseSmallNumberAsNegative(
                        c - '0',
                        this.options);
                    nextChar[0] = c2;
                    return(obj);
                }
                else if (c2 == 0x20 || c2 == 0x0a || c2 == 0x0d || c2 == 0x09)
                {
                    ++this.index;
                    obj = CBORDataUtilities.ParseSmallNumberAsNegative(
                        c - '0',
                        this.options);
                    nextChar[0] = this.SkipWhitespaceJSON();
                    return(obj);
                }
            }
            // NOTE: Differs from CBORJson2, notably because the whole
            // rest of the string is checked whether the beginning of the rest
            // is a JSON number
            var endIndex = new int[1];

            endIndex[0] = numberStartIndex;
            obj         = CBORDataUtilitiesTextString.ParseJSONNumber(
                this.jstring,
                numberStartIndex,
                this.endPos - numberStartIndex,
                this.options,
                endIndex);
            int numberEndIndex = endIndex[0];

            this.index = numberEndIndex >= this.endPos ? this.endPos :
                         (numberEndIndex + 1);
            if (obj == null)
            {
                int    strlen = numberEndIndex - numberStartIndex;
                string errstr = this.jstring.Substring(numberStartIndex,
                                                       Math.Min(100, strlen));
                if (strlen > 100)
                {
                    errstr += "...";
                }
                this.RaiseError("JSON number can't be parsed. " + errstr);
            }
      #if DEBUG
            if (numberEndIndex < numberStartIndex)
            {
                throw new ArgumentException("numberEndIndex (" + numberEndIndex +
                                            ") is not greater or equal to " + numberStartIndex);
            }
      #endif
            c = numberEndIndex >= this.endPos ? -1 : this.jstring[numberEndIndex];
            // check if character can validly appear after a JSON number
            if (c != ',' && c != ']' && c != '}' && c != -1 &&
                c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09)
            {
                this.RaiseError("Invalid character after JSON number");
            }
            // DebugUtility.Log("endIndex="+endIndex[0]+", "+
            // this.jstring.Substring(endIndex[0],
            // Math.Min(20, this.endPos-endIndex[0])));
            if (c == -1 || (c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09))
            {
                nextChar[0] = c;
            }
            else
            {
                nextChar[0] = this.SkipWhitespaceJSON();
            }
            return(obj);
        }
Esempio n. 2
0
        private CBORObject NextJSONNegativeNumber(
            int[] nextChar)
        {
            // Assumes the last character read was '-'
            CBORObject obj;
            int        numberStartIndex = this.index - 1;
            int        c = this.index < this.endPos ? ((int)this.bytes[this.index++]) &
                           0xff : -1;

            if (c < '0' || c > '9')
            {
                this.RaiseError("JSON number can't be parsed.");
            }
            int cstart = c;

            while (c == '-' || c == '+' || c == '.' || (c >= '0' && c <= '9') ||
                   c == 'e' || c == 'E')
            {
                c = this.index < this.endPos ? ((int)this.bytes[this.index++]) &
                    0xff : -1;
            }
            // check if character can validly appear after a JSON number
            if (c != ',' && c != ']' && c != '}' && c != -1 &&
                c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09)
            {
                this.RaiseError("Invalid character after JSON number");
            }
            int numberEndIndex = c < 0 ?
                                 this.endPos : this.index - 1;

            if (numberEndIndex - numberStartIndex == 2 && cstart != '0')
            {
                // Negative single digit other than negative zero
                obj = CBORDataUtilities.ParseSmallNumberAsNegative((int)(cstart
                                                                         - '0'),
                                                                   this.options);
            }
            else
            {
                obj = CBORDataUtilities.ParseJSONNumber(
                    this.bytes,
                    numberStartIndex,
                    numberEndIndex - numberStartIndex,
                    this.options);
        #if DEBUG
                if (this.options.NumberConversion == JSONOptions.ConversionMode.Full &&
                    (
                        (EDecimal)obj.ToObject(
                            typeof(EDecimal))).CompareToValue(EDecimal.FromString(this.bytes,
                                                                                  numberStartIndex,
                                                                                  numberEndIndex - numberStartIndex)) != 0)
                {
                    this.RaiseError(String.Empty + obj);
                }
        #endif
                if (obj == null)
                {
                    string errstr = String.Empty;
                    // errstr = (str.Length <= 100) ? str : (str.Substring(0,
                    // 100) + "...");
                    this.RaiseError("JSON number can't be parsed. " + errstr);
                }
            }
            if (c == -1 || (c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09))
            {
                nextChar[0] = c;
            }
            else
            {
                nextChar[0] = this.SkipWhitespaceJSON();
            }
            return(obj);
        }
        private CBORObject NextJSONNegativeNumber(
            int[] nextChar)
        {
            // Assumes the last character read was '-'
            CBORObject obj;
            int        numberStartIndex = this.index - 1;
            int        c = this.index < this.endPos ? ((int)this.bytes[this.index++]) &
                           0xff : -1;

            if (c < '0' || c > '9')
            {
                this.RaiseError("JSON number can't be parsed.");
            }
            int cstart = c;

            while (c == '-' || c == '+' || c == '.' || (c >= '0' && c <= '9') ||
                   c == 'e' || c == 'E')
            {
                c = this.index < this.endPos ? ((int)this.bytes[this.index++]) &
                    0xff : -1;
            }
            // check if character can validly appear after a JSON number
            if (c != ',' && c != ']' && c != '}' && c != -1 &&
                c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09)
            {
                this.RaiseError("Invalid character after JSON number");
            }
            int numberEndIndex = c < 0 ?
                                 this.endPos : this.index - 1;

            if (numberEndIndex - numberStartIndex == 2 && cstart != '0')
            {
                // Negative single digit other than negative zero
                obj = CBORDataUtilities.ParseSmallNumberAsNegative((int)(cstart
                                                                         - '0'),
                                                                   this.options);
            }
            else
            {
                var ssb = new StringBuilder(numberEndIndex - numberStartIndex);
                int ki;
                for (ki = numberStartIndex; ki < numberEndIndex; ++ki)
                {
                    ssb.Append((char)(((int)this.bytes[ki]) & 0xff));
                }
                string str = ssb.ToString();
                obj = CBORDataUtilities.ParseJSONNumber(str, this.options);
                if (obj == null)
                {
                    string errstr = (str.Length <= 100) ? str : (str.Substring(0,
                                                                               100) + "...");
                    this.RaiseError("JSON number can't be parsed. " + errstr);
                }
            }
            if (c == -1 || (c != 0x20 && c != 0x0a && c != 0x0d && c != 0x09))
            {
                nextChar[0] = c;
            }
            else
            {
                nextChar[0] = this.SkipWhitespaceJSON();
            }
            return(obj);
        }