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

Commit 4f09feb

Browse files
committed
Proide better Reset for JsConfig<T>, add SerializEmitLowerCaseUnderscoreNamesTests to tests
1 parent 1431d5b commit 4f09feb

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,8 @@ public static void Reset()
792792
{
793793
RawSerializeFn = null;
794794
DeSerializeFn = null;
795+
ExcludePropertyNames = null;
796+
EmitCamelCaseNames = EmitLowercaseUnderscoreNames = IncludeTypeInfo = ExcludeTypeInfo = null;
795797
}
796798
}
797799

tests/ServiceStack.Text.Tests/JsConfigTests.cs

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using NUnit.Framework;
1+
using System.Diagnostics;
2+
using NUnit.Framework;
23

34
namespace ServiceStack.Text.Tests
45
{
@@ -44,4 +45,153 @@ public class Bar
4445
{
4546
public string FooBar { get; set; }
4647
}
48+
49+
50+
[TestFixture]
51+
public class SerializEmitLowerCaseUnderscoreNamesTests
52+
{
53+
//[SetUp]
54+
//public void Reset()
55+
//{
56+
// JsConfig.EmitLowercaseUnderscoreNames = false;
57+
// JsConfig<TestObject>.EmitLowercaseUnderscoreNames = null;
58+
//}
59+
60+
[Test]
61+
public void TestJsonDataWithJsConfigScope()
62+
{
63+
using (JsConfig.With(emitLowercaseUnderscoreNames:true,
64+
propertyConvention:PropertyConvention.Lenient))
65+
AssertObjectJson();
66+
}
67+
68+
[Test]
69+
public void TestCloneObjectWithJsConfigScope()
70+
{
71+
using (JsConfig.With(emitLowercaseUnderscoreNames: true,
72+
propertyConvention: PropertyConvention.Lenient))
73+
AssertObject();
74+
}
75+
76+
[Test]
77+
public void TestJsonDataWithJsConfigGlobal()
78+
{
79+
JsConfig.EmitLowercaseUnderscoreNames = true;
80+
JsConfig.PropertyConvention = PropertyConvention.Lenient;
81+
82+
AssertObjectJson();
83+
84+
JsConfig.Reset();
85+
}
86+
87+
[Test]
88+
public void TestCloneObjectWithJsConfigGlobal()
89+
{
90+
JsConfig.EmitLowercaseUnderscoreNames = true;
91+
JsConfig.PropertyConvention = PropertyConvention.Lenient;
92+
93+
AssertObject();
94+
95+
JsConfig.Reset();
96+
}
97+
98+
[Test]
99+
public void TestJsonDataWithJsConfigLocal()
100+
{
101+
JsConfig.EmitLowercaseUnderscoreNames = true;
102+
JsConfig.PropertyConvention = PropertyConvention.Lenient;
103+
104+
AssertObjectJson();
105+
106+
JsConfig.Reset();
107+
}
108+
109+
[Test]
110+
public void TestCloneObjectWithJsConfigLocal()
111+
{
112+
JsConfig.EmitLowercaseUnderscoreNames = false;
113+
JsConfig<TestObject>.EmitLowercaseUnderscoreNames = true;
114+
JsConfig.PropertyConvention = PropertyConvention.Lenient;
115+
116+
AssertObject();
117+
118+
JsConfig.Reset();
119+
}
120+
121+
[Test]
122+
public void TestCloneObjectWithoutLowercaseThroughJsConfigLocal()
123+
{
124+
JsConfig.EmitLowercaseUnderscoreNames = true;
125+
JsConfig<TestObject>.EmitLowercaseUnderscoreNames = false;
126+
JsConfig.PropertyConvention = PropertyConvention.Lenient;
127+
128+
AssertObject();
129+
130+
JsConfig.Reset();
131+
}
132+
133+
private void AssertObject()
134+
{
135+
var obj = CreateObject();
136+
var clonedObj = Deserialize(Serialize(obj));
137+
138+
Assert.AreEqual(obj.Id, clonedObj.Id, AssertMessageFormat.Fmt("Id"));
139+
Assert.AreEqual(obj.RootId, clonedObj.RootId, AssertMessageFormat.Fmt("RootId"));
140+
Assert.AreEqual(obj.DisplayName, clonedObj.DisplayName, AssertMessageFormat.Fmt("DisplayName"));
141+
}
142+
143+
private void AssertObjectJson()
144+
{
145+
var obj = CreateObject();
146+
var json = Serialize(obj);
147+
AssertObjectJson("Object Json: {0}", json);
148+
149+
var cloned = CloneObject(obj);
150+
var clonedJson = Serialize(cloned);
151+
AssertObjectJson("Clone Object Json: {0}", clonedJson);
152+
}
153+
154+
private void AssertObjectJson(string traceFormat, string json)
155+
{
156+
Trace.WriteLine(string.Format(traceFormat, json));
157+
158+
Assert.True(json.Contains("\"root_id\":100,"), AssertMessageFormat.Fmt("root_id"));
159+
Assert.True(json.Contains("\"display_name\":\"Test object\""), AssertMessageFormat.Fmt("display_name"));
160+
}
161+
162+
private string Serialize(TestObject obj)
163+
{
164+
return obj.ToJson();
165+
}
166+
167+
private TestObject Deserialize(string str)
168+
{
169+
return str.FromJson<TestObject>();
170+
}
171+
172+
private TestObject CreateObject()
173+
{
174+
return new TestObject
175+
{
176+
Id = 1,
177+
RootId = 100,
178+
DisplayName = "Test object"
179+
};
180+
}
181+
182+
private TestObject CloneObject(TestObject src)
183+
{
184+
return Deserialize(Serialize(src));
185+
}
186+
187+
class TestObject
188+
{
189+
public int Id { get; set; }
190+
public int RootId { get; set; }
191+
public string DisplayName { get; set; }
192+
}
193+
194+
private const string AssertMessageFormat = "Cannot find correct property value ({0})";
195+
}
196+
47197
}

0 commit comments

Comments
 (0)