Aufgrund einer Wartung wird GitLab am 26.10. zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 26.10. between 8:00 and 9:00 am.

ObjectFactory.cs 730 Bytes
Newer Older
1
using System.IO;
2
using Newtonsoft.Json;
3
4
5
6
7

namespace Coscine.Api.Project.Factories
{
    public class ObjectFactory<T>
    {
8
9
        public static Stream SerializeToStream(T o)
        {            
10
            MemoryStream stream = new MemoryStream();
11
12
13
14
15
            StreamWriter writer = new StreamWriter(stream);
            var json = JsonConvert.SerializeObject(o);
            writer.Write(json);
            writer.Flush();
            stream.Position = 0;
16
17
18
19
20
            return stream;
        }

        public static T DeserializeFromStream(Stream body)
        {
21
22
23
            var sr = new StreamReader(body);
            var json = sr.ReadToEnd();
            return JsonConvert.DeserializeObject<T>(json);
24
25
26
        }
    }
}