Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BsonNetSerializer.cs « Serializers « SerializerBenchmark « benchmark - github.com/aspnet/MessagePack-CSharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 68616af447dd4463ba1617bc7f2fc3b512d629ae (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
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;

namespace Benchmark.Serializers
{
    public class BsonNetSerializer : SerializerBase
    {
        private static readonly JsonSerializer Serializer = new JsonSerializer();

        public override T Deserialize<T>(object input)
        {
            using (var ms = new MemoryStream((byte[])input))
            using (var jr = new BsonDataReader(ms))
            {
                return Serializer.Deserialize<T>(jr);
            }
        }

        public override object Serialize<T>(T input)
        {
            object value = input;
            if (typeof(T).IsValueType)
            {
                value = new[] { input };
            }

            using (var ms = new MemoryStream())
            {
                using (var jw = new BsonDataWriter(ms))
                {
                    Serializer.Serialize(jw, value);
                }

                ms.Flush();
                return ms.ToArray();
            }
        }

        public override string ToString()
        {
            return "BsonNet";
        }
    }
}