@@ -16,6 +16,7 @@ package valueshim
1616
1717import (
1818 "encoding/json"
19+ "fmt"
1920 "math/big"
2021
2122 "github.com/hashicorp/terraform-plugin-go/tftypes"
@@ -31,21 +32,22 @@ func tftypeValueToJSON(typ tftypes.Type, v tftypes.Value) ([]byte, error) {
3132}
3233
3334func jsonMarshal (v tftypes.Value , typ tftypes.Type , p * tftypes.AttributePath ) (interface {}, error ) {
34- if v .IsNull () {
35- return nil , nil
36- }
3735 if ! v .IsKnown () {
3836 return nil , p .NewErrorf ("unknown values cannot be serialized to JSON" )
3937 }
38+ if typ .Is (tftypes .DynamicPseudoType ) {
39+ return jsonMarshalDynamicPseudoType (v , typ , p )
40+ }
41+ if v .IsNull () {
42+ return nil , nil
43+ }
4044 switch {
4145 case typ .Is (tftypes .String ):
4246 return jsonMarshalString (v , typ , p )
4347 case typ .Is (tftypes .Number ):
4448 return jsonMarshalNumber (v , typ , p )
4549 case typ .Is (tftypes .Bool ):
4650 return jsonMarshalBool (v , typ , p )
47- case typ .Is (tftypes .DynamicPseudoType ):
48- return jsonMarshalDynamicPseudoType (v , typ , p )
4951 case typ .Is (tftypes.List {}):
5052 return jsonMarshalList (v , typ .(tftypes.List ).ElementType , p )
5153 case typ .Is (tftypes.Set {}):
@@ -99,10 +101,11 @@ func jsonMarshalDynamicPseudoType(v tftypes.Value, _ tftypes.Type, p *tftypes.At
99101 if err != nil {
100102 return nil , p .NewError (err )
101103 }
102- return map [string ]interface {}{
103- "type" : string (typeJSON ),
104- "value" : valJSON ,
105- }, nil
104+ marshalledValJSON , err := json .Marshal (valJSON )
105+ if err != nil {
106+ return nil , p .NewError (err )
107+ }
108+ return json .RawMessage (fmt .Sprintf (`{"value": %s, "type": %s}` , marshalledValJSON , typeJSON )), nil
106109}
107110
108111func jsonMarshalList (v tftypes.Value , elementType tftypes.Type , p * tftypes.AttributePath ) (interface {}, error ) {
0 commit comments