Example #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));
            }
        }
Example #2
0
        /// <summary>
        /// Deserialize the json integer token to the desired object
        /// </summary>
        /// <param name="jsonToken">The json integer token to be deserialized</param>
        /// <param name="dataType">The type of the desired object</param>
        /// <returns>The desired object instance</returns>
        public override Object Deserialize(LazyJsonToken jsonToken, Type dataType)
        {
            if (jsonToken == null || dataType == null)
            {
                return(null);
            }

            if (jsonToken.Type == LazyJsonType.Null)
            {
                if (dataType == typeof(Int16))
                {
                    return(0);
                }
                if (dataType == typeof(Int32))
                {
                    return(0);
                }
                if (dataType == typeof(Int64))
                {
                    return(0);
                }
                if (dataType == typeof(Object))
                {
                    return(0);
                }
                if (dataType == typeof(Nullable <Int16>))
                {
                    return(null);
                }
                if (dataType == typeof(Nullable <Int32>))
                {
                    return(null);
                }
                if (dataType == typeof(Nullable <Int64>))
                {
                    return(null);
                }
            }
            else if (jsonToken.Type == LazyJsonType.Integer)
            {
                LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonToken;

                if (dataType == typeof(Int16))
                {
                    return(jsonInteger.Value == null ? 0 : Convert.ToInt16(jsonInteger.Value));
                }
                if (dataType == typeof(Int32))
                {
                    return(jsonInteger.Value == null ? 0 : Convert.ToInt32(jsonInteger.Value));
                }
                if (dataType == typeof(Int64))
                {
                    return(jsonInteger.Value == null ? 0 : Convert.ToInt64(jsonInteger.Value));
                }
                if (dataType == typeof(Object))
                {
                    return(jsonInteger.Value == null ? 0 : Convert.ToInt64(jsonInteger.Value));
                }
                if (dataType == typeof(Nullable <Int16>))
                {
                    return(jsonInteger.Value == null ? null : Convert.ToInt16(jsonInteger.Value));
                }
                if (dataType == typeof(Nullable <Int32>))
                {
                    return(jsonInteger.Value == null ? null : Convert.ToInt32(jsonInteger.Value));
                }
                if (dataType == typeof(Nullable <Int64>))
                {
                    return(jsonInteger.Value == null ? null : Convert.ToInt64(jsonInteger.Value));
                }
            }

            return(null);
        }
        /// <summary>
        /// Write the root node of the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonToken">The root token</param>
        private static void WriteRoot(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonToken jsonToken)
        {
            switch (jsonToken.Type)
            {
            case LazyJsonType.Null:
                #region LazyJsonType.Null

                stringBuilder.Append("null");

                #endregion LazyJsonType.Null
                break;

            case LazyJsonType.Boolean:
                #region LazyJsonType.Boolean

                LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonToken;
                stringBuilder.Append(jsonBoolean.Value == null ? "null" : jsonBoolean.Value.ToString().ToLower());

                #endregion LazyJsonType.Boolean
                break;

            case LazyJsonType.Integer:
                #region LazyJsonType.Integer

                LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonToken;
                stringBuilder.Append(jsonInteger.Value == null ? "null" : jsonInteger.Value);

                #endregion LazyJsonType.Integer
                break;

            case LazyJsonType.Decimal:
                #region LazyJsonType.Decimal

                LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonToken;
                stringBuilder.Append(jsonDecimal.Value == null ? "null" : jsonDecimal.Value.ToString().Replace(',', '.'));

                #endregion LazyJsonType.Decimal
                break;

            case LazyJsonType.String:
                #region LazyJsonType.String

                LazyJsonString jsonString = (LazyJsonString)jsonToken;
                stringBuilder.Append(jsonString.Value == null ? "null" : "\"" + jsonString.Value + "\"");

                #endregion LazyJsonType.String
                break;

            case LazyJsonType.Object:
                #region LazyJsonType.Object

                WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonToken, LazyJsonPathType.Property);

                #endregion LazyJsonType.Object
                break;

            case LazyJsonType.Array:
                #region LazyJsonType.Array

                WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonToken, LazyJsonPathType.Property);

                #endregion LazyJsonType.Array
                break;
            }
        }
        /// <summary>
        /// Write a property on the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonProperty">The json property</param>
        private static void WriteProperty(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonProperty jsonProperty)
        {
            String property = "{0}{1}\"{2}\"{3}";

            if (jsonWriterOptions.Indent == true)
            {
                property = String.Format(property, Environment.NewLine, jsonWriterOptions.IndentValue, jsonProperty.Name, ": ");
            }
            else
            {
                property = String.Format(property, String.Empty, String.Empty, jsonProperty.Name, ":");
            }

            stringBuilder.Append(property);

            switch (jsonProperty.Token.Type)
            {
            case LazyJsonType.Null:
                #region LazyJsonType.Null

                stringBuilder.Append("null");

                #endregion LazyJsonType.Null
                break;

            case LazyJsonType.Boolean:
                #region LazyJsonType.Boolean

                LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonProperty.Token;
                stringBuilder.Append(jsonBoolean.Value == null ? "null" : jsonBoolean.Value.ToString().ToLower());

                #endregion LazyJsonType.Boolean
                break;

            case LazyJsonType.Integer:
                #region LazyJsonType.Integer

                LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonProperty.Token;
                stringBuilder.Append(jsonInteger.Value == null ? "null" : jsonInteger.Value);

                #endregion LazyJsonType.Integer
                break;

            case LazyJsonType.Decimal:
                #region LazyJsonType.Decimal

                LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonProperty.Token;
                stringBuilder.Append(jsonDecimal.Value == null ? "null" : jsonDecimal.Value.ToString().Replace(',', '.'));

                #endregion LazyJsonType.Decimal
                break;

            case LazyJsonType.String:
                #region LazyJsonType.String

                LazyJsonString jsonString = (LazyJsonString)jsonProperty.Token;
                stringBuilder.Append(jsonString.Value == null ? "null" : "\"" + jsonString.Value + "\"");

                #endregion LazyJsonType.String
                break;

            case LazyJsonType.Object:
                #region LazyJsonType.Object

                WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonProperty.Token, LazyJsonPathType.Property);

                #endregion LazyJsonType.Object
                break;

            case LazyJsonType.Array:
                #region LazyJsonType.Array

                WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonProperty.Token, LazyJsonPathType.Property);

                #endregion LazyJsonType.Array
                break;
            }
        }
        /// <summary>
        /// Write an array on the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonArray">The json array</param>
        /// <param name="jsonPathType">The json path type</param>
        private static void WriteArray(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonArray jsonArray, LazyJsonPathType jsonPathType)
        {
            String value        = String.Empty;
            String openBracket  = "{0}{1}{2}";
            String closeBracket = "{0}{1}{2}";

            if (jsonWriterOptions.Indent == true)
            {
                jsonWriterOptions.IndentLevel++;
                value = Environment.NewLine + jsonWriterOptions.IndentValue + "{0}";
                jsonWriterOptions.IndentLevel--;

                if (jsonPathType == LazyJsonPathType.ArrayIndex)
                {
                    openBracket  = String.Format(openBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "[");
                    closeBracket = String.Format(closeBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "]");
                }
                else if (jsonPathType == LazyJsonPathType.Property)
                {
                    openBracket  = "[";
                    closeBracket = String.Format(closeBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "]");
                }
            }
            else
            {
                value        = "{0}";
                openBracket  = "[";
                closeBracket = "]";
            }

            stringBuilder.Append(openBracket);
            for (int i = 0; i < jsonArray.TokenList.Count; i++)
            {
                switch (jsonArray.TokenList[i].Type)
                {
                case LazyJsonType.Null:
                    #region LazyJsonType.Null

                    stringBuilder.Append(String.Format(value, "null"));

                    #endregion LazyJsonType.Null
                    break;

                case LazyJsonType.Boolean:
                    #region LazyJsonType.Boolean

                    LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonBoolean.Value == null ? "null" : jsonBoolean.Value.ToString().ToLower()));

                    #endregion LazyJsonType.Boolean
                    break;

                case LazyJsonType.Integer:
                    #region LazyJsonType.Integer

                    LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonInteger.Value == null ? "null" : jsonInteger.Value));

                    #endregion LazyJsonType.Integer
                    break;

                case LazyJsonType.Decimal:
                    #region LazyJsonType.Decimal

                    LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonDecimal.Value == null ? "null" : jsonDecimal.Value.ToString().Replace(',', '.')));

                    #endregion LazyJsonType.Decimal
                    break;

                case LazyJsonType.String:
                    #region LazyJsonType.String

                    LazyJsonString jsonString = (LazyJsonString)jsonArray.TokenList[i];
                    stringBuilder.Append(String.Format(value, jsonString.Value == null ? "null" : "\"" + jsonString.Value + "\""));

                    #endregion LazyJsonType.String
                    break;

                case LazyJsonType.Object:
                    #region LazyJsonType.Object

                    jsonWriterOptions.IndentLevel++;
                    WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonArray.TokenList[i], LazyJsonPathType.ArrayIndex);
                    jsonWriterOptions.IndentLevel--;

                    #endregion LazyJsonType.Object
                    break;

                case LazyJsonType.Array:
                    #region LazyJsonType.Array

                    jsonWriterOptions.IndentLevel++;
                    WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonArray.TokenList[i], LazyJsonPathType.ArrayIndex);
                    jsonWriterOptions.IndentLevel--;

                    #endregion LazyJsonType.Array
                    break;
                }
                stringBuilder.Append(",");
            }

            if (stringBuilder[stringBuilder.Length - 1] == ',')
            {
                stringBuilder.Remove(stringBuilder.Length - 1, 1);
            }

            stringBuilder.Append(closeBracket);
        }
Example #6
0
 /// <summary>
 /// Append a json integer token at specified jPath
 /// </summary>
 /// <param name="jPath">The jPath location to append the token</param>
 /// <param name="jsonInteger">The json integer token</param>
 public void AppendInteger(String jPath, LazyJsonInteger jsonInteger)
 {
     AppendToken(jPath, jsonInteger);
 }