Customize string de-serialization but don't include properties with null values

I want to trim strings when they are deserialized so I added the following

JsConfig<string>.DeSerializeFn = str => str?.Trim();

I noticed “str” was never null and properties that weren’t included in the json are included with a value of “”.

Before adding the JsConfig

"{"Name":"John Doe","Age":18}"

After adding the JsConfig

"{"Name":"John Doe","Age":18,"Description":""}"

Is there a way to customize the de-serialization of strings but not include the property in the json if its null?

Can you please create a small repro showing the issue, you can start from this live demo on gistlyn:

https://gistlyn.com/?gist=abbbb0e707e910e6379703edd442d2db

using ServiceStack;
using ServiceStack.Text;

JsConfig<string>.DeSerializeFn = str => str?.Trim();

public class Test
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string Description { get; set; }
}

var dto = new Test { Name = " John Doe ", Age = "18" };
var json = dto.ToJson(); //= {"Name":" John Doe ","Age":"18"}	

var fromDto = json.FromJson<Test>();

var isDescriptionNull = fromDto.Description == null; //= true

I can’t show the issue just using ToJson/FromJson but I’ll work on creating an example.

I’m guessing the behavior I’m seeing is unexpected, correct?

It’s unexpected if it’s converting a null value to an empty string.

I have a small example reproducing the issue but there are a handful of files involved and I don’t see how that would work with gist. It’s doing more then just .ToJson/.FromJson, a client and server are involved. Is there a way I can send a zip?

The problem appears to be how null values are serialized for the request object. Newtonsoft for example serializes to “{”[property name]":null}" but ServiceStack.Text serializes to “{}”. If the property is serialized to “{}” the behavior is as expected.

Is there a way to ignore null like “{”[property name]":null}"? I’ve tried “JsConfig.IncludeNullValues = false” in the Configure method but that doesn’t do it.

Note: a lot of the JsConfig behavior no longer applies when you take over handling of a type.

Please publish the repro on GitHub and paste the link to it here.

[Removed link to repository]

Should now be resolved from this commit.

This change is available from v5.5.1 that’s now available on MyGet.

Note: JsConfig.IncludeNullValues only specifies whether to serialize null values or not, it has no affect in deserialization.