private void WriteIDictionary <K, V>(object obj) { if (obj == null) { writer.Write(nullBytes); return; } var dict = (IDictionary <K, V>)obj; var wf = GetWriteFunc(typeof(V)); writer.Write((byte)'{'); if (dict.Count > 0) { try { depth += 1; WriteFieldSeparator(); var isFirst = true; foreach (var elem in dict) { WriteSep(ref isFirst); WriteIndent(); // TODO: Option to not escape dictionary keys. JsonStringWriter.WriteEscapedString(writer, elem.Key.ToString()); writer.Write((byte)':'); wf(elem.Value); } WriteFieldSeparator(); } finally { depth -= 1; } WriteIndent(); } writer.Write((byte)'}'); }
private void WriteDateTimeOffset(object obj) { var d = (DateTimeOffset)obj; JsonStringWriter.WriteEscapedString( writer, d.ToString(JsonOptions.DateTimeOffsetFormat, CultureInfo.InvariantCulture)); }
private void WriteNullableEscapedString(object obj) { if (obj == null) { writer.Write(nullBytes); return; } JsonStringWriter.WriteEscapedString(writer, obj.ToString()); }
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)); } }
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)); } }
private void WriteChar(object obj) => JsonStringWriter.WriteEscapedString(writer, obj.ToString());