You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/recipes/null-support.md
+68-14Lines changed: 68 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,17 @@
1
1
<!--
2
2
{
3
-
"title": "Null Support",
3
+
"title": "Null Handling in CFML",
4
4
"id": "null_support",
5
5
"related": [
6
6
"function-isnull",
7
7
"function-nullvalue",
8
8
"developing-with-lucee-server",
9
-
"function-structkeyexists"
9
+
"function-structkeyexists",
10
+
"function-serializejson",
11
+
"function-deserializejson",
12
+
"tag-application"
10
13
],
11
-
"description": "This document explains how to set null support in the Lucee server admin, assigning `null` value for a variable and how to use `null` and `nullvalue`.",
14
+
"description": "Understand the differences between partial and full null support - how it affects structKeyExists(), JSON serialization, queries and variable assignment.",
12
15
"keywords": [
13
16
"Null support",
14
17
"null keyword",
@@ -17,30 +20,35 @@
17
20
"Lucee"
18
21
],
19
22
"categories": [
20
-
"core"
23
+
"core",
24
+
"decision"
21
25
]
22
26
}
23
27
-->
24
28
25
-
# Null Support
29
+
# Null Support in CFML
26
30
27
-
This document explains how to set null support in the Lucee server admin, assigning `null` value for a variable and how to use `null` and `nullvalue`.
31
+
CFML supports two null handling modes: partial (default) and full. This recipe explains the behavioural differences and when to use each.
28
32
29
33
It is an annotation of the video found here: [https://www.youtube.com/watch?v=GSlWfLR8Frs](https://www.youtube.com/watch?v=GSlWfLR8Frs)
30
34
31
35
## Enabling NULL support
32
36
33
-
You can enable null support via the **Lucee Server Admin** --> **Language/compiler** and setting Null support to **complete support**(exclusive to Lucee) or **partial support** (default, same as Adobe CF).
37
+
You can enable null support via the **Lucee Server Admin** --> **Language/compiler** and setting Null support to **full support** or **partial support** (default).
34
38
35
-
Or via [[tag-application]]
39
+
Per Application via [[tag-application]]
36
40
37
41
```lucee
38
42
this.nullSupport = true;
39
43
```
40
44
41
-
## Explanation
45
+
Or toggle dynamically via [[tag-application]]
42
46
43
-
### Illustration 1:
47
+
```lucee
48
+
application action="update" nullsupport="true";
49
+
```
50
+
51
+
## Function Return Values
44
52
45
53
```lucee
46
54
<cfscript>
@@ -60,9 +68,9 @@ In this example, the function `test()` does not return a value. This, in effect,
60
68
61
69
If we assign the function result to a variable, i.e. `t = test();`, and reference the variable, i.e. `dump( t );` an error will be thrown when using **partial support** for null: "the key [T] does not exist". If we enable **full support**, you will be able to reference the variable without error, the dump output will be `Empty: Null` and `IsNull( t )` will evaluate `true`.
62
70
63
-
In all cases, `dump( isNull( notexisting ) );` will throw an error.
71
+
In both modes, `isNull( notexisting )` will return `true` for an undefined variable - the function is specifically designed to safely check for null/undefined values without throwing an error.
64
72
65
-
### Illustration 2:
73
+
##Query Column Values
66
74
67
75
```luceescript
68
76
query datasource="test" name="qry" {
@@ -73,11 +81,12 @@ dump( qry._null );
73
81
```
74
82
75
83
With **partial support** for NULL enabled, `dump(qry._null);` will output an **empty string**.
84
+
76
85
With **full support**, `Empty: null` will be output and `IsNull( qry._null );` will evaluate `true`.
77
86
78
-
## NullValue() function and null keyword
87
+
## [[function-nullvalue]] and the null keyword
79
88
80
-
With **partial support** for NULL, the `NullValue()` function must be used to explicitly return a null value (this will work in all scenarios). For example:
89
+
With **partial support** for NULL, the [[function-nullvalue]] function must be used to explicitly return a null value (this will work in all scenarios). For example:
81
90
82
91
```luceescript
83
92
var possibleVariable = functionThatMayOrMayNotReturnNull();
@@ -90,3 +99,48 @@ With **full support**, you are able to use the `null` keyword directly and, as i
90
99
t = null;
91
100
dump( t );
92
101
```
102
+
103
+
## StructKeyExists Behaviour
104
+
105
+
One of the most significant behavioural differences between partial and full null support is how [[function-structkeyexists]] (and the member function `keyExists()`) handles keys with null values.
106
+
107
+
With **partial support**, when a struct key holds a null value, [[function-structkeyexists]] returns `false` because the key is effectively removed:
108
+
109
+
```luceescript
110
+
s = { foo: nullValue() };
111
+
dump( structKeyExists( s, "foo" ) ); // false
112
+
dump( s.keyExists( "foo" ) ); // false
113
+
dump( structCount( s ) ); // 0 - the key doesn't exist
114
+
```
115
+
116
+
This standard CFML behaviour can be surprising when you explicitly set a key to null but then can't detect its presence.
117
+
118
+
With **full support**, keys explicitly set to `null` still exist - they just hold a null value:
119
+
120
+
```luceescript
121
+
s = { foo: null };
122
+
dump( structKeyExists( s, "foo" ) ); // true
123
+
dump( s.keyExists( "foo" ) ); // true
124
+
dump( structCount( s ) ); // 1 - the key exists
125
+
dump( isNull( s.foo ) ); // true - but the value is null
126
+
```
127
+
128
+
This allows you to distinguish between "key exists with null value" and "key doesn't exist at all" - which is important when working with APIs, database results, or any scenario where the absence of a key has different meaning than a null value.
129
+
130
+
## JSON Serialization
131
+
132
+
With **partial support**, null values in structs are removed before serialization, so they don't appear in the JSON output:
133
+
134
+
```luceescript
135
+
s = { name: "John", middleName: nullValue() };
136
+
dump( serializeJSON( s ) ); // {"name":"John"} - middleName is missing
137
+
```
138
+
139
+
With **full support**, null values are properly serialized as JSON `null` via [[function-serializejson]]:
140
+
141
+
```luceescript
142
+
s = { name: "John", middleName: null };
143
+
dump( serializeJSON( s ) ); // {"name":"John","middleName":null}
144
+
```
145
+
146
+
This is critical when working with APIs that expect explicit `null` values rather than missing keys.
0 commit comments