blob: f7734a36b6492fdc965203407d5501a5cee68fbf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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;
var serializer = GetStreamSerializer(contentType);
serializer(response, responseStream);
}
public static Action<object, Stream> GetStreamSerializer(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 (o, s) => ServiceStackHost.Instance.SerializeToXml(o, s);
case "application/json":
case "text/json":
return (o, s) => ServiceStackHost.Instance.SerializeToJson(o, s);
}
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();
}
}
}
|