I regret my answer. It didn’t work so beautifully after all.
Even with the using(new JSConfig.With(..)) { /* api calls here */ }
there’s a big chance of configuration leak.
At least this is my experience. It’s just too easy to make a mistake (human error). I’ve had lots of trouble with serialization/desalinization issues earlier, so when I see this it gives me goosebumps.
A few examples:
First the long form with brackets, which deserializes correctly.
using (JsConfig.With(new Config
{
TextCase = TextCase.CamelCase,
PropertyConvention = PropertyConvention.Lenient
}))
{
response.FromJson<MyType>()
}
Tempting to try the syntax without the brackets:
using (JsConfig.With(new Config
{
TextCase = TextCase.SnakeCase,
PropertyConvention = PropertyConvention.Lenient
}));
response.FromJson<MyType>()
This short form of using usually works. But not in this case (don’t know why).
So back to the long form w. brackets, but it’s too big to have everywhere, so let’s keep the config in a variable to avoid some repetition:
// kept globally in my class
var theConfig = JsConfig.With(new Config
{
TextCase = TextCase.CamelCase,
PropertyConvention = PropertyConvention.Lenient
}));
// then many places:
using(theConfig) {
response.FromJson<MyType>()
}
Deserialization works, but now I discovered snake-case in my database (stored by OrmLite). So the config has leaked into the global config used by OrmLite (and more).
I’m not reporting this as a ServiceStack bug – it’s probably a human error by myself – but it shows how easily the JSConfig can leak to places you don’t want, just by programming normally.
Recommendation
So this is what I’m using now – a separate JSON parser for stuff not-ServiceStack, to avoid any mixup. Here I’m using Newtonsoft’s:
// keep a class-global instance of this:
JsonSerializerSettings mySettings = new JsonSerializerSettings {
ContractResolver = new DefaultContractResolver() {
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
// then wherever:
var parsedResult = JsonConvert.DeserializeObject<MyType>(responseBody, mySettings );