Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 7a4ba2b

Browse files
committed
clean up custom serializer fn's after test
1 parent 5f24fea commit 7a4ba2b

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

tests/ServiceStack.Text.Tests/JsonTests/CustomRawSerializerTests.cs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@ public void Can_Serialize_TypeProperties_WithCustomFunction()
2525
{
2626
var test = new RealType { Name = "Test", Data = new byte[] { 1, 2, 3, 4, 5 } };
2727

28-
// Act: now we set a custom function for byte[]
2928
JsConfig<byte[]>.RawSerializeFn = c =>
30-
{
31-
var temp = new int[c.Length];
32-
Array.Copy(c, temp, c.Length);
33-
return JsonSerializer.SerializeToString(temp);
34-
};
29+
{
30+
var temp = new int[c.Length];
31+
Array.Copy(c, temp, c.Length);
32+
return JsonSerializer.SerializeToString(temp);
33+
};
3534
var json = JsonSerializer.SerializeToString(test);
3635

37-
// Assert:
3836
Assert.That(json, Is.EquivalentTo("{\"Name\":\"Test\",\"Data\":[1,2,3,4,5]}"));
3937

38+
JsConfig<byte[]>.RawSerializeFn = null;
4039
JsConfig.Reset();
4140
}
4241

@@ -48,7 +47,7 @@ public void Can_Serialize_bytes_as_Hex()
4847
{
4948
hex = hex.Replace("-", "");
5049
return Enumerable.Range(0, hex.Length)
51-
.Where(x => x%2 == 0)
50+
.Where(x => x % 2 == 0)
5251
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
5352
.ToArray();
5453
};
@@ -66,7 +65,14 @@ public void Can_Serialize_bytes_as_Hex()
6665

6766
Assert.That(fromJson.Data, Is.EquivalentTo(dto.Data));
6867

68+
JsConfig<byte[]>.SerializeFn = null;
69+
JsConfig<byte[]>.DeSerializeFn = null;
6970
JsConfig.Reset();
71+
72+
json = dto.ToJson();
73+
json.Print();
74+
fromJson = json.FromJson<RealType>();
75+
Assert.That(fromJson.Data, Is.EquivalentTo(dto.Data));
7076
}
7177

7278
[Test]
@@ -76,34 +82,38 @@ public void Can_Serialize_AnonymousTypeProperties_WithCustomFunction()
7682

7783
// Act: now we set a custom function for byte[]
7884
JsConfig<byte[]>.RawSerializeFn = c =>
79-
{
80-
var temp = new int[c.Length];
81-
Array.Copy(c, temp, c.Length);
82-
return JsonSerializer.SerializeToString(temp);
83-
};
85+
{
86+
var temp = new int[c.Length];
87+
Array.Copy(c, temp, c.Length);
88+
return JsonSerializer.SerializeToString(temp);
89+
};
8490
var json = JsonSerializer.SerializeToString(test);
8591

8692
// Assert:
8793
Assert.That(json, Is.EquivalentTo("{\"Name\":\"Test\",\"Data\":[1,2,3,4,5]}"));
94+
95+
JsConfig<byte[]>.RawSerializeFn = null;
96+
JsConfig.Reset();
8897
}
8998

9099
[Test]
91100
public void Reset_ShouldClear_JsConfigT_CachedFunctions()
92101
{
93102
var test = new { Name = "Test", Data = new byte[] { 1, 2, 3, 4, 5 } };
94103
JsConfig<byte[]>.RawSerializeFn = c =>
95-
{
96-
var temp = new int[c.Length];
97-
Array.Copy(c, temp, c.Length);
98-
return JsonSerializer.SerializeToString(temp);
99-
};
104+
{
105+
var temp = new int[c.Length];
106+
Array.Copy(c, temp, c.Length);
107+
return JsonSerializer.SerializeToString(temp);
108+
};
100109
var json = JsonSerializer.SerializeToString(test);
101110

102111
Assert.That(json, Is.EquivalentTo("{\"Name\":\"Test\",\"Data\":[1,2,3,4,5]}"));
103-
// Act: now we set a custom function for byte[]
112+
113+
JsConfig<byte[]>.RawSerializeFn = null;
104114
JsConfig.Reset();
105115
json = JsonSerializer.SerializeToString(test);
106-
// Assert:
116+
107117
Assert.That(json, Is.EquivalentTo("{\"Name\":\"Test\",\"Data\":\"AQIDBAU=\"}"));
108118
}
109119

0 commit comments

Comments
 (0)