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

Commit 2e758cd

Browse files
authored
Fix issue where DeserializeFn is not cleared by reset (#536)
Co-authored-by: Arjan Veenstra <[email protected]>
1 parent d4239f7 commit 2e758cd

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

src/ServiceStack.Text/Json/JsonReader.Generic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static void Refresh()
7272
return;
7373

7474
ReadFn = JsonReader.Instance.GetParseStringSpanFn<T>();
75+
JsConfig.AddUniqueType(typeof(T));
7576
}
7677

7778
public static ParseStringDelegate GetParseFn() => ReadFn != null

src/ServiceStack.Text/Json/JsonWriter.Generic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public static void Refresh()
167167
CacheFn = typeof(T) == typeof(object)
168168
? JsonWriter.WriteLateBoundObject
169169
: JsonWriter.Instance.GetWriteFn<T>();
170+
JsConfig.AddUniqueType(typeof(T));
170171
}
171172

172173
public static WriteObjectDelegate WriteFn()

src/ServiceStack.Text/Jsv/JsvReader.Generic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public static void Refresh()
7070
return;
7171

7272
ReadFn = JsvReader.Instance.GetParseStringSpanFn<T>();
73+
JsConfig.AddUniqueType(typeof(T));
7374
}
7475

7576
public static ParseStringDelegate GetParseFn() => ReadFn != null

src/ServiceStack.Text/Jsv/JsvWriter.Generic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public static void Refresh()
124124
CacheFn = typeof(T) == typeof(object)
125125
? JsvWriter.WriteLateBoundObject
126126
: JsvWriter.Instance.GetWriteFn<T>();
127+
JsConfig.AddUniqueType(typeof(T));
127128
}
128129

129130
public static WriteObjectDelegate WriteFn()

tests/ServiceStack.Text.Tests/Issues/JsConfigIssues.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public CustomFormatType(int value)
2222
{
2323
_value = value;
2424
}
25+
26+
public override string ToString()
27+
{
28+
return _value.ToString();
29+
}
2530
}
2631

2732
class Dto
@@ -44,6 +49,18 @@ public void CallReset_AfterSerializingOnce_WithCustomSerializationForProperty_Do
4449

4550
TestRoundTripValue(dto);
4651
}
52+
53+
[Test]
54+
public void CallReset_AfterSerializingOnce_WithCustomSerializationForProperty_MustClearCustomSerialization()
55+
{
56+
var dto = new Dto { CustomFormatTypeProperty = new CustomFormatType(12345) };
57+
JsConfig<CustomFormatType>.DeSerializeFn = str =>
58+
new CustomFormatType(int.Parse(str));
59+
var json = dto.ToJson();
60+
JsConfig.Reset();
61+
var fromJson = json.FromJson<Dto>();
62+
Assert.That(fromJson.CustomFormatTypeProperty.Value, Is.EqualTo(0));
63+
}
4764

4865
private static void ConfigureCustomFormatType()
4966
{

0 commit comments

Comments
 (0)