Beispiel #1
0
        /// <summary>
        /// Tries to parse a string into a number.
        /// First it tries to parse as Int64 or UInt64 and if the number
        /// </summary>
        internal static object ParseNumber(IObjectFactory factory, string number)
        {
            Int64  resultInt64;
            UInt64 resultUInt64;
            double resultDouble;

            // if the number starts with '-' sign, then it might be Int64, also when it fits the range of Int64 values, we prefere Int64 to be used;
            // otherwise try UInt64, finally if both reading failed, assume it's Double:

            if (number.Length > 0 && number.IndexOf('.') == -1 && number.IndexOf('e') == -1 && number.IndexOf('E') == -1)
            {
                if (NumericHelper.TryParseInt64(number, out resultInt64) && string.Compare(resultInt64.ToString(CultureInfo.InvariantCulture), number, StringComparison.Ordinal) == 0)
                {
                    return(factory.CreateNumber(resultInt64));
                }

                if (number[0] != '-' && NumericHelper.TryParseUInt64(number, out resultUInt64) &&
                    string.Compare(resultUInt64.ToString(CultureInfo.InvariantCulture), number, StringComparison.Ordinal) == 0)
                {
                    return(factory.CreateNumber(resultUInt64));
                }
            }

            if (NumericHelper.TryParseDouble(number, NumberStyles.Float, out resultDouble))
            {
                return(factory.CreateNumber(resultDouble));
            }

            return(null);
        }
        /// <summary>
        /// Parse string as 'Double' and return a factory specific number wrapper.
        /// </summary>
        internal static object ParseDouble(IObjectFactory factory, string data)
        {
            Double number;

            if (NumericHelper.TryParseDouble(data, NumberStyles.Float, out number))
                return factory.CreateNumber(number);
            return null;
        }
Beispiel #3
0
        /// <summary>
        /// Parse string as 'Int64' and return a factory specific number wrapper.
        /// </summary>
        internal static object ParseInt64(IObjectFactory factory, string data)
        {
            Int64 number;

            if (NumericHelper.TryParseInt64(data, out number))
            {
                return(factory.CreateNumber(number));
            }
            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// Parse string as 'Double' and return a factory specific number wrapper.
        /// </summary>
        internal static object ParseDouble(IObjectFactory factory, string data)
        {
            Double number;

            if (NumericHelper.TryParseDouble(data, NumberStyles.Float, out number))
            {
                return(factory.CreateNumber(number));
            }
            return(null);
        }
        /// <summary>
        /// Tries to parse a string into a number.
        /// First it tries to parse as Int64 or UInt64 and if the number 
        /// </summary>
        internal static object ParseNumber(IObjectFactory factory, string number)
        {
            Int64 resultInt64;
            UInt64 resultUInt64;
            double resultDouble;

            // if the number starts with '-' sign, then it might be Int64, also when it fits the range of Int64 values, we prefere Int64 to be used;
            // otherwise try UInt64, finally if both reading failed, assume it's Double:

            if (number.Length > 0 && number.IndexOf('.') == -1 && number.IndexOf('e') == -1 && number.IndexOf('E') == -1)
            {
                if (NumericHelper.TryParseInt64(number, out resultInt64) && string.Compare(resultInt64.ToString(CultureInfo.InvariantCulture), number, StringComparison.Ordinal) == 0)
                    return factory.CreateNumber(resultInt64);

                if (number[0] != '-' && NumericHelper.TryParseUInt64(number, out resultUInt64) &&
                    string.Compare(resultUInt64.ToString(CultureInfo.InvariantCulture), number, StringComparison.Ordinal) == 0)
                    return factory.CreateNumber(resultUInt64);
            }

            if (NumericHelper.TryParseDouble(number, NumberStyles.Float, out resultDouble))
                return factory.CreateNumber(resultDouble);

            return null;
        }
Beispiel #6
0
        /// <summary>
        /// Read number from an input stream.
        /// </summary>
        private object ReadNumber()
        {
            JSonReaderTokenInfo topToken = PopTopToken();

            // top token contains the first letter of current number:
            StringBuilder buffer       = new StringBuilder(topToken.Text);
            int           numberOffset = _input.LineOffset;

            StringHelper.ReadDecimalNumberChars(_input, buffer);

            // verify what kind of character is just after the number, if it's a letter then it is an error,
            // the only allowed are white-chars and JSON object separators (comma, ']', '}')!
            if (!_input.IsEof)
            {
                if (_input.CurrentChar != ',' && !char.IsWhiteSpace(_input.CurrentChar) && _input.CurrentChar != ']' && _input.CurrentChar != '}')
                {
                    buffer.Append(_input.CurrentChar);
                    throw new JSonReaderException("Invalid number", buffer.ToString(), _input.Line, numberOffset);
                }
            }

            // since number has no closing token (as arrays or strings), it might
            // happen that we read too many chars... so put that new char as a token on the
            // stack and instruct reader that token is already there...
            ReadNextToken(char.IsWhiteSpace(_input.CurrentChar));
            _getTokenFromStack = true;

            string number = buffer.ToString();
            object result = _factory.CreateNumber(number);

            if (result != null)
            {
                return(result);
            }

            // number had some invalid format:
            throw new JSonReaderException(string.Concat("Invalid number format, value: \"", number, "\", expected format: ", _factory.Format),
                                          number, _input.Line, numberOffset);
        }
        /// <summary>
        /// Parse string as 'Int32' and return a factory specific number wrapper.
        /// </summary>
        internal static object ParseInt32(IObjectFactory factory, string data)
        {
            Int32 number;

            if (NumericHelper.TryParseInt32(data, out number))
                return factory.CreateNumber(number);
            return null;
        }
Beispiel #8
0
        private object ReadItem(BSonItemType type, out string key)
        {
            int length;
            int dataOffset;

            key        = _input.ReadStringUTF8(int.MaxValue);
            dataOffset = _input.Offset;
            switch (type)
            {
            case BSonItemType.Double:
                return(_factory.CreateNumber(_input.ReadDouble()));

            case BSonItemType.String:
            case BSonItemType.JavaScript:
            case BSonItemType.Symbol:
                length = _input.ReadInt32();
                return(_factory.CreateString(_input.ReadStringUTF8(length)));

            case BSonItemType.Object:
                return(ReadDocument());

            case BSonItemType.Array:
                return(ReadArray());

            case BSonItemType.Binary:
                throw new FormatException("Unsupported 'binary' field at " + dataOffset);

            case BSonItemType.Undefined:
                throw new FormatException("Unsupported 'undefined' field at " + dataOffset);

            case BSonItemType.ObjectID:
                throw new FormatException("Unsupported 'object-id' field at " + dataOffset);

            case BSonItemType.Boolean:
                return(_factory.CreateKeyword(_input.ReadByte() > 0 ? JSonReader.TrueTokenData : JSonReader.FalseTokenData));

            case BSonItemType.DateTime:
                return(_factory.CreateNumber(_input.ReadUInt64()));

            case BSonItemType.Null:
                return(_factory.CreateKeyword(JSonReader.FalseTokenData));

            case BSonItemType.RegExp:
                return(ReadRegExp());

            case BSonItemType.DbPointer:
                throw new FormatException("Unsupported 'DbPointer' field at " + dataOffset);

            case BSonItemType.JavaScriptScoped:
                return(ReadJavaScriptScoped());

            case BSonItemType.Int32:
                return(_factory.CreateNumber(_input.ReadInt32()));

            case BSonItemType.Timestamp:
                return(_factory.CreateNumber(_input.ReadInt64()));

            case BSonItemType.Int64:
                return(_factory.CreateNumber(_input.ReadInt64()));

            default:
                throw new FormatException("Unsupported '" + type + "' field at " + dataOffset);
            }
        }