Search code examples
servicestackservicestack-text

ServiceStack's SerializeFn custom serializer/deserializers - sticks across requests?


I'm using ASP.Net with MVC, and would like to have custom SerializeFn for only certain requests. It looks like the JsConfig stuff is static, and I do see the JsConfig.BeginScope() stuff to keep the config scoped, but it doesn't seem to apply to the custom serializers/deserializers. If I define a custom SerializeFn, is it going to stick around across requests because it's static? If so, is there some way to stop that from happening?

Thanks.


Solution

  • The Custom SerializerFn's are static and not overridable in JsConfigScope scoped configuration so they do apply to all requests.

    As custom serializers can't be limited in scope, one solution would be to map it to a custom type and attach the serializer to that:

    public class CustomPoco : Poco {}
    
    JsConfig<CustomPoco>.SerializerFn = //...;
    

    So when you need special serialization you can map it to your custom type and serialize that:

    var customType = dto.ConvertTo<CustomPoco>();
    
    var jsonWithCustomSerialization = customType.ToJson();