Пример #1
0
        /// <summary>
        /// Formats an object and writes it to the specified writer.
        /// </summary>
        /// <param name="obj">The object to be formatted.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="mimeType">The mime type to format to.</param>
        public static void FormatTo(
            T obj,
            TextWriter writer,
            string mimeType = PlainTextFormatter.MimeType)
        {
            if (obj == null)
            {
                var formatter = Formatter.GetBestFormatterFor(typeof(T), mimeType);
                formatter.Format(null, writer);
                return;
            }

            using var _ = Formatter.RecursionCounter.Enter();

            // find a formatter for the object type, and possibly register one on the fly
            if (Formatter.RecursionCounter.Depth <= Formatter.RecursionLimit)
            {
                var formatter = Formatter.GetBestFormatterFor(typeof(T), mimeType);
                formatter.Format(obj, writer);
            }
            else
            {
                PlainTextFormatter <T> .Default.Format(obj, writer);
            }
        }
Пример #2
0
 public static ITypeFormatter GetBestFormatterFor(Type type) =>
 Formatter.GetBestFormatterFor(type, MimeType);
Пример #3
0
 public static ITypeFormatter GetBestFormatterFor(Type type)
 {
     return(Formatter.GetBestFormatterFor(type, MimeType));
 }