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

Commit c585996

Browse files
committed
Add JsConfig.UseSystemParseMethods setting
1 parent eccabd3 commit c585996

File tree

3 files changed

+98
-22
lines changed

3 files changed

+98
-22
lines changed

src/ServiceStack.Text/JsConfig.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ public static JsConfigScope With(
233233
bool? includePublicFields = null,
234234
int? maxDepth = null,
235235
EmptyCtorFactoryDelegate modelFactory = null,
236-
string[] excludePropertyReferences = null)
236+
string[] excludePropertyReferences = null,
237+
bool? useSystemParseMethods = null)
237238
{
238239
return new JsConfigScope
239240
{
@@ -269,10 +270,27 @@ public static JsConfigScope With(
269270
IncludePublicFields = includePublicFields ?? sIncludePublicFields,
270271
MaxDepth = maxDepth ?? sMaxDepth,
271272
ModelFactory = modelFactory ?? ModelFactory,
272-
ExcludePropertyReferences = excludePropertyReferences ?? sExcludePropertyReferences
273+
ExcludePropertyReferences = excludePropertyReferences ?? sExcludePropertyReferences,
274+
UseSystemParseMethods = useSystemParseMethods ?? sUseSystemParseMethods
273275
};
274276
}
275277

278+
private static bool? sUseSystemParseMethods;
279+
public static bool UseSystemParseMethods
280+
{
281+
get
282+
{
283+
return (JsConfigScope.Current != null ? JsConfigScope.Current.UseSystemParseMethods : null)
284+
?? sUseSystemParseMethods
285+
?? false;
286+
}
287+
set
288+
{
289+
if (!sConvertObjectTypesIntoStringDictionary.HasValue) sConvertObjectTypesIntoStringDictionary = value;
290+
}
291+
}
292+
293+
276294
private static bool? sConvertObjectTypesIntoStringDictionary;
277295
public static bool ConvertObjectTypesIntoStringDictionary
278296
{

src/ServiceStack.Text/JsConfigScope.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void Dispose()
5252
public bool? TryToParseNumericType { get; set; }
5353
public ParseAsType? ParsePrimitiveFloatingPointTypes { get; set; }
5454
public ParseAsType? ParsePrimitiveIntegerTypes { get; set; }
55+
public bool? UseSystemParseMethods { get; set; }
5556
public bool? ExcludeDefaultValues { get; set; }
5657
public bool? IncludeNullValues { get; set; }
5758
public bool? IncludeNullValuesInDictionaries { get; set; }

src/ServiceStack.Text/Support/StringSegmentExtensions.cs

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.CompilerServices;
33
using System.Globalization;
4+
using ServiceStack.Text.Json;
45
#if NETSTANDARD1_1
56
using Microsoft.Extensions.Primitives;
67
#endif
@@ -118,17 +119,63 @@ enum ParseState
118119
TrailingWhite
119120
}
120121

121-
public static sbyte ParseSByte(this StringSegment value) => (sbyte)ParseSignedInteger(value, sbyte.MaxValue, sbyte.MinValue);
122-
public static byte ParseByte(this StringSegment value) => (byte)ParseUnsignedInteger(value, byte.MaxValue);
122+
public static sbyte ParseSByte(this StringSegment value)
123+
{
124+
return JsConfig.UseSystemParseMethods
125+
? sbyte.Parse(value.Value)
126+
: (sbyte) ParseSignedInteger(value, sbyte.MaxValue, sbyte.MinValue);
127+
}
128+
129+
public static byte ParseByte(this StringSegment value)
130+
{
131+
return JsConfig.UseSystemParseMethods
132+
? byte.Parse(value.Value)
133+
: (byte) ParseUnsignedInteger(value, byte.MaxValue);
134+
}
135+
136+
public static short ParseInt16(this StringSegment value)
137+
{
138+
return JsConfig.UseSystemParseMethods
139+
? short.Parse(value.Value)
140+
: (short) ParseSignedInteger(value, short.MaxValue, short.MinValue);
141+
}
142+
143+
public static ushort ParseUInt16(this StringSegment value)
144+
{
145+
return JsConfig.UseSystemParseMethods
146+
? ushort.Parse(value.Value)
147+
: (ushort) ParseUnsignedInteger(value, ushort.MaxValue);
148+
}
149+
150+
151+
public static int ParseInt32(this StringSegment value)
152+
{
153+
return JsConfig.UseSystemParseMethods
154+
? int.Parse(value.Value)
155+
: (int) ParseSignedInteger(value, Int32.MaxValue, Int32.MinValue);
156+
}
123157

124-
public static short ParseInt16(this StringSegment value) => (short)ParseSignedInteger(value, short.MaxValue, short.MinValue);
125-
public static ushort ParseUInt16(this StringSegment value) => (ushort)ParseUnsignedInteger(value, ushort.MaxValue);
158+
public static uint ParseUInt32(this StringSegment value)
159+
{
160+
return JsConfig.UseSystemParseMethods
161+
? uint.Parse(value.Value)
162+
: (uint) ParseUnsignedInteger(value, UInt32.MaxValue);
163+
}
126164

127-
public static int ParseInt32(this StringSegment value) => (int)ParseSignedInteger(value, Int32.MaxValue, Int32.MinValue);
128-
public static uint ParseUInt32(this StringSegment value) => (uint)ParseUnsignedInteger(value, UInt32.MaxValue);
165+
public static long ParseInt64(this StringSegment value)
166+
{
167+
return JsConfig.UseSystemParseMethods
168+
? long.Parse(value.Value)
169+
: ParseSignedInteger(value, Int64.MaxValue, Int64.MinValue);
129170

130-
public static long ParseInt64(this StringSegment value) => ParseSignedInteger(value, Int64.MaxValue, Int64.MinValue);
131-
public static ulong ParseUInt64(this StringSegment value) => ParseUnsignedInteger(value, UInt64.MaxValue);
171+
}
172+
173+
public static ulong ParseUInt64(this StringSegment value)
174+
{
175+
return JsConfig.UseSystemParseMethods
176+
? ulong.Parse(value.Value)
177+
: ParseUnsignedInteger(value, UInt64.MaxValue);
178+
}
132179

133180
private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)
134181
{
@@ -146,7 +193,7 @@ private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)
146193
switch (state)
147194
{
148195
case ParseState.LeadingWhite:
149-
if (Char.IsWhiteSpace(c))
196+
if (JsonUtils.IsWhiteSpace(c))
150197
break;
151198
if (c == '0')
152199
{
@@ -172,7 +219,7 @@ private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)
172219
if (result > maxValue) //check only minvalue, because in absolute value it's greater than maxvalue
173220
throw new OverflowException();
174221
}
175-
else if (Char.IsWhiteSpace(c))
222+
else if (JsonUtils.IsWhiteSpace(c))
176223
{
177224
state = ParseState.TrailingWhite;
178225
}
@@ -182,7 +229,7 @@ private static ulong ParseUnsignedInteger(StringSegment value, ulong maxValue)
182229
}
183230
break;
184231
case ParseState.TrailingWhite:
185-
if (Char.IsWhiteSpace(c))
232+
if (JsonUtils.IsWhiteSpace(c))
186233
{
187234
state = ParseState.TrailingWhite;
188235
}
@@ -214,7 +261,7 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
214261
switch (state)
215262
{
216263
case ParseState.LeadingWhite:
217-
if (Char.IsWhiteSpace(c))
264+
if (JsonUtils.IsWhiteSpace(c))
218265
break;
219266

220267
if (c == '-')
@@ -256,7 +303,7 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
256303
if (result < minValue) //check only minvalue, because in absolute value it's greater than maxvalue
257304
throw new OverflowException();
258305
}
259-
else if (Char.IsWhiteSpace(c))
306+
else if (JsonUtils.IsWhiteSpace(c))
260307
{
261308
state = ParseState.TrailingWhite;
262309
} else
@@ -265,7 +312,7 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
265312
}
266313
break;
267314
case ParseState.TrailingWhite:
268-
if (Char.IsWhiteSpace(c))
315+
if (JsonUtils.IsWhiteSpace(c))
269316
{
270317
state = ParseState.TrailingWhite;
271318
} else
@@ -292,6 +339,13 @@ private static long ParseSignedInteger(StringSegment value, long maxValue, long
292339

293340
public static decimal ParseDecimal(this StringSegment value, bool allowThousands = false)
294341
{
342+
if (JsConfig.UseSystemParseMethods)
343+
{
344+
return decimal.Parse(value.Value,
345+
allowThousands ? NumberStyles.Float : (NumberStyles.Float | NumberStyles.AllowThousands),
346+
CultureInfo.InvariantCulture);
347+
}
348+
295349
if (value.Length == 0)
296350
throw new FormatException(BadFormat);
297351

@@ -312,7 +366,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
312366
switch (state)
313367
{
314368
case ParseState.LeadingWhite:
315-
if (Char.IsWhiteSpace(c))
369+
if (JsonUtils.IsWhiteSpace(c))
316370
break;
317371

318372
if (c == '-')
@@ -389,7 +443,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
389443
}
390444
}
391445
}
392-
else if (Char.IsWhiteSpace(c))
446+
else if (JsonUtils.IsWhiteSpace(c))
393447
{
394448
state = ParseState.TrailingWhite;
395449
}
@@ -411,7 +465,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
411465
}
412466
break;
413467
case ParseState.FractionNumber:
414-
if (Char.IsWhiteSpace(c))
468+
if (JsonUtils.IsWhiteSpace(c))
415469
{
416470
if (noIntegerPart)
417471
throw new FormatException(BadFormat);
@@ -484,7 +538,7 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
484538
}
485539
break;
486540
case ParseState.TrailingWhite:
487-
if (!Char.IsWhiteSpace(c))
541+
if (!JsonUtils.IsWhiteSpace(c))
488542
throw new FormatException(BadFormat);
489543
break;
490544
}
@@ -534,14 +588,17 @@ public static decimal ParseDecimal(this StringSegment value, bool allowThousands
534588

535589
public static Guid ParseGuid(this StringSegment value)
536590
{
591+
if (JsConfig.UseSystemParseMethods)
592+
return Guid.Parse(value.Value);
593+
537594
//Guid can be in one of 3 forms:
538595
//1. General `{dddddddd-dddd-dddd-dddd-dddddddddddd}` or `(dddddddd-dddd-dddd-dddd-dddddddddddd)` 8-4-4-4-12 chars
539596
//2. Hex `{0xdddddddd,0xdddd,0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}` 8-4-4-8x2 chars
540597
//3. No style `dddddddddddddddddddddddddddddddd` 32 chars
541598

542599
int i = value.Offset;
543600
int end = value.Offset + value.Length;
544-
while (Char.IsWhiteSpace(value.Buffer[i]) && i < end) i++;
601+
while (JsonUtils.IsWhiteSpace(value.Buffer[i]) && i < end) i++;
545602

546603
if (i == end)
547604
throw new FormatException(BadFormat);
@@ -552,7 +609,7 @@ public static Guid ParseGuid(this StringSegment value)
552609
result = ParseGeneralStyleGuid(new StringSegment(value.Buffer, i, end - i), out guidLen);
553610
i += guidLen;
554611

555-
while (i < end && Char.IsWhiteSpace(value.Buffer[i])) i++;
612+
while (i < end && JsonUtils.IsWhiteSpace(value.Buffer[i])) i++;
556613

557614
if (i < end)
558615
throw new FormatException(BadFormat);

0 commit comments

Comments
 (0)