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

Commit 99358b2

Browse files
committed
Separate Refresh reads from writes
1 parent e388077 commit 99358b2

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ public static Func<T, string> RawSerializeFn
10931093
public static Func<T, T> OnSerializingFn
10941094
{
10951095
get { return onSerializingFn; }
1096-
set { onSerializingFn = value; Refresh(); }
1096+
set { onSerializingFn = value; RefreshWrite(); }
10971097
}
10981098

10991099
/// <summary>
@@ -1103,7 +1103,7 @@ public static Func<T, T> OnSerializingFn
11031103
public static Action<T> OnSerializedFn
11041104
{
11051105
get { return onSerializedFn; }
1106-
set { onSerializedFn = value; Refresh(); }
1106+
set { onSerializedFn = value; RefreshWrite(); }
11071107
}
11081108

11091109
/// <summary>
@@ -1113,7 +1113,7 @@ public static Action<T> OnSerializedFn
11131113
public static Func<string, T> DeSerializeFn
11141114
{
11151115
get { return deSerializeFn; }
1116-
set { deSerializeFn = value; Refresh(); }
1116+
set { deSerializeFn = value; RefreshRead(); }
11171117
}
11181118

11191119
/// <summary>
@@ -1123,7 +1123,7 @@ public static Func<string, T> DeSerializeFn
11231123
public static Func<string, T> RawDeserializeFn
11241124
{
11251125
get { return rawDeserializeFn; }
1126-
set { rawDeserializeFn = value; Refresh(); }
1126+
set { rawDeserializeFn = value; RefreshRead(); }
11271127
}
11281128

11291129
public static bool HasDeserializeFn
@@ -1135,7 +1135,7 @@ public static bool HasDeserializeFn
11351135
public static Func<T, T> OnDeserializedFn
11361136
{
11371137
get { return onDeserializedFn; }
1138-
set { onDeserializedFn = value; Refresh(); }
1138+
set { onDeserializedFn = value; RefreshRead(); }
11391139
}
11401140

11411141
public static bool HasDeserialingFn
@@ -1147,7 +1147,7 @@ public static bool HasDeserialingFn
11471147
public static Func<T, string, object, object> OnDeserializingFn
11481148
{
11491149
get { return onDeserializingFn; }
1150-
set { onDeserializingFn = value; Refresh(); }
1150+
set { onDeserializingFn = value; RefreshRead(); }
11511151
}
11521152

11531153
/// <summary>
@@ -1241,11 +1241,15 @@ public static void Reset()
12411241
EmitCamelCaseNames = EmitLowercaseUnderscoreNames = IncludeTypeInfo = ExcludeTypeInfo = null;
12421242
}
12431243

1244-
public static void Refresh()
1244+
public static void RefreshRead()
12451245
{
12461246
JsonReader<T>.Refresh();
1247-
JsonWriter<T>.Refresh();
12481247
JsvReader<T>.Refresh();
1248+
}
1249+
1250+
public static void RefreshWrite()
1251+
{
1252+
JsonWriter<T>.Refresh();
12491253
JsvWriter<T>.Refresh();
12501254
}
12511255
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Globalization;
3+
using NUnit.Framework;
4+
using ServiceStack;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.Text.Tests.Issues
8+
{
9+
[TestFixture]
10+
public class JsConfigIssues
11+
{
12+
struct CustomFormatType
13+
{
14+
private int _value;
15+
16+
public int Value
17+
{
18+
get { return _value; }
19+
}
20+
21+
public CustomFormatType(int value)
22+
{
23+
_value = value;
24+
}
25+
}
26+
27+
class Dto
28+
{
29+
public CustomFormatType CustomFormatTypeProperty { get; set; }
30+
}
31+
32+
[Test]
33+
public void CallReset_AfterSerializingOnce_WithCustomSerializationForProperty_DoesNotPickUpFurtherConfigChangesForPropertyType()
34+
{
35+
var dto = new Dto { CustomFormatTypeProperty = new CustomFormatType(12345) };
36+
37+
ConfigureCustomFormatType();
38+
TestRoundTripValue(dto);
39+
40+
JsConfig.Reset();
41+
42+
ConfigureCustomFormatType();
43+
TestRoundTripValue(dto);
44+
}
45+
46+
private static void ConfigureCustomFormatType()
47+
{
48+
JsConfig<CustomFormatType>.RawSerializeFn = value =>
49+
value.Value.ToString("x");
50+
51+
JsConfig<CustomFormatType>.DeSerializeFn = str =>
52+
new CustomFormatType(int.Parse(str, NumberStyles.HexNumber));
53+
}
54+
55+
private static void TestRoundTripValue(Dto dto)
56+
{
57+
var json = dto.ToJson();
58+
var fromJson = json.FromJson<Dto>();
59+
Assert.AreEqual(dto.CustomFormatTypeProperty.Value, dto.CustomFormatTypeProperty.Value);
60+
}
61+
}
62+
}

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
<Compile Include="AttributeTests.cs" />
192192
<Compile Include="CsvTypeTests.cs" />
193193
<Compile Include="HttpUtilsTests.cs" />
194+
<Compile Include="Issues\JsConfigIssues.cs" />
194195
<Compile Include="JsonTests\InheritAbstractTests.cs" />
195196
<Compile Include="JsonTests\JsonEnumTests.cs" />
196197
<Compile Include="JsonTests\InvalidJsonTests.cs" />

tests/ServiceStack.Text.Tests/StructTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public void Test_structs_with_double_quotes()
9494

9595
JsConfig<Text>.SerializeFn = text => text.ToString();
9696
JsConfig<Text>.DeSerializeFn = v => new Text(v);
97-
JsConfig<Foo>.Refresh();
9897

9998
var json = JsonSerializer.SerializeToString(dto, dto.GetType());
10099
Assert.That(json, Is.EqualTo("{\"Name\":\"My \\\"quoted\\\" name\",\"Content1\":\"My \\\"quoted\\\" content\"}"));

0 commit comments

Comments
 (0)