forked from angularsen/UnitsNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataContractJsonSerializerTestsBase.cs
More file actions
43 lines (37 loc) · 1.55 KB
/
DataContractJsonSerializerTestsBase.cs
File metadata and controls
43 lines (37 loc) · 1.55 KB
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
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
using System.IO;
using System.Runtime.Serialization.Json;
namespace UnitsNet.Serialization.DataContract.Tests.DataContractJsonSerializerTests
{
public abstract class DataContractJsonSerializerTestsBase : SerializationTestsBase<string>
{
private readonly DataContractJsonSerializerSettings _settings;
protected DataContractJsonSerializerTestsBase()
{
}
protected DataContractJsonSerializerTestsBase(DataContractJsonSerializerSettings settings)
{
_settings = settings;
}
protected override string SerializeObject(object obj)
{
var serializer = new DataContractJsonSerializer(obj.GetType(), _settings);
using var stream = new MemoryStream();
serializer.WriteObject(stream, obj);
stream.Position = 0;
using var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
protected override T DeserializeObject<T>(string xml)
{
var serializer = new DataContractJsonSerializer(typeof(T), _settings);
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
return (T) serializer.ReadObject(stream);
}
}
}