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

Commit 6d9b6fb

Browse files
committed
Add support for initializing JsConfigScope from a queryString
1 parent 624351a commit 6d9b6fb

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,90 @@ public static JsConfigScope BeginScope()
3131
return new JsConfigScope();
3232
}
3333

34+
public static JsConfigScope CreateScope(string config)
35+
{
36+
if (string.IsNullOrEmpty(config))
37+
return null;
38+
39+
var scope = BeginScope();
40+
var items = config.Split(',');
41+
foreach (var item in items)
42+
{
43+
var parts = item.SplitOnFirst(':');
44+
var key = parts[0].ToLower();
45+
var boolValue = parts.Length == 1 || (parts[1].ToLower() != "false" && parts[1] != "0");
46+
47+
switch (key)
48+
{
49+
case "convertobjecttypesintostringdictionary":
50+
scope.ConvertObjectTypesIntoStringDictionary = boolValue;
51+
break;
52+
case "trytoparseprimitivetypevalues":
53+
scope.TryToParsePrimitiveTypeValues = boolValue;
54+
break;
55+
case "trytoparsenumerictype":
56+
scope.TryToParseNumericType = boolValue;
57+
break;
58+
case "excludedefaultvalues":
59+
scope.ExcludeDefaultValues = boolValue;
60+
break;
61+
case "includenullvalues":
62+
scope.IncludeNullValues = boolValue;
63+
break;
64+
case "includenullvaluesindictionaries":
65+
scope.IncludeNullValuesInDictionaries = boolValue;
66+
break;
67+
case "includedefaultenums":
68+
scope.IncludeDefaultEnums = boolValue;
69+
break;
70+
case "excludetypeinfo":
71+
scope.ExcludeTypeInfo = boolValue;
72+
break;
73+
case "includetypeinfo":
74+
scope.IncludeTypeInfo = boolValue;
75+
break;
76+
case "emitcamelcasenames":
77+
scope.EmitCamelCaseNames = boolValue;
78+
break;
79+
case "emitlowercaseunderscorenames":
80+
scope.EmitLowercaseUnderscoreNames = boolValue;
81+
break;
82+
case "preferinterfaces":
83+
scope.PreferInterfaces = boolValue;
84+
break;
85+
case "throwondeserializationerror":
86+
scope.ThrowOnDeserializationError = boolValue;
87+
break;
88+
case "treatenumasinteger":
89+
scope.TreatEnumAsInteger = boolValue;
90+
break;
91+
case "skipdatetimeconversion":
92+
scope.SkipDateTimeConversion = boolValue;
93+
break;
94+
case "alwaysuseutc":
95+
scope.AlwaysUseUtc = boolValue;
96+
break;
97+
case "assumeutc":
98+
scope.AssumeUtc = boolValue;
99+
break;
100+
case "appendutcoffset":
101+
scope.AppendUtcOffset = boolValue;
102+
break;
103+
case "escapeunicode":
104+
scope.EscapeUnicode = boolValue;
105+
break;
106+
case "includepublicfields":
107+
scope.IncludePublicFields = boolValue;
108+
break;
109+
case "reuseStringBuffer":
110+
scope.ReuseStringBuffer = boolValue;
111+
break;
112+
}
113+
}
114+
115+
return scope;
116+
}
117+
34118
public static JsConfigScope With(
35119
bool? convertObjectTypesIntoStringDictionary = null,
36120
bool? tryToParsePrimitiveTypeValues = null,

tests/ServiceStack.Text.Tests/JsConfigTests.cs

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

45
namespace ServiceStack.Text.Tests
@@ -187,4 +188,19 @@ class TestObject
187188
private const string AssertMessageFormat = "Cannot find correct property value ({0})";
188189
}
189190

191+
[TestFixture]
192+
public class JsConfigCreateTests
193+
{
194+
[Test]
195+
public void Does_create_scope_from_string()
196+
{
197+
var scope = JsConfig.CreateScope("EmitCamelCaseNames,emitlowercaseunderscorenames,IncludeNullValues:false,ExcludeDefaultValues:0,IncludeDefaultEnums:1");
198+
Assert.That(scope.EmitCamelCaseNames.Value);
199+
Assert.That(scope.EmitLowercaseUnderscoreNames.Value);
200+
Assert.That(!scope.IncludeNullValues.Value);
201+
Assert.That(!scope.ExcludeDefaultValues.Value);
202+
Assert.That(scope.IncludeDefaultEnums.Value);
203+
scope.Dispose();
204+
}
205+
}
190206
}

0 commit comments

Comments
 (0)