Problems detecting null in Jobjects
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);
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.
Ah ok. Thanks for the info! That workaround solves my problem for now. I really appreciate the help.
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"