diff options
Diffstat (limited to 'vendor/github.com/zclconf/go-cty/cty/json/simple.go')
-rw-r--r-- | vendor/github.com/zclconf/go-cty/cty/json/simple.go | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/vendor/github.com/zclconf/go-cty/cty/json/simple.go b/vendor/github.com/zclconf/go-cty/cty/json/simple.go new file mode 100644 index 0000000..507c9cc --- /dev/null +++ b/vendor/github.com/zclconf/go-cty/cty/json/simple.go | |||
@@ -0,0 +1,41 @@ | |||
1 | package json | ||
2 | |||
3 | import ( | ||
4 | "github.com/zclconf/go-cty/cty" | ||
5 | ) | ||
6 | |||
7 | // SimpleJSONValue is a wrapper around cty.Value that adds implementations of | ||
8 | // json.Marshaler and json.Unmarshaler for simple-but-type-lossy automatic | ||
9 | // encoding and decoding of values. | ||
10 | // | ||
11 | // The couplet Marshal and Unmarshal both take extra type information to | ||
12 | // inform the encoding and decoding process so that all of the cty types | ||
13 | // can be represented even though JSON's type system is a subset. | ||
14 | // | ||
15 | // SimpleJSONValue instead takes the approach of discarding the value's type | ||
16 | // information and then deriving a new type from the stored structure when | ||
17 | // decoding. This results in the same data being returned but not necessarily | ||
18 | // with exactly the same type. | ||
19 | // | ||
20 | // For information on how types are inferred when decoding, see the | ||
21 | // documentation of the function ImpliedType. | ||
22 | type SimpleJSONValue struct { | ||
23 | cty.Value | ||
24 | } | ||
25 | |||
26 | // MarshalJSON is an implementation of json.Marshaler. See the documentation | ||
27 | // of SimpleJSONValue for more information. | ||
28 | func (v SimpleJSONValue) MarshalJSON() ([]byte, error) { | ||
29 | return Marshal(v.Value, v.Type()) | ||
30 | } | ||
31 | |||
32 | // UnmarshalJSON is an implementation of json.Unmarshaler. See the | ||
33 | // documentation of SimpleJSONValue for more information. | ||
34 | func (v *SimpleJSONValue) UnmarshalJSON(buf []byte) error { | ||
35 | t, err := ImpliedType(buf) | ||
36 | if err != nil { | ||
37 | return err | ||
38 | } | ||
39 | v.Value, err = Unmarshal(buf, t) | ||
40 | return err | ||
41 | } | ||