コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// Write an object on the json
        /// </summary>
        /// <param name="stringBuilder">The string builder</param>
        /// <param name="jsonWriterOptions">The json writer options</param>
        /// <param name="jsonObject">The json object</param>
        /// <param name="jsonPathType">The json path type</param>
        private static void WriteObject(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonObject jsonObject, LazyJsonPathType jsonPathType)
        {
            String openKey  = "{0}{1}{2}";
            String closeKey = "{0}{1}{2}";

            if (jsonWriterOptions.Indent == true)
            {
                if (jsonPathType == LazyJsonPathType.ArrayIndex)
                {
                    openKey  = String.Format(openKey, Environment.NewLine, jsonWriterOptions.IndentValue, "{");
                    closeKey = String.Format(closeKey, Environment.NewLine, jsonWriterOptions.IndentValue, "}");
                }
                else if (jsonPathType == LazyJsonPathType.Property)
                {
                    openKey  = "{";
                    closeKey = String.Format(closeKey, Environment.NewLine, jsonWriterOptions.IndentValue, "}");
                }
            }
            else
            {
                openKey  = "{";
                closeKey = "}";
            }

            stringBuilder.Append(openKey);
            jsonWriterOptions.IndentLevel++;
            for (int i = 0; i < jsonObject.PropertyList.Count; i++)
            {
                WriteProperty(stringBuilder, jsonWriterOptions, jsonObject.PropertyList[i]);
                stringBuilder.Append(",");
            }
            jsonWriterOptions.IndentLevel--;

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

            stringBuilder.Append(closeKey);
        }