123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
-
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace etoy
- {
- interface IJsonWritable
- {
- /// <summary>
- /// 迭代器写入
- /// 会自动拼接 ','
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="enumerable"></param>
- /// <param name="len"></param>
- /// <param name="callback"></param>
- /// <returns></returns>
- IJsonWritable EnumeratorToJson<T>(IEnumerable<T> enumerable, int len, Action<int, T> callback);
- /// <summary>
- /// 写入key 自动拼接成 -> \"key\":
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- IJsonWritable AppendKey(string value);
- /// <summary>
- /// 写入value
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- IJsonWritable Append(int value);
- /// <summary>
- /// 写入value -> \"value\"
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- IJsonWritable Append(string value);
- /// <summary>
- /// 强制写入value 不会进行转义
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- IJsonWritable AppendForce(string value);
- /// <summary>
- /// [
- /// </summary>
- /// <returns></returns>
- IJsonWritable ArrayBracket();
- /// <summary>
- /// ]
- /// </summary>
- /// <returns></returns>
- IJsonWritable EndArrayBracket();
- /// <summary>
- /// {
- /// </summary>
- /// <returns></returns>
- IJsonWritable Bracket();
- /// <summary>
- /// }
- /// </summary>
- /// <returns></returns>
- IJsonWritable EndBracket();
- /// <summary>
- /// ,
- /// </summary>
- /// <returns></returns>
- IJsonWritable Dot();
- /// <summary>
- /// 换行
- /// </summary>
- /// <returns></returns>
- IJsonWritable NewLine();
- IJsonWritable Indent();
- IJsonWritable Dedent();
- }
- class JsonWriter : IDisposable
- , IJsonWritable
- {
- #region Constructors
- private readonly TextWriter _w;
- private MemoryStream _ms = new MemoryStream();
- private readonly bool _formatting;
- private Encoding _utf8WithoutBom = new UTF8Encoding(false);
- public JsonWriter(bool formatting = false)
- {
- _ms = new MemoryStream();
- _w = new StreamWriter(_ms, _utf8WithoutBom);
- _formatting = formatting;
- }
- /// <summary>
- /// Return the generated code as a string.
- /// </summary>
- public string Json
- {
- get
- {
- _w.Flush();
- return _utf8WithoutBom.GetString(_ms.ToArray());
- }
- }
- public virtual void Dispose()
- {
- _w.Close();
- }
- #endregion
- #region Json Write
- public IJsonWritable EnumeratorToJson<T>(IEnumerable<T> enumerable, int len, Action<int, T> callback)
- {
- var count = 0;
- var enumerator = enumerable.GetEnumerator();
- while (enumerator.MoveNext())
- {
- callback.Invoke(count, enumerator.Current);
- if (++count >= len) break;
- this.Dot();
- }
- enumerator.Dispose();
- return this;
- }
- public IJsonWritable AppendKey(string value)
- {
- _w.Write($"\"{value}\":");
- return this;
- }
- public IJsonWritable Append(int value)
- {
- _w.Write(value);
- return this;
- }
- public IJsonWritable Append(string value)
- {
- _w.Write($"\"{value}\"");
- return this;
- }
- /// <summary>
- /// 不拼接引号强制写入
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public IJsonWritable AppendForce(string value)
- {
- _w.Write($"{value}");
- return this;
- }
- public IJsonWritable ArrayBracket()
- {
- _w.Write('[');
- return this;
- }
- public IJsonWritable EndArrayBracket()
- {
- _w.Write(']');
- return this;
- }
- public IJsonWritable Bracket()
- {
- _w.Write('{');
- return this;
- }
- public IJsonWritable Dot()
- {
- _w.Write(',');
- return this;
- }
- public IJsonWritable EndBracket()
- {
- _w.Write('}');
- return this;
- }
- #endregion
- #region 格式化
- public static string IndentPrefix = " ";
- /// <summary>
- /// Level of indentation
- /// </summary>
- public int IndentLevel { get; private set; }
- /// <summary>
- /// Accumulated prefixes over indentations
- /// </summary>
- string prefix = "";
- ///<summary> 增加缩进 </summary>
- public IJsonWritable Indent()
- {
- IndentLevel += 1;
- prefix += IndentPrefix;
- return this;
- }
- ///<summary> 减少缩进 </summary>
- public IJsonWritable Dedent()
- {
- IndentLevel -= 1;
- if (IndentLevel < 0)
- throw new InvalidOperationException("Indent error");
- prefix = prefix.Substring(0, prefix.Length - IndentPrefix.Length);
- return this;
- }
- public IJsonWritable NewLine()
- {
- if (_formatting)
- {
- _w.Write("\r\n");
- _w.Write(prefix);
- }
- return this;
- }
- #endregion
- }
- }
|