예제 #1
0
 public IndentWriter(IndentWriter writer, int depthDelta)
 {
     this.depth        = Math.Max(writer.depth + depthDelta, 0);
     this.indentStr    = writer.indentStr;
     this.separatorStr = writer.separatorStr;
     this.newLineStr   = writer.newLineStr;
 }
예제 #2
0
        public static void WriteArray(IEnumerable input, StringBuilder sb, IndentWriter indentWriter)
        {
            sb.Append("[");
            var childIndentWriter = indentWriter + 1;

            bool firstElement = true;

            foreach (var child in input)
            {
                if (!firstElement)
                {
                    sb.Append(",");
                }
                indentWriter.WriteLineBreak(sb);

                childIndentWriter.Write(sb);
                WriteElement(child, sb, childIndentWriter);

                firstElement = false;
            }

            indentWriter.WriteLineBreak(sb);
            indentWriter.Write(sb);
            sb.Append("]");
        }
예제 #3
0
 public static void WriteElement(object input, StringBuilder sb, IndentWriter indentWriter)
 {
     if (input is string)
     {
         WriteString((string)input, sb);
     }
     else if (input is char)
     {
         WriteString(input.ToString(), sb);
     }
     else if (input is long)
     {
         WriteLong((long)input, sb);
     }
     else if (input is int)
     {
         WriteInt((int)input, sb);
     }
     else if (input is short)
     {
         WriteShort((short)input, sb);
     }
     else if (input is double)
     {
         WriteDouble((double)input, sb);
     }
     else if (input is float)
     {
         WriteFloat((float)input, sb);
     }
     else if (input is bool)
     {
         WriteBool((bool)input, sb);
     }
     else if (input is null)
     {
         WriteNull(sb);
     }
     else if (IsObject(input))
     {
         WriteObject((IDictionary <string, object>)input, sb, indentWriter);
     }
     else if (IsArray(input))
     {
         WriteArray((IEnumerable)input, sb, indentWriter);
     }
     else
     {
         WriteString(input.ToString(), sb);
     }
 }
예제 #4
0
        public static void WriteObject(IDictionary <string, object> input, StringBuilder sb, IndentWriter indentWriter)
        {
            sb.Append("{");
            var childIndentWriter = indentWriter + 1;

            bool firstElement = true;

            foreach (var child in input)
            {
                if (!firstElement)
                {
                    sb.Append(",");
                }
                indentWriter.WriteLineBreak(sb);

                childIndentWriter.Write(sb);
                WriteString(child.Key, sb);

                sb.Append(":");
                if (IsObject(child.Value) || IsArray(child.Value))
                {
                    childIndentWriter.WriteLineBreak(sb);
                    childIndentWriter.Write(sb);
                }
                else
                {
                    childIndentWriter.WriteSeparator(sb);
                }

                WriteElement(child.Value, sb, childIndentWriter);

                firstElement = false;
            }

            indentWriter.WriteLineBreak(sb);
            indentWriter.Write(sb);
            sb.Append("}");
        }
예제 #5
0
        public static StringBuilder WriteJSONToStringBuilder(object element, StringBuilder stringBuilder = null, IndentWriter indentWriter = null)
        {
            if (indentWriter == null)
            {
                indentWriter = new NoIndentWriter();
            }

            stringBuilder = stringBuilder ?? new StringBuilder();
            WriteElement(element, stringBuilder, indentWriter);

            return(stringBuilder);
        }
예제 #6
0
        public static string WriteJSON(object element, IndentWriter indentWriter = null)
        {
            var stringBuilder = WriteJSONToStringBuilder(element, indentWriter: indentWriter);

            return(stringBuilder.ToString());
        }