Venkatakrishnan
Venkatakrishnan

Reputation: 31

Including/Excluding null values at a DTO level - Service Stack

Is it possible in service stack to include/exclude null values at a DTO/property level rather than on the whole using "JsConfig.IncludeNullValues". I have a scenario where i need specific responses to have null values in the returned JSON.

Upvotes: 3

Views: 911

Answers (1)

Rachid
Rachid

Reputation: 832

Using JsConfig scope block, just drop these lines into your AppHost Configure method:

JsConfig<NotNullDtoResponse>.RawSerializeFn = (obj) =>
{
    using(JsConfig.With(new Config { IncludeNullValues = true }))
    {
        return obj.ToJson();
    }
};

JsConfig<NotNullDtoResponse>.RawDeserializeFn = (json) =>
{
    using(JsConfig.With(new Config { IncludeNullValues = true }))
    {
        return JsonSerializer.DeserializeFromString<NotNullDtoResponse>(json);
    }
};

NotNullDtoResponse is your responseDTO

Upvotes: 2

Related Questions