コード例 #1
0
        /// <summary>
        /// Read an integer or decimal on the json
        /// </summary>
        /// <param name="json">The json</param>
        /// <param name="jsonReaderOptions">The json reader options</param>
        /// <param name="jsonReaderResult">The json reader result</param>
        /// <param name="jsonToken">The json token</param>
        /// <param name="line">The current line</param>
        /// <param name="index">The current index</param>
        private static void ReadIntegerOrDecimal(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, out LazyJsonToken jsonToken, ref Int32 line, ref Int32 index)
        {
            Int32 startIndex = index;

            if (json[index] == '-')
            {
                index++;

                if (index == json.Length || Char.IsDigit(json[index]) == false)
                {
                    jsonToken = new LazyJsonNull();

                    jsonReaderResult.Success = false;
                    jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "0-9", index < json.Length ? json[index] : String.Empty));
                    index = json.Length; // Exit
                    return;
                }
            }

            while (index < json.Length && Char.IsDigit(json[index]) == true)
            {
                index++;
            }

            if (index < json.Length && json[index] == '.')
            {
                index++;
                while (index < json.Length && Char.IsDigit(json[index]) == true)
                {
                    index++;
                }
            }

            String token = json.Substring(startIndex, index - startIndex);

            if (token.Contains('.') == true)
            {
                if (token.Substring(token.IndexOf('.') + 1).Length > 0)
                {
                    jsonToken = new LazyJsonDecimal(Convert.ToDecimal(token.Replace('.', ',')));
                }
                else
                {
                    jsonToken = new LazyJsonNull();

                    jsonReaderResult.Success = false;
                    jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "0-9", index < json.Length ? json[index] : String.Empty));
                    index = json.Length; // Exit
                }
            }
            else
            {
                jsonToken = new LazyJsonInteger(Convert.ToInt64(token));
            }
        }
コード例 #2
0
        /// <summary>
        /// Read null or boolean on the json
        /// </summary>
        /// <param name="json">The json</param>
        /// <param name="jsonReaderOptions">The json reader options</param>
        /// <param name="jsonReaderResult">The json reader result</param>
        /// <param name="jsonToken">The json token</param>
        /// <param name="line">The current line</param>
        /// <param name="index">The current index</param>
        private static void ReadNullOrBoolean(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, out LazyJsonToken jsonToken, ref Int32 line, ref Int32 index)
        {
            Int32 startIndex = index;

            while (index < json.Length && Char.IsLetter(json[index]) == true)
            {
                index++;
            }

            String token = json.Substring(startIndex, index - startIndex);

            if (token.ToLower() == "null")
            {
                jsonToken = new LazyJsonNull();

                if (jsonReaderOptions.CaseSensitive == true && token != "null")
                {
                    jsonReaderResult.Success = false;
                    jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "null", token));
                    index = json.Length; // Exit
                }
            }
            else if (token.ToLower() == "true")
            {
                jsonToken = new LazyJsonBoolean(true);

                if (jsonReaderOptions.CaseSensitive == true && token != "true")
                {
                    jsonReaderResult.Success = false;
                    jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "true", token));
                    index = json.Length; // Exit
                }
            }
            else if (token.ToLower() == "false")
            {
                jsonToken = new LazyJsonBoolean(false);

                if (jsonReaderOptions.CaseSensitive == true && token != "false")
                {
                    jsonReaderResult.Success = false;
                    jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "false", token));
                    index = json.Length; // Exit
                }
            }
            else
            {
                jsonToken = new LazyJsonNull();

                jsonReaderResult.Success = false;
                jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "null|true|false", token));
                index = json.Length; // Exit
            }
        }