Skip to content

Commit

Permalink
function/stdlib: jsonencode should produce a string representation of…
Browse files Browse the repository at this point in the history
… null
  • Loading branch information
pselle authored May 28, 2020
1 parent 6e20bc2 commit e76fe06
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cty/function/stdlib/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var JSONEncodeFunc = function.New(&function.Spec{
Name: "val",
Type: cty.DynamicPseudoType,
AllowDynamicType: true,
AllowNull: true,
},
},
Type: function.StaticReturnType(cty.String),
Expand All @@ -24,6 +25,10 @@ var JSONEncodeFunc = function.New(&function.Spec{
return cty.UnknownVal(retType), nil
}

if val.IsNull() {
return cty.StringVal("null"), nil
}

buf, err := json.Marshal(val, val.Type())
if err != nil {
return cty.NilVal, err
Expand Down
4 changes: 4 additions & 0 deletions cty/function/stdlib/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func TestJSONEncode(t *testing.T) {
cty.DynamicVal,
cty.UnknownVal(cty.String),
},
{
cty.NullVal(cty.String),
cty.StringVal("null"),
},
}

for _, test := range tests {
Expand Down

0 comments on commit e76fe06

Please sign in to comment.