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

JsonNetSerializer.cs « Serializers « SerializerBenchmark « benchmark - github.com/aspnet/MessagePack-CSharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b85574ddcd5140c7506edd4472baef6d549c2ce2 (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
// 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 System.Text;
using Newtonsoft.Json;

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

        public override T Deserialize<T>(object input)
        {
            using (var ms = new MemoryStream((byte[])input))
            using (var sr = new StreamReader(ms, Encoding.UTF8))
            using (var jr = new JsonTextReader(sr))
            {
                return Serializer.Deserialize<T>(jr);
            }
        }

        public override object Serialize<T>(T input)
        {
            using (var ms = new MemoryStream())
            {
                using (var sw = new StreamWriter(ms, Encoding.UTF8))
                using (var jw = new JsonTextWriter(sw))
                {
                    Serializer.Serialize(jw, input);
                }

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

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