kimsagro
kimsagro

Reputation: 17435

ServiceStack - Set serialize function within a scope

I currently scope some configuration values to prevent any changes being made globally:

 using(var scope = JsConfig.BeginScope()) 
 {
    scope.DateHandler = JsonDateHandler.ISO8601;
    scope.EmitCamelCaseNames = true;

    // perform serialization
 }

However I now need to format Guids with a dash which requires me to change the serialize function for guids as below:

 JsConfig<Guid>.SerializeFn = guid => guid.ToString("D");

Is is possible to make this change within a scope as with the other configuration settings above ?

Upvotes: 2

Views: 339

Answers (1)

kampsj
kampsj

Reputation: 3149

No you cannot scope it. But you can add then remove the serialization methods as you need them managing the scope yourself.

JsConfig<Guid>.SerializeFn = guid => guid.ToString("D");
Debug.WriteLine(new Guid().ToJson());

JsConfig<Guid>.SerializeFn = null;
Debug.WriteLine(new Guid().ToJson());

This will output:

"00000000-0000-0000-0000-000000000000"
"00000000000000000000000000000000"

Upvotes: 1

Related Questions