123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Text;
- namespace LitJson
- {
- public class JsonWriter
- {
- public JsonWriter()
- {
- this.inst_string_builder = new StringBuilder();
- this.writer = new StringWriter(this.inst_string_builder);
- this.Init();
- }
- public JsonWriter(StringBuilder sb) : this(new StringWriter(sb))
- {
- }
- public JsonWriter(TextWriter writer)
- {
- if (writer == null)
- {
- throw new ArgumentNullException("writer");
- }
- this.writer = writer;
- this.Init();
- }
- public int IndentValue
- {
- get
- {
- return this.indent_value;
- }
- set
- {
- this.indentation = this.indentation / this.indent_value * value;
- this.indent_value = value;
- }
- }
- public bool PrettyPrint
- {
- get
- {
- return this.pretty_print;
- }
- set
- {
- this.pretty_print = value;
- }
- }
- public TextWriter TextWriter
- {
- get
- {
- return this.writer;
- }
- }
- public bool Validate
- {
- get
- {
- return this.validate;
- }
- set
- {
- this.validate = value;
- }
- }
- private void DoValidation(Condition cond)
- {
- if (!this.context.ExpectingValue)
- {
- this.context.Count++;
- }
- if (!this.validate)
- {
- return;
- }
- if (this.has_reached_end)
- {
- throw new JsonException("A complete JSON symbol has already been written");
- }
- switch (cond)
- {
- case Condition.InArray:
- if (!this.context.InArray)
- {
- throw new JsonException("Can't close an array here");
- }
- break;
- case Condition.InObject:
- if (!this.context.InObject || this.context.ExpectingValue)
- {
- throw new JsonException("Can't close an object here");
- }
- break;
- case Condition.NotAProperty:
- if (this.context.InObject && !this.context.ExpectingValue)
- {
- throw new JsonException("Expected a property");
- }
- break;
- case Condition.Property:
- if (!this.context.InObject || this.context.ExpectingValue)
- {
- throw new JsonException("Can't add a property here");
- }
- break;
- case Condition.Value:
- if (!this.context.InArray && (!this.context.InObject || !this.context.ExpectingValue))
- {
- throw new JsonException("Can't add a value here");
- }
- break;
- }
- }
- private void Init()
- {
- this.has_reached_end = false;
- this.hex_seq = new char[4];
- this.indentation = 0;
- this.indent_value = 4;
- this.pretty_print = false;
- this.validate = true;
- this.ctx_stack = new Stack<WriterContext>();
- this.context = new WriterContext();
- this.ctx_stack.Push(this.context);
- }
- private static void IntToHex(int n, char[] hex)
- {
- for (int i = 0; i < 4; i++)
- {
- int num = n % 16;
- if (num < 10)
- {
- hex[3 - i] = (char)(48 + num);
- }
- else
- {
- hex[3 - i] = (char)(65 + (num - 10));
- }
- n >>= 4;
- }
- }
- private void Indent()
- {
- if (this.pretty_print)
- {
- this.indentation += this.indent_value;
- }
- }
- private void Put(string str)
- {
- if (this.pretty_print && !this.context.ExpectingValue)
- {
- for (int i = 0; i < this.indentation; i++)
- {
- this.writer.Write(' ');
- }
- }
- this.writer.Write(str);
- }
- private void PutNewline()
- {
- this.PutNewline(true);
- }
- private void PutNewline(bool add_comma)
- {
- if (add_comma && !this.context.ExpectingValue && this.context.Count > 1)
- {
- this.writer.Write(',');
- }
- if (this.pretty_print && !this.context.ExpectingValue)
- {
- this.writer.Write('\n');
- }
- }
- private void PutString(string str)
- {
- this.Put(string.Empty);
- this.writer.Write('"');
- int length = str.Length;
- for (int i = 0; i < length; i++)
- {
- char c = str[i];
- switch (c)
- {
- case '\b':
- this.writer.Write("\\b");
- break;
- case '\t':
- this.writer.Write("\\t");
- break;
- case '\n':
- this.writer.Write("\\n");
- break;
- default:
- if (c != '"' && c != '\\')
- {
- if (str[i] >= ' ' && str[i] <= '~')
- {
- this.writer.Write(str[i]);
- }
- else
- {
- JsonWriter.IntToHex((int)str[i], this.hex_seq);
- this.writer.Write("\\u");
- this.writer.Write(this.hex_seq);
- }
- }
- else
- {
- this.writer.Write('\\');
- this.writer.Write(str[i]);
- }
- break;
- case '\f':
- this.writer.Write("\\f");
- break;
- case '\r':
- this.writer.Write("\\r");
- break;
- }
- }
- this.writer.Write('"');
- }
- private void Unindent()
- {
- if (this.pretty_print)
- {
- this.indentation -= this.indent_value;
- }
- }
- public override string ToString()
- {
- if (this.inst_string_builder == null)
- {
- return string.Empty;
- }
- return this.inst_string_builder.ToString();
- }
- public void Reset()
- {
- this.has_reached_end = false;
- this.ctx_stack.Clear();
- this.context = new WriterContext();
- this.ctx_stack.Push(this.context);
- if (this.inst_string_builder != null)
- {
- this.inst_string_builder.Remove(0, this.inst_string_builder.Length);
- }
- }
- public void Write(bool boolean)
- {
- this.DoValidation(Condition.Value);
- this.PutNewline();
- this.Put((!boolean) ? "false" : "true");
- this.context.ExpectingValue = false;
- }
- public void Write(decimal number)
- {
- this.DoValidation(Condition.Value);
- this.PutNewline();
- this.Put(Convert.ToString(number, JsonWriter.number_format));
- this.context.ExpectingValue = false;
- }
- public void Write(double number)
- {
- this.DoValidation(Condition.Value);
- this.PutNewline();
- string text = Convert.ToString(number, JsonWriter.number_format);
- this.Put(text);
- if (text.IndexOf('.') == -1 && text.IndexOf('E') == -1)
- {
- this.writer.Write(".0");
- }
- this.context.ExpectingValue = false;
- }
- public void Write(int number)
- {
- this.DoValidation(Condition.Value);
- this.PutNewline();
- this.Put(Convert.ToString(number, JsonWriter.number_format));
- this.context.ExpectingValue = false;
- }
- public void Write(long number)
- {
- this.DoValidation(Condition.Value);
- this.PutNewline();
- this.Put(Convert.ToString(number, JsonWriter.number_format));
- this.context.ExpectingValue = false;
- }
- public void Write(string str)
- {
- this.DoValidation(Condition.Value);
- this.PutNewline();
- if (str == null)
- {
- this.Put("null");
- }
- else
- {
- this.PutString(str);
- }
- this.context.ExpectingValue = false;
- }
- public void Write(ulong number)
- {
- this.DoValidation(Condition.Value);
- this.PutNewline();
- this.Put(Convert.ToString(number, JsonWriter.number_format));
- this.context.ExpectingValue = false;
- }
- public void WriteArrayEnd()
- {
- this.DoValidation(Condition.InArray);
- this.PutNewline(false);
- this.ctx_stack.Pop();
- if (this.ctx_stack.Count == 1)
- {
- this.has_reached_end = true;
- }
- else
- {
- this.context = this.ctx_stack.Peek();
- this.context.ExpectingValue = false;
- }
- this.Unindent();
- this.Put("]");
- }
- public void WriteArrayStart()
- {
- this.DoValidation(Condition.NotAProperty);
- this.PutNewline();
- this.Put("[");
- this.context = new WriterContext();
- this.context.InArray = true;
- this.ctx_stack.Push(this.context);
- this.Indent();
- }
- public void WriteObjectEnd()
- {
- this.DoValidation(Condition.InObject);
- this.PutNewline(false);
- this.ctx_stack.Pop();
- if (this.ctx_stack.Count == 1)
- {
- this.has_reached_end = true;
- }
- else
- {
- this.context = this.ctx_stack.Peek();
- this.context.ExpectingValue = false;
- }
- this.Unindent();
- this.Put("}");
- }
- public void WriteObjectStart()
- {
- this.DoValidation(Condition.NotAProperty);
- this.PutNewline();
- this.Put("{");
- this.context = new WriterContext();
- this.context.InObject = true;
- this.ctx_stack.Push(this.context);
- this.Indent();
- }
- public void WritePropertyName(string property_name)
- {
- this.DoValidation(Condition.Property);
- this.PutNewline();
- this.PutString(property_name);
- if (this.pretty_print)
- {
- if (property_name.Length > this.context.Padding)
- {
- this.context.Padding = property_name.Length;
- }
- for (int i = this.context.Padding - property_name.Length; i >= 0; i--)
- {
- this.writer.Write(' ');
- }
- this.writer.Write(": ");
- }
- else
- {
- this.writer.Write(':');
- }
- this.context.ExpectingValue = true;
- }
- private static NumberFormatInfo number_format = NumberFormatInfo.InvariantInfo;
- private WriterContext context;
- private Stack<WriterContext> ctx_stack;
- private bool has_reached_end;
- private char[] hex_seq;
- private int indentation;
- private int indent_value;
- private StringBuilder inst_string_builder;
- private bool pretty_print;
- private bool validate;
- private TextWriter writer;
- }
- }
|