Ejemplo n.º 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);
            }
        }
Ejemplo n.º 2
0
 public static ITypeFormatter GetBestFormatterFor(Type type) =>
 Formatter.GetBestFormatterFor(type, MimeType);
Ejemplo n.º 3
0
 public static ITypeFormatter GetBestFormatterFor(Type type)
 {
     return(Formatter.GetBestFormatterFor(type, MimeType));
 }