SmartFormat icon indicating copy to clipboard operation
SmartFormat copied to clipboard

Problems detecting null in Jobjects

Open knabb opened this issue 4 years ago • 3 comments

I cant seem to get null detection to work when using Jobject. If I understand the wiki correctly, the example below should detect null and format IsNull in the first output string below. However, in both tests I get NotNull. Am I doing something wrong, or is this a bug? Thanks

Found in SmartFormat 2.6.1 .net Framework 4.7.2

Example code:

var formatter = Smart.CreateDefaultSmartFormat();
JObject testObject = new JObject();

testObject["Attr1"] = null;
string output = formatter.Format("Null Test {Attr1:IsNull|NotNull}", testObject);

testObject["Attr1"] = "something";
string output2 = formatter.Format("Null Test {Attr1:IsNull|NotNull}", testObject);

knabb avatar Dec 09 '21 16:12 knabb

Yes that's a bug in the JsonSource, which is fixed in v3. There, we also have an explicit NullFormatter. => As a workaround use Attr1.ToString. Note that the second option is used for null.

axunonb avatar Dec 09 '21 18:12 axunonb

Ah ok. Thanks for the info! That workaround solves my problem for now. I really appreciate the help.

knabb avatar Dec 09 '21 19:12 knabb

For v3.0.0 it's best to use the NullFormatter

var formatter2 = Smart.CreateDefaultSmartFormat();
JObject testObject = new JObject();

testObject["Attr1"] = null;
string output2 = formatter2.Format("Null Test {Attr1:isnull:IsNull|NotNull}", testObject);
// result: "Null Test IsNull"

testObject["Attr1"] = "something";
output2 = formatter2.Format("Null Test {Attr1:isnull:IsNull|NotNull}", testObject);
// result: "Null Test NotNull"

axunonb avatar Dec 27 '21 17:12 axunonb