DeserializeFromString fails with System.FormatException

Hi all,

Apologies if the title is too long (kept getting a make it more descriptive message)

Using ServiceStack.Text v4.5.12. I would expect JsonSerializer.DeserializeFromString (string, type) to work when using


However I get a:

‘JsonSerializer.DeserializeFromString(“011”,typeof(Int32))’ threw an exception of type ‘System.FormatException’
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233033
HelpLink: null
InnerException: null
Message: “Input string was not in a correct format.”
Source: “ServiceStack.Text”
StackTrace: " at ServiceStack.Text.Support.StringSegmentExtensions.ParseSignedInteger(StringSegment value, Int64 maxValue, Int64 minValue)\r\n at ServiceStack.Text.Common.DeserializeBuiltin1.<>c.<GetParseStringSegmentFn>b__7_5(StringSegment value)\r\n at ServiceStack.Text.Common.JsReader1.<>c__DisplayClass4_0`1.b__4(StringSegment value)\r\n at ServiceStack.Text.Json.JsonReader.<>c__DisplayClass2_0.b__0(String v)"
TargetSite: {Int64 ParseSignedInteger(ServiceStack.Text.Support.StringSegment, Int64, Int64)}

Am I missing a configuration parameter?


JSON specification does not allow to use leading zeroes in numbers. However you can use custom deserialization for Int32 type. It’s not clear which result do you want to get (“011” also can be treated as a number in octal or binary form), but if you want to deserialize decimal numbers with leading zeroes, you can use

JsConfig<Int32>.DeSerializeFn = x => Int32.Parse(x, CultureInfo.InvariantCulture);

Here is working example in Gistlyn

Thanks. I’ve made the change suggested. I was expecting to get 11 as an int.