Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# 1.4.2 (Unreleased)

* `function/stdlib`: The `jsonencode` function will now correctly accept a null as its argument, and produce the JSON representation `"null"` rather than returning an error. ([#54](https://github.com/zclconf/go-cty/pull/54))
* `convert`: Don't panic when asked to convert a tuple of objects to a list type constraint containing a nested `cty.DynamicPseudoType`. ([#53](https://github.com/zclconf/go-cty/pull/53))

# 1.4.1 (March 5, 2025)
Expand Down
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