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

Commit 3bf0736

Browse files
committed
Add support for setting enums in scope string as well
1 parent 7dd1216 commit 3bf0736

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public static JsConfigScope CreateScope(string config, JsConfigScope scope = nul
4444
{
4545
var parts = item.SplitOnFirst(':');
4646
var key = parts[0].ToLower();
47-
var boolValue = parts.Length == 1 || (parts[1].ToLower() != "false" && parts[1] != "0");
47+
var value = parts.Length == 2 ? parts[1].ToLower() : null;
48+
var boolValue = parts.Length == 1 || (value != "false" && value != "0");
4849

4950
switch (key)
5051
{
@@ -111,6 +112,51 @@ public static JsConfigScope CreateScope(string config, JsConfigScope scope = nul
111112
case "reuseStringBuffer":
112113
scope.ReuseStringBuffer = boolValue;
113114
break;
115+
case "datehandler":
116+
switch (value)
117+
{
118+
case "timestampoffset":
119+
scope.DateHandler = DateHandler.TimestampOffset;
120+
break;
121+
case "dcjscompatible":
122+
scope.DateHandler = DateHandler.DCJSCompatible;
123+
break;
124+
case "iso8601":
125+
scope.DateHandler = DateHandler.ISO8601;
126+
break;
127+
case "rfc1123":
128+
scope.DateHandler = DateHandler.RFC1123;
129+
break;
130+
case "unixtime":
131+
scope.DateHandler = DateHandler.UnixTime;
132+
break;
133+
case "unixtimems":
134+
scope.DateHandler = DateHandler.UnixTimeMs;
135+
break;
136+
}
137+
break;
138+
case "timespanhandler":
139+
switch (value)
140+
{
141+
case "durationformat":
142+
scope.TimeSpanHandler = TimeSpanHandler.DurationFormat;
143+
break;
144+
case "standardformat":
145+
scope.TimeSpanHandler = TimeSpanHandler.StandardFormat;
146+
break;
147+
}
148+
break;
149+
case "propertyconvention":
150+
switch (value)
151+
{
152+
case "lenient":
153+
scope.PropertyConvention = PropertyConvention.Lenient;
154+
break;
155+
case "strict":
156+
scope.PropertyConvention = PropertyConvention.Strict;
157+
break;
158+
}
159+
break;
114160
}
115161
}
116162

tests/ServiceStack.Text.Tests/JsConfigTests.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,12 @@ public void Does_create_scope_from_string()
201201
Assert.That(!scope.ExcludeDefaultValues.Value);
202202
Assert.That(scope.IncludeDefaultEnums.Value);
203203
scope.Dispose();
204-
}
204+
205+
scope = JsConfig.CreateScope("DateHandler:ISO8601,timespanhandler:durationformat,PropertyConvention:strict");
206+
Assert.That(scope.DateHandler, Is.EqualTo(DateHandler.ISO8601));
207+
Assert.That(scope.TimeSpanHandler, Is.EqualTo(TimeSpanHandler.DurationFormat));
208+
Assert.That(scope.PropertyConvention, Is.EqualTo(PropertyConvention.Strict));
209+
scope.Dispose();
210+
}
205211
}
206212
}

0 commit comments

Comments
 (0)