예제 #1
0
        private void WriteDateTime(object obj)
        {
            var d = (DateTime)obj;

            // 'Roundtrip' format is guaranteed to be ASCII-clean.
            if (JsonOptions.DateFormat == "O")
            {
                writer.Write((byte)'"');
                JsonIntWriter.WriteInt4Digits(writer, d.Year);
                writer.Write((byte)'-');
                JsonIntWriter.WriteInt2Digits(writer, d.Month);
                writer.Write((byte)'-');
                JsonIntWriter.WriteInt2Digits(writer, d.Day);
                writer.Write((byte)'T');
                JsonIntWriter.WriteInt2Digits(writer, d.Hour);
                writer.Write((byte)':');
                JsonIntWriter.WriteInt2Digits(writer, d.Minute);
                writer.Write((byte)':');
                JsonIntWriter.WriteInt2Digits(writer, d.Second);
                writer.Write((byte)'.');
                JsonIntWriter.WriteInt7Digits(writer, (int)(d.Ticks % TimeSpan.TicksPerSecond));
                switch (d.Kind)
                {
                case DateTimeKind.Local:
                    writer.Write(localTimeZone);
                    break;

                case DateTimeKind.Unspecified:
                    break;

                case DateTimeKind.Utc:
                    writer.Write((byte)'Z');
                    break;
                }
                writer.Write((byte)'"');
            }
            else
            {
                JsonStringWriter.WriteEscapedString(
                    writer, d.ToString(JsonOptions.DateFormat, CultureInfo.InvariantCulture));
            }
        }
예제 #2
0
        private void WriteTimeSpan(object obj)
        {
            var t = (TimeSpan)obj;

            // 'Constant' format is guaranteed to be ASCII-clean.
            if (JsonOptions.TimeSpanFormat == "c")
            {
                writer.Write((byte)'"');
                if (t.Ticks < 0)
                {
                    writer.Write((byte)'-');
                    t = t.Duration();
                }
                var d = t.Days;
                if (d > 0)
                {
                    JsonIntWriter.WriteInt(writer, d);
                    writer.Write((byte)'.');
                }
                JsonIntWriter.WriteInt2Digits(writer, t.Hours);
                writer.Write((byte)':');
                JsonIntWriter.WriteInt2Digits(writer, t.Minutes);
                writer.Write((byte)':');
                JsonIntWriter.WriteInt2Digits(writer, t.Seconds);
                var f = (int)(t.Ticks % TimeSpan.TicksPerSecond);
                if (f > 0)
                {
                    writer.Write((byte)'.');
                    JsonIntWriter.WriteInt7Digits(writer, f);
                }
                writer.Write((byte)'"');
            }
            else
            {
                JsonStringWriter.WriteEscapedString(
                    writer, t.ToString(JsonOptions.TimeSpanFormat, CultureInfo.InvariantCulture));
            }
        }
예제 #3
0
 private void WriteULong(object obj) => JsonIntWriter.WriteULong(writer, obj);
예제 #4
0
 private void WriteULongAsString(object obj)
 {
     writer.Write((byte)'"');
     JsonIntWriter.WriteULong(writer, obj);
     writer.Write((byte)'"');
 }
예제 #5
0
 private void WriteUInt(object obj) => JsonIntWriter.WriteUInt(writer, obj);