/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, string source) { /*要进行转义的*/ if (source == null) { if (setting.WriteNullWhenObjectIsNull) { writer.Write("null"); } else { writer.Write("\"\""); } return; } else if (source.Length == 0) { writer.Write("\"\""); return; } writer.Write("\""); for (var i = 0; i < source.Length; i++) { CharMethodProvider.Default.Write(writer, setting, source[i]); } writer.Write("\""); }
public void Write(ISerializerWriter writer, JsonSerializeSetting setting, InvestModel model, byte level) { writer.Write("{"); writer.Write("Amount:"); writer.Write(model.Amount.ToString()); writer.Write("}"); }
/// <summary> /// 写入流中 /// </summary> /// <param name="writer">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="level"></param> /// <param name="array">The array.</param> public virtual void Write(ISerializerWriter writer, JsonSerializeSetting setting, byte level, IEnumerable array) { if (array == null) { if (setting.WriteNullWhenObjectIsNull) { writer.Write("null"); } else { writer.Write("[]"); } return; } writer.Write("["); var enumerator = array.GetEnumerator(); bool moveNext = enumerator.MoveNext(); while (moveNext) { var item = enumerator.Current; SerialierBuilderHelper.QueryBuilderInvoker(item.GetType())(writer, setting, item, level++); if (moveNext = enumerator.MoveNext()) { writer.Write(","); } } writer.Write("]"); return; }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, Nullable <T> source) { if (source.HasValue) { if (setting.WriteNumberOnEnumType) { if (setting.WriteQuoteWhenObjectIsNumber) { writer.Write("\""); MethodProviders.EnumMethodProvider <T> .Default.Write(writer, setting, source.Value); writer.Write("\""); } else { MethodProviders.EnumMethodProvider <T> .Default.Write(writer, setting, source.Value); } } else { writer.Write("\""); MethodProviders.EnumMethodProvider <T> .Default.Write(writer, setting, source.Value); writer.Write("\""); } return; } writer.Write("null"); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, bool?source) { if (source.HasValue) { if (setting.WriteNumberOnBoolenType) { if (setting.WriteQuoteWhenObjectIsNumber) { writer.Write("\""); MethodProviders.BooleanMethodProvider.Default.Write(writer, setting, source.Value); writer.Write("\""); } else { MethodProviders.BooleanMethodProvider.Default.Write(writer, setting, source.Value); } } else { writer.Write("\""); MethodProviders.BooleanMethodProvider.Default.Write(writer, setting, source.Value); writer.Write("\""); } return; } writer.Write("null"); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, Type source) { if (setting.WriteVersionOnTypeInfo) { writer.Write(source.AssemblyQualifiedName); } else { writer.Write(Regex.Replace(source.AssemblyQualifiedName, "\\s+version=(.*?),", "", RegexOptions.IgnoreCase)); } }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, long source) { if (source == long.MinValue) { writer.Write("-9223372036854775808"); return; } if (source == long.MaxValue) { writer.Write("9223372036854775807"); return; } var target = source; if (source < 0) { writer.Write('-'); target = -target; } char[] buffer = null; /*long的最大长度为19,加一个负数符号*/ var maxlength = "9223372036854775807".Length; buffer = new char[maxlength + 1]; var lessen = 0L; KeyValuePair <char, char> item; do { lessen = target % 100; item = ZeroToHundred[lessen]; buffer[maxlength] = item.Value; buffer[maxlength - 1] = item.Key; maxlength -= 2; target = (target / 100); } while (target != 0); if (item.Key == '0') { writer.Write(buffer, maxlength + 2); return; } writer.Write(buffer, maxlength + 1); return; //var digitlength = GetDigitLength(source); //buffer = new char[digitlength]; //WriteIntoChar(buffer, source, digitlength, 0); //writer.Write(buffer); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, DateTime?source) { if (source.HasValue) { writer.Write("\""); MethodProviders.DateTimes.ChineseStyleDateMethodProvider.Default.Write(writer, setting, source.Value); writer.Write("\""); return; } writer.Write("null"); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, bool source) { if (setting.WriteNumberOnBoolenType) { writer.Write(source ? "1" : "0"); } else { writer.Write(source ? "true" : "false"); } return; }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, TimeSpan?source) { if (source.HasValue) { writer.Write("\""); MethodProviders.TimeSpanMethodProvider.Default.Write(writer, setting, source.Value); writer.Write("\""); return; } writer.Write("null"); }
/// <summary> /// 写入流中 /// </summary> /// <param name="writer">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="level"></param> /// <param name="array">The array.</param> public virtual void WriteObjectValue(ISerializerWriter writer, JsonSerializeSetting setting, byte level, IDictionary <Key, object> array) { if (array == null) { if (setting.WriteNullWhenObjectIsNull) { writer.Write("null"); } else { writer.Write("{}"); } return; } if (array.Count == 0) { writer.Write("{}"); return; } writer.Write("{"); var i = -1; foreach (var a in array) { i++; ZzzZzSerialierBuilder <Key> .Register(setting).Invoke(writer, setting, a.Key, level++); writer.Write(":"); if (a.Value == null) { if (setting.WriteNullWhenObjectIsNull) { writer.Write("null"); } else { writer.Write("{}"); } } else { SerialierBuilderHelper.QueryBuilderInvoker(a.Value.GetType()).Invoke(writer, setting, a.Value, level++); } if (i < array.Count - 1) { writer.Write(","); } } writer.Write("}"); return; }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, byte source) { char[] buffer = new char[3]; var it = ZeroToHundred[source % 100]; buffer[2] = it.Value; buffer[1] = it.Key; if (source < 10) { writer.Write(buffer, 2); return; } if (source < 100) { writer.Write(buffer, 1); return; } buffer[0] = ZeroToHundred[source / 100].Value; writer.Write(buffer); return; //char[] buffer = null; /*ushort的最大长度为5*/ //var maxlength = "255".Length; //buffer = new char[maxlength + 1]; //byte lessen = 0; //Tuple<char, char> item; //do //{ // lessen = (byte)(source % 100); // item = ZeroToHundred[lessen]; // buffer[maxlength] = item.Item2; // buffer[maxlength - 1] = item.Item1; // maxlength -= 2; // source = (byte)(source / 100); //} while (source != 0); //if (item.Item1 == '0') //{ // writer.Write(buffer, maxlength + 2); // return; //} //writer.Write(buffer, maxlength + 1); //return; //var digitlength = GetDigitLength(source); //buffer = new char[digitlength]; //WriteIntoChar(buffer, source, digitlength, 0); //writer.Write(buffer); }
/// <summary> /// 执行builder,反射用到的 /// </summary> /// <param name="writer">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="level"></param> /// <param name="source">The source.</param> internal static void CallNullablePrimitiveBuilderInvoke <T>(ISerializerWriter writer, JsonSerializeSetting setting, Nullable <T> source, byte level) where T : struct { if (!source.HasValue) { writer.Write("null"); return; } ZzzZzSerialierBuilder <T> .Register(setting).Invoke(writer, setting, source.Value, level); }
/// <summary> /// 执行builder,反射用到的 /// </summary> /// <param name="writer">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="level"></param> /// <param name="source">The source.</param> internal static void CallObjectBuilderInvoke(ISerializerWriter writer, JsonSerializeSetting setting, object source, byte level) { if (source == null) { writer.Write("null"); return; } QueryBuilderInvoker(source.GetType()).Invoke(writer, setting, source, level++); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source"></param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, DateTime source) { char[] buffer = null; buffer = new char[24]; var item = ZeroToHundred[source.Year % 100]; buffer[2] = item.Key; buffer[3] = item.Value; item = ZeroToHundred[source.Year / 100]; buffer[0] = item.Key; buffer[1] = item.Value; buffer[4] = '-'; item = ZeroToHundred[source.Month]; buffer[5] = item.Key; buffer[6] = item.Value; buffer[7] = '-'; item = ZeroToHundred[source.Day]; buffer[8] = item.Key; buffer[9] = item.Value; buffer[10] = 'T'; item = ZeroToHundred[source.Hour]; buffer[11] = item.Key; buffer[12] = item.Value; buffer[13] = ':'; item = ZeroToHundred[source.Minute]; buffer[14] = item.Key; buffer[15] = item.Value; buffer[16] = ':'; item = ZeroToHundred[source.Second]; buffer[17] = item.Key; buffer[18] = item.Value; buffer[19] = '.'; item = ZeroToHundred[source.Millisecond % 100]; buffer[20] = item.Key; buffer[21] = item.Value; item = ZeroToHundred[source.Millisecond / 100]; buffer[22] = item.Key; buffer[23] = 'Z'; writer.Write(buffer); return; }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, ushort?source) { if (source.HasValue) { if (setting.WriteQuoteWhenObjectIsNumber) { writer.Write("\""); MethodProviders.UInt16MethodProvider.Default.Write(writer, setting, source.Value); writer.Write("\""); } else { MethodProviders.UInt16MethodProvider.Default.Write(writer, setting, source.Value); } return; } writer.Write("null"); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, ulong source) { if (source == ulong.MinValue) { writer.Write("0"); return; } if (source == ulong.MaxValue) { writer.Write("18446744073709551615"); return; } char[] buffer = null; /*ushort的最大长度为10*/ var maxlength = "18446744073709551615".Length; buffer = new char[maxlength + 1]; ulong lessen = 0; KeyValuePair <char, char> item; do { lessen = source % 100; item = ZeroToHundred[lessen]; buffer[maxlength] = item.Value; buffer[maxlength - 1] = item.Key; maxlength -= 2; source = source / 100; } while (source != 0); if (item.Key == '0') { writer.Write(buffer, maxlength + 2); return; } writer.Write(buffer, maxlength + 1); return; }
/// <summary> /// 写入流中 /// </summary> /// <param name="writer">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="array">The array.</param> public virtual void WriteString(ISerializerWriter writer, JsonSerializeSetting setting, IEnumerable <T> array) { if (array == null) { if (setting.WriteNullWhenObjectIsNull) { writer.Write("null"); } else { writer.Write("[]"); } return; } var provider = this.MethodProvider; var list = array as IList <T>; if (list != null) { writer.Write("["); for (var i = 0; i < list.Count; i++) { provider.Write(writer, setting, list[i]); if (i < list.Count - 1) { writer.Write(","); } } writer.Write("]"); return; } writer.Write("["); var enumerator = array.GetEnumerator(); if (enumerator.MoveNext()) { provider.Write(writer, setting, enumerator.Current); } while (enumerator.MoveNext()) { writer.Write(","); provider.Write(writer, setting, enumerator.Current); } writer.Write("]"); return; }
/// <summary> /// 序列化对象 /// </summary> /// <param name="object">源对象</param> /// <param name="setting">配置</param> /// <param name="writer">写入流</param> /// <returns></returns> private static void Serialize(object @object, JsonSerializeSetting setting, ISerializerWriter writer) { if (@object == null) { writer.Write("null"); return; } var @delegate = SerialierBuilderHelper.QueryBuilderInvoker(@object.GetType()); @delegate(writer, setting, @object, 0); }
/// <summary> /// 写入流中 /// </summary> /// <param name="writer">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="level"></param> /// <param name="array">The array.</param> public virtual void Write(ISerializerWriter writer, JsonSerializeSetting setting, byte level, IEnumerable <T> array) { if (array == null) { if (setting.WriteNullWhenObjectIsNull) { writer.Write("null"); } else { writer.Write("[]"); } return; } var list = array as IList <T>; if (list != null) { writer.Write("["); for (var i = 0; i < list.Count; i++) { ZzzZzSerialierBuilder <T> .Register(setting).Invoke(writer, setting, list[i], level++); if (i < list.Count - 1) { writer.Write(","); } } writer.Write("]"); return; } writer.Write("["); var enumerator = array.GetEnumerator(); if (enumerator.MoveNext()) { ZzzZzSerialierBuilder <T> .Register(setting).Invoke(writer, setting, enumerator.Current, level++); } while (enumerator.MoveNext()) { writer.Write(","); ZzzZzSerialierBuilder <T> .Register(setting).Invoke(writer, setting, enumerator.Current, level++); } writer.Write("]"); return; }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, Guid source) { var guid = source.ToString(); if (setting.WriteHorizontalLineOnGuidType) { writer.Write(guid); return; } /*9c43f081-a003-4b6f-b1fd-a5e8017c6b2b*/ var buffer = new char[32]; for (var i = 0; i < 8; i++) { buffer[i] = guid[i]; } for (var i = 9; i < 13; i++) { buffer[i - 1] = guid[i]; } for (var i = 14; i < 18; i++) { buffer[i - 2] = guid[i]; } for (var i = 19; i < 23; i++) { buffer[i - 3] = guid[i]; } for (var i = 24; i < 36; i++) { buffer[i - 4] = guid[i]; } writer.Write(buffer); }
public void Write(object @object, Type objectType) { if (@object == null) { WriteNull(); return; } if (objectType == typeof(string)) { _writer.Write($"\"{@object.ToString()}\""); } else if (objectType.IsIntegerType()) { _writer.Write($"{@object.ToString()}"); } else if (objectType.IsFloatType()) { throw new NotSupportedException(); } else if (!objectType.IsClass) { throw new NotSupportedException(); } else { if (_serializedObjectReferences.Contains(@object)) { WriteReference(@object); } else { _serializedObjectReferences.Add(@object); WriteStart(); WriteReferenceIdField(@object); _serializer.Serialize(@object, objectType); WriteEnd(); } } }
/// <summary> /// 写入流中 /// </summary> /// <param name="writer">The writer.</param> /// <param name="setting">The setting.</param> /// <param name="level"></param> /// <param name="array">The array.</param> public virtual void Write(ISerializerWriter writer, JsonSerializeSetting setting, byte level, IDictionary array) { if (array == null) { if (setting.WriteNullWhenObjectIsNull) { writer.Write("null"); } else { writer.Write("{}"); } return; } if (array.Count == 0) { writer.Write("{}"); return; } writer.Write("{"); var enumerator = array.Keys.GetEnumerator(); bool moveNext = enumerator.MoveNext(); while (moveNext) { SerialierBuilderHelper.QueryBuilderInvoker(enumerator.Current.GetType())(writer, setting, enumerator.Current, level++); writer.Write(":"); var item = array[enumerator.Current]; SerialierBuilderHelper.QueryBuilderInvoker(item.GetType())(writer, setting, item, level++); if (moveNext = enumerator.MoveNext()) { writer.Write(","); } } writer.Write("}"); return; }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, TimeSpan source) { char[] buffer = null; var item = new KeyValuePair <char, char>(); if (source.Days == 0) { buffer = new char[8]; item = ZeroToHundred[source.Hours]; buffer[0] = item.Key; buffer[1] = item.Value; buffer[2] = ':'; item = ZeroToHundred[source.Minutes]; buffer[3] = item.Key; buffer[4] = item.Value; buffer[5] = ':'; item = ZeroToHundred[source.Seconds]; buffer[6] = item.Key; buffer[7] = item.Value; writer.Write(buffer); return; } var diglength = 1; if (source.Days < 10) { buffer = new char[10]; buffer[0] = ZeroToHundred[source.Days].Value; } else { diglength = GetDigitLength(source.Days); var max = source.Days; var less = source.Days % 100; buffer = new char[diglength + 9]; for (var i = diglength; i > 0; i = i - 2) { item = ZeroToHundred[less]; if (less > 9) { buffer[i - 1] = item.Value; buffer[i - 2] = item.Key; } else { buffer[i - 1] = item.Value; } max = max / 100; less = max % 100; } } buffer[diglength] = '.'; item = ZeroToHundred[source.Hours]; buffer[diglength + 1] = item.Key; buffer[diglength + 2] = item.Value; buffer[diglength + 3] = ':'; item = ZeroToHundred[source.Minutes]; buffer[diglength + 4] = item.Key; buffer[diglength + 5] = item.Value; buffer[diglength + 6] = ':'; item = ZeroToHundred[source.Seconds]; buffer[diglength + 7] = item.Key; buffer[diglength + 8] = item.Value; writer.Write(buffer); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, decimal source) { writer.Write(source.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, float source) { writer.Write(source.ToString(System.Globalization.CultureInfo.InvariantCulture)); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source">源数据</param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, char source) { //var str = new string(new[] { source }); //characterEscapes.TryGetValue(source, out str); //writer.Write(str); //return; if (setting.WriteWithUnicodeOnStringType) { writer.Write("\\u"); writer.Write(hexChars[(source >> 12) & 15]); writer.Write(hexChars[(source >> 8) & 15]); writer.Write(hexChars[(source >> 4) & 15]); writer.Write(hexChars[(source) & 15]); return; } switch (source) { case '\u0000': { writer.Write(@"\u0000"); return; } case '\u0001': { writer.Write(@"\u0001"); return; } case '\u0002': { writer.Write(@"\u0002"); return; } case '\u0003': { writer.Write(@"\u0003"); return; } case '\u0004': { writer.Write(@"\u0004"); return; } case '\u0005': { writer.Write(@"\u0005"); return; } case '\u0006': { writer.Write(@"\u0006"); return; } case '\u0007': { writer.Write(@"\u0007"); return; } case '\u0008': { writer.Write(@"\b"); return; } case '\u0009': { writer.Write(@"\t"); return; } case '\u000A': { writer.Write(@"\n"); return; } case '\u000B': { writer.Write(@"\u000b"); return; } case '\u000C': { writer.Write(@"\f"); return; } case '\u000D': { writer.Write(@"\r"); return; } case '\u000E': { writer.Write(@"\u000e"); return; } case '\u000F': { writer.Write(@"\u000f"); return; } case '\u0010': { writer.Write(@"\u0010"); return; } case '\u0011': { writer.Write(@"\u0011"); return; } case '\u0012': { writer.Write(@"\u0012"); return; } case '\u0013': { writer.Write(@"\u0013"); return; } case '\u0014': { writer.Write(@"\u0014"); return; } case '\u0015': { writer.Write(@"\u0015"); return; } case '\u0016': { writer.Write(@"\u0016"); return; } case '\u0017': { writer.Write(@"\u0017"); return; } case '\u0018': { writer.Write(@"\u0018"); return; } case '\u0019': { writer.Write(@"\u0019"); return; } case '\u001A': { writer.Write(@"\u001a"); return; } case '\u001B': { writer.Write(@"\u001b"); return; } case '\u001C': { writer.Write(@"\u001c"); return; } case '\u001D': { writer.Write(@"\u001d"); return; } case '\u001E': { writer.Write(@"\u001e"); return; } case '\u001F': { writer.Write(@"\u001f"); return; } case '\u0022': { writer.Write("\\\""); return; } case '\u0027': { writer.Write(@"'"); return; } case '\u005C': { writer.Write(@"\\"); return; } } writer.Write(source); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source"></param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, DateTime source) { writer.Write(source.ToString()); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source"></param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, DateTime source) { char[] buffer = null; var timespan = source.ToUniversalTime() - _1970; KeyValuePair <char, char> item; /*long的最大长度为20,加预留字段10个*/ var maxlength = 26; buffer = new char[30]; buffer[27] = ')'; buffer[28] = '\\'; buffer[29] = '/'; var target = (long)timespan.TotalMilliseconds; var lessen = 0L; do { lessen = target % 100; item = ZeroToHundred[lessen]; buffer[maxlength] = item.Value; buffer[maxlength - 1] = item.Key; maxlength -= 2; target = target / 100; } while (target > 10); if (target == 0) { buffer[maxlength - 6] = '\\'; buffer[maxlength - 5] = '/'; buffer[maxlength - 4] = 'D'; buffer[maxlength - 3] = 'a'; buffer[maxlength - 2] = 't'; buffer[maxlength - 1] = 'e'; buffer[maxlength] = '('; writer.Write(buffer, maxlength - 6); return; } buffer[maxlength - 7] = '\\'; buffer[maxlength - 6] = '/'; buffer[maxlength - 5] = 'D'; buffer[maxlength - 4] = 'a'; buffer[maxlength - 3] = 't'; buffer[maxlength - 2] = 'e'; buffer[maxlength - 1] = '('; buffer[maxlength] = ZeroToHundred[target].Value; writer.Write(buffer, maxlength - 7); return; /*old算法*/ //var max = (long)timespan.TotalMilliseconds; //var diglength = GetDigitLength(max); //var less = max % 100; //* 7 + length + 3*/ //buffer = new char[diglength + 10]; //buffer[0] = '\\'; //buffer[1] = '/'; //buffer[2] = 'D'; //buffer[3] = 'a'; //buffer[4] = 't'; //buffer[5] = 'e'; //buffer[6] = '('; /*long max length 20*/ //for (var i = diglength + 6; i > 6; i = i - 2) //{ // item = ZeroToHundred[less]; // buffer[i] = item.Item2; // buffer[i - 1] = item.Item1; // max = max / 100; // less = max % 100; //} /*因为上面循环条件中i>6,表示i的最小值为7,有可能会写了6的值,但能写6的值通常是是\0这个值 * // 为什么是\0这个值,由于max的长度已经计算出来,所以在分配的时候长度固定,max在buffer中开始的索引是从7开始的,一直到长度 + 6,%100会表示每次取2位值,到了less为1-9的数,在ZeroToHundred中1-9是\0\n开头的 * // */ //buffer[6] = '('; //buffer[diglength + 7] = ')'; //buffer[diglength + 8] = '\\'; //buffer[diglength + 9] = '/'; //writer.Write(buffer); }
/// <summary> /// 从对象中写入流中 /// </summary> /// <param name="writer">写入流</param> /// <param name="setting">配置项</param> /// <param name="source"></param> public override void Write(ISerializerWriter writer, JsonSerializeSetting setting, DateTime source) { //Thu, 10 Apr 2008 13:30:00 GMT char[] buffer = new char[29]; switch (source.DayOfWeek) { case DayOfWeek.Friday: { buffer[0] = 'F'; buffer[1] = 'r'; buffer[2] = 'i'; } break; case DayOfWeek.Monday: { buffer[0] = 'M'; buffer[1] = 'o'; buffer[2] = 'n'; } break; case DayOfWeek.Saturday: { buffer[0] = 'S'; buffer[1] = 'a'; buffer[2] = 't'; } break; case DayOfWeek.Sunday: { buffer[0] = 'S'; buffer[1] = 'u'; buffer[2] = 'n'; } break; case DayOfWeek.Thursday: { buffer[0] = 'T'; buffer[1] = 'h'; buffer[2] = 'u'; } break; case DayOfWeek.Tuesday: { buffer[0] = 'T'; buffer[1] = 'u'; buffer[2] = 's'; } break; case DayOfWeek.Wednesday: { buffer[0] = 'W'; buffer[1] = 'e'; buffer[2] = 'd'; } break; } buffer[3] = ','; buffer[4] = ' '; var item = ZeroToHundred[source.Month]; buffer[5] = item.Key; buffer[6] = item.Value; buffer[7] = ' '; switch (source.Month) { case 1: { buffer[8] = 'J'; buffer[9] = 'a'; buffer[10] = 'n'; } break; case 2: { buffer[8] = 'F'; buffer[9] = 'e'; buffer[10] = 'b'; } break; case 3: { buffer[8] = 'M'; buffer[9] = 'a'; buffer[10] = 'r'; } break; case 4: { buffer[8] = 'A'; buffer[9] = 'p'; buffer[10] = 'r'; } break; case 5: { buffer[8] = 'M'; buffer[9] = 'a'; buffer[10] = 'y'; } break; case 6: { buffer[8] = 'J'; buffer[9] = 'u'; buffer[10] = 'n'; } break; case 7: { buffer[8] = 'J'; buffer[9] = 'u'; buffer[10] = 'l'; } break; case 8: { buffer[8] = 'A'; buffer[9] = 'u'; buffer[10] = 'g'; } break; case 9: { buffer[8] = 'S'; buffer[9] = 'e'; buffer[10] = 'p'; } break; case 10: { buffer[8] = 'O'; buffer[9] = 'c'; buffer[10] = 't'; } break; case 11: { buffer[8] = 'N'; buffer[9] = 'o'; buffer[10] = 'v'; } break; case 12: { buffer[8] = 'D'; buffer[9] = 'e'; buffer[10] = 'c'; } break; } buffer[11] = ' '; item = ZeroToHundred[source.Year % 100]; buffer[14] = item.Key; buffer[15] = item.Value; item = ZeroToHundred[source.Year / 100]; buffer[12] = item.Key; buffer[13] = item.Value; buffer[16] = ' '; item = ZeroToHundred[source.Hour]; buffer[17] = item.Key; buffer[18] = item.Value; buffer[19] = ':'; item = ZeroToHundred[source.Minute]; buffer[20] = item.Key; buffer[21] = item.Value; buffer[22] = ':'; item = ZeroToHundred[source.Second]; buffer[23] = item.Key; buffer[24] = item.Value; buffer[25] = ' '; buffer[26] = 'G'; buffer[27] = 'M'; buffer[28] = 'T'; writer.Write(buffer); }