I have my own version of CredentialAutenticationProvider and in its Authenticate
method I add a property named DisplayName
to a Dictionary<string,string>
. If the VALUE is null, the serializer drops the entire key - value pair. Here the screenshot from the debugger:
Elements [4] and [8] have valid keys but both values are NULL
And this is what is serialized and arrives at the client side (a WPF C# application):
{
"userId": "14",
"sessionId": "J8nbtcBAS2ag00ZBVGmn",
"userName": "tbednarz",
"displayName": "tbednarz",
"responseStatus": {
},
"meta": {
"TenantCode": "t-63211725",
"ResetPwdRequired": "True",
"IsLockedOut": "False",
"Email": "thomas@bednarz.ch",
"RemoteIP": "::ffff:10.66.66.50",
"UserName": "tbednarz",
"UserId": "14"
}
}
The meta
object does not contain the two elements with the NULL values at all. Why are Elements of a dictionary removed, if the value is NULL? It is totally legal to have NULL values in dictionaries or key-value pairs in both c# and/ or JavaScript.
In C# it is correct to do something like var fullName = meta["FullName"]
and then fullName equals NULL
.
If the Serializer removes all NULL values, you are forced to test with Meta.ContainsKey
or Meta.TryGetValue
, which is no fun if you have larger dictionaries and the data contract in the API defines, that all keys are present (but the values may be NULL).
I am using MongoDB as data store. It has its own JSON/BSON serializer that returns dictionaries with NULL values correctly (does NOT remove entries).
Bug?
(I am running still .NETCore 2.1 and ServiceStack 5.5 on RedHat)