Skip to content

Commit 7574211

Browse files
psellebbasata
authored andcommitted
function/stdlib: jsonencode should produce a string representation of null
1 parent 91a14dc commit 7574211

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

cty/function/stdlib/json.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var JSONEncodeFunc = function.New(&function.Spec{
1212
Name: "val",
1313
Type: cty.DynamicPseudoType,
1414
AllowDynamicType: true,
15+
AllowNull: true,
1516
},
1617
},
1718
Type: function.StaticReturnType(cty.String),
@@ -24,6 +25,10 @@ var JSONEncodeFunc = function.New(&function.Spec{
2425
return cty.UnknownVal(retType), nil
2526
}
2627

28+
if val.IsNull() {
29+
return cty.StringVal("null"), nil
30+
}
31+
2732
buf, err := json.Marshal(val, val.Type())
2833
if err != nil {
2934
return cty.NilVal, err

cty/function/stdlib/json_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func TestJSONEncode(t *testing.T) {
5252
cty.DynamicVal,
5353
cty.UnknownVal(cty.String),
5454
},
55+
{
56+
cty.NullVal(cty.String),
57+
cty.StringVal("null"),
58+
},
5559
}
5660

5761
for _, test := range tests {

0 commit comments

Comments
 (0)