2016-11-11 20:55:12 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Text;
|
|
|
|
using MediaBrowser.Model.Services;
|
|
|
|
|
|
|
|
namespace ServiceStack.Host
|
|
|
|
{
|
|
|
|
public class ContentTypes
|
|
|
|
{
|
|
|
|
public static ContentTypes Instance = new ContentTypes();
|
|
|
|
|
|
|
|
public void SerializeToStream(IRequest req, object response, Stream responseStream)
|
|
|
|
{
|
|
|
|
var contentType = req.ResponseContentType;
|
2016-11-12 07:58:50 +01:00
|
|
|
var serializer = GetStreamSerializer(contentType);
|
2016-11-11 20:55:12 +01:00
|
|
|
|
2016-11-12 07:58:50 +01:00
|
|
|
serializer(response, responseStream);
|
2016-11-11 20:55:12 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 08:45:06 +01:00
|
|
|
public static Action<object, Stream> GetStreamSerializer(string contentType)
|
2016-11-11 20:55:12 +01:00
|
|
|
{
|
|
|
|
switch (GetRealContentType(contentType))
|
|
|
|
{
|
|
|
|
case "application/xml":
|
|
|
|
case "text/xml":
|
|
|
|
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
|
2016-11-12 07:58:50 +01:00
|
|
|
return (o, s) => ServiceStackHost.Instance.SerializeToXml(o, s);
|
2016-11-11 20:55:12 +01:00
|
|
|
|
|
|
|
case "application/json":
|
|
|
|
case "text/json":
|
2016-11-12 07:58:50 +01:00
|
|
|
return (o, s) => ServiceStackHost.Instance.SerializeToJson(o, s);
|
2016-11-11 20:55:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Func<Type, Stream, object> GetStreamDeserializer(string contentType)
|
|
|
|
{
|
|
|
|
switch (GetRealContentType(contentType))
|
|
|
|
{
|
|
|
|
case "application/xml":
|
|
|
|
case "text/xml":
|
|
|
|
case "text/xml; charset=utf-8": //"text/xml; charset=utf-8" also matches xml
|
|
|
|
return ServiceStackHost.Instance.DeserializeXml;
|
|
|
|
|
|
|
|
case "application/json":
|
|
|
|
case "text/json":
|
|
|
|
return ServiceStackHost.Instance.DeserializeJson;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string GetRealContentType(string contentType)
|
|
|
|
{
|
|
|
|
return contentType == null
|
|
|
|
? null
|
|
|
|
: contentType.Split(';')[0].ToLower().Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|