aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Serialization/ProtobufSerializer.cs
blob: 6151e83413580a88699d4687afbdaa084f4ffb5c (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
using System;
using System.IO;

namespace MediaBrowser.Common.Serialization
{
    /// <summary>
    /// Protocol buffers is google's binary serialization format. This is a .NET implementation of it.
    /// You have to tag your classes with some annoying attributes, but in return you get the fastest serialization around with the smallest possible output.
    /// </summary>
    public static class ProtobufSerializer
    {
        public static void SerializeToStream<T>(T obj, Stream stream)
        {
            ProtoBuf.Serializer.Serialize<T>(stream, obj);
        }

        public static T DeserializeFromStream<T>(Stream stream)
        {
            return ProtoBuf.Serializer.Deserialize<T>(stream);
        }

        public static object DeserializeFromStream(Stream stream, Type type)
        {
            throw new NotImplementedException();
        }
        
        public static void SerializeToFile<T>(T obj, string file)
        {
            using (Stream stream = File.Open(file, FileMode.Create))
            {
                SerializeToStream<T>(obj, stream);
            }
        }

        public static T DeserializeFromFile<T>(string file)
        {
            using (Stream stream = File.OpenRead(file))
            {
                return DeserializeFromStream<T>(stream);
            }
        }
    }
}