protected override bool TryInferFormatter(Type type, out ITypeFormatter formatter) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(ReadOnlyMemory <>)) { formatter = Formatter.Create( type, (obj, writer) => { var toArray = Formatter.FormatReadOnlyMemoryMethod.MakeGenericMethod (type.GetGenericArguments()); var array = toArray.Invoke(null, new[] { obj }); writer.Write(array.ToDisplayString()); }, PlainTextFormatter.MimeType); return(true); } formatter = null; return(false); }
protected override bool TryInferFormatter(Type type, out ITypeFormatter formatter) { if (typeof(JToken).IsAssignableFrom(type)) { formatter = Formatter.Create( type, (value, writer) => writer.Write(value.ToString()), JsonFormatter.MimeType); return(true); } formatter = null; return(false); }
private static ConcurrentDictionary <Type, ITypeFormatter> DefaultFormatters() => new ConcurrentDictionary <Type, ITypeFormatter> { [typeof(DateTime)] = new HtmlFormatter <DateTime>((value, writer) => writer.Write(value.ToString("u"))), [typeof(DateTimeOffset)] = new HtmlFormatter <DateTimeOffset>((value, writer) => writer.Write(value.ToString("u"))), [typeof(ExpandoObject)] = new HtmlFormatter <ExpandoObject>((obj, writer) => { var headers = new List <IHtmlContent>(); var values = new List <IHtmlContent>(); foreach (var pair in obj.OrderBy(p => p.Key)) { headers.Add(PocketViewTags.th(pair.Key)); values.Add(PocketViewTags.td(pair.Value)); } PocketView t = PocketViewTags.table( PocketViewTags.thead( PocketViewTags.tr( headers)), PocketViewTags.tbody( PocketViewTags.tr( values)) ); t.WriteTo(writer, HtmlEncoder.Default); }), [typeof(HtmlString)] = new HtmlFormatter <HtmlString>((view, writer) => view.WriteTo(writer, HtmlEncoder.Default)), [typeof(JsonString)] = new HtmlFormatter <JsonString>((view, writer) => view.WriteTo(writer, HtmlEncoder.Default)), [typeof(PocketView)] = new HtmlFormatter <PocketView>((view, writer) => view.WriteTo(writer, HtmlEncoder.Default)), [typeof(ReadOnlyMemory <char>)] = Formatter.Create <ReadOnlyMemory <char> >((memory, writer) => { PocketView view = PocketViewTags.span(memory.Span.ToString()); view.WriteTo(writer, HtmlEncoder.Default); }, HtmlFormatter.MimeType), [typeof(string)] = new HtmlFormatter <string>((s, writer) => writer.Write(s)), [typeof(Type)] = _formatterForSystemType, [typeof(Type).GetType()] = _formatterForSystemType, };
private static ConcurrentDictionary <Type, Func <Type, ITypeFormatter> > DefaultFormatterFactories() => new ConcurrentDictionary <Type, Func <Type, ITypeFormatter> > { [typeof(ReadOnlyMemory <>)] = type => { return(Formatter.Create( type, (obj, writer) => { var toArray = Formatter.FormatReadOnlyMemoryMethod.MakeGenericMethod (type.GetGenericArguments()); var array = toArray.Invoke(null, new[] { obj }); writer.Write(array.ToDisplayString(HtmlFormatter.MimeType)); }, HtmlFormatter.MimeType)); } };
private static ConcurrentDictionary <Type, ITypeFormatter> DefaultFormatters() { var singleLineFormatter = new SingleLinePlainTextFormatter(); return(new ConcurrentDictionary <Type, ITypeFormatter> { [typeof(ExpandoObject)] = new PlainTextFormatter <ExpandoObject>((expando, writer) => { singleLineFormatter.WriteStartObject(writer); var pairs = expando.ToArray(); var length = pairs.Length; for (var i = 0; i < length; i++) { var pair = pairs[i]; writer.Write(pair.Key); singleLineFormatter.WriteNameValueDelimiter(writer); pair.Value.FormatTo(writer); if (i < length - 1) { singleLineFormatter.WritePropertyDelimiter(writer); } } singleLineFormatter.WriteEndObject(writer); }), [typeof(PocketView)] = new PlainTextFormatter <PocketView>((view, writer) => view.WriteTo(writer, HtmlEncoder.Default)), [typeof(KeyValuePair <string, object>)] = new PlainTextFormatter <KeyValuePair <string, object> >((pair, writer) => { writer.Write(pair.Key); singleLineFormatter.WriteNameValueDelimiter(writer); pair.Value.FormatTo(writer); }), [typeof(ReadOnlyMemory <char>)] = Formatter.Create <ReadOnlyMemory <char> >((memory, writer) => { writer.Write(memory.Span.ToString()); }, PlainTextFormatter.MimeType), [typeof(Type)] = new PlainTextFormatter <Type>((type, writer) => { var typeName = type.FullName ?? type.Name; if (typeName.Contains("`") && !type.IsAnonymous()) { writer.Write(typeName.Remove(typeName.IndexOf('`'))); writer.Write("<"); var genericArguments = type.GetGenericArguments(); for (var i = 0; i < genericArguments.Length; i++) { Formatter <Type> .FormatTo(genericArguments[i], writer); if (i < genericArguments.Length - 1) { writer.Write(","); } } writer.Write(">"); } else { writer.Write(typeName); } }), [typeof(DateTime)] = new PlainTextFormatter <DateTime>((value, writer) => writer.Write(value.ToString("u"))), [typeof(DateTimeOffset)] = new PlainTextFormatter <DateTimeOffset>((value, writer) => writer.Write(value.ToString("u"))) }); }