aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.ApiInteraction/DataSerializer.cs
blob: ae3a72e622cfeb4f2654f9d1547aa7da3dd25147 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using ServiceStack.Text;
using System;
using System.IO;

namespace MediaBrowser.ApiInteraction
{
    public static class DataSerializer
    {
        /// <summary>
        /// This is an auto-generated Protobuf Serialization assembly for best performance.
        /// It is created during the Model project's post-build event.
        /// This means that this class can currently only handle types within the Model project.
        /// If we need to, we can always add a param indicating whether or not the model serializer should be used.
        /// </summary>
        private static ProtobufModelSerializer ProtobufModelSerializer = new ProtobufModelSerializer();
        
        /// <summary>
        /// Deserializes an object using generics
        /// </summary>
        public static T DeserializeFromStream<T>(Stream stream, SerializationFormats format)
            where T : class
        {
            if (format == ApiInteraction.SerializationFormats.Protobuf)
            {
                //return Serializer.Deserialize<T>(stream);
                return ProtobufModelSerializer.Deserialize(stream, null, typeof(T)) as T;
            }
            else if (format == ApiInteraction.SerializationFormats.Jsv)
            {
                return TypeSerializer.DeserializeFromStream<T>(stream);
            }
            else if (format == ApiInteraction.SerializationFormats.Json)
            {
                return JsonSerializer.DeserializeFromStream<T>(stream);
            }

            throw new NotImplementedException();
        }

        /// <summary>
        /// Deserializes an object
        /// </summary>
        public static object DeserializeFromStream(Stream stream, SerializationFormats format, Type type)
        {
            if (format == ApiInteraction.SerializationFormats.Protobuf)
            {
                //throw new NotImplementedException();
                return ProtobufModelSerializer.Deserialize(stream, null, type);
            }
            else if (format == ApiInteraction.SerializationFormats.Jsv)
            {
                return TypeSerializer.DeserializeFromStream(type, stream);
            }
            else if (format == ApiInteraction.SerializationFormats.Json)
            {
                return JsonSerializer.DeserializeFromStream(type, stream);
            }

            throw new NotImplementedException();
        }

        public static void Configure()
        {
            JsConfig.DateHandler = ServiceStack.Text.JsonDateHandler.ISO8601;
            JsConfig.ExcludeTypeInfo = true;
            JsConfig.IncludeNullValues = false;
        }

        public static bool CanDeSerializeJsv
        {
            get
            {
                return true;
            }
        }
    }
}