blob: 10c8cb837ed5fd3050509de4c158ffaf52d5e089 [file] [log] [blame]
sslobodrd046be82019-01-16 10:02:22 -05001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package json
18
19import (
20 "bytes"
21 "encoding/json"
22 "io"
23)
24
25// NewEncoder delegates to json.NewEncoder
26// It is only here so this package can be a drop-in for common encoding/json uses
27func NewEncoder(w io.Writer) *json.Encoder {
28 return json.NewEncoder(w)
29}
30
31// Marshal delegates to json.Marshal
32// It is only here so this package can be a drop-in for common encoding/json uses
33func Marshal(v interface{}) ([]byte, error) {
34 return json.Marshal(v)
35}
36
37// Unmarshal unmarshals the given data
38// If v is a *map[string]interface{}, numbers are converted to int64 or float64
39func Unmarshal(data []byte, v interface{}) error {
40 switch v := v.(type) {
41 case *map[string]interface{}:
42 // Build a decoder from the given data
43 decoder := json.NewDecoder(bytes.NewBuffer(data))
44 // Preserve numbers, rather than casting to float64 automatically
45 decoder.UseNumber()
46 // Run the decode
47 if err := decoder.Decode(v); err != nil {
48 return err
49 }
50 // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
51 return convertMapNumbers(*v)
52
53 case *[]interface{}:
54 // Build a decoder from the given data
55 decoder := json.NewDecoder(bytes.NewBuffer(data))
56 // Preserve numbers, rather than casting to float64 automatically
57 decoder.UseNumber()
58 // Run the decode
59 if err := decoder.Decode(v); err != nil {
60 return err
61 }
62 // If the decode succeeds, post-process the map to convert json.Number objects to int64 or float64
63 return convertSliceNumbers(*v)
64
65 default:
66 return json.Unmarshal(data, v)
67 }
68}
69
70// convertMapNumbers traverses the map, converting any json.Number values to int64 or float64.
71// values which are map[string]interface{} or []interface{} are recursively visited
72func convertMapNumbers(m map[string]interface{}) error {
73 var err error
74 for k, v := range m {
75 switch v := v.(type) {
76 case json.Number:
77 m[k], err = convertNumber(v)
78 case map[string]interface{}:
79 err = convertMapNumbers(v)
80 case []interface{}:
81 err = convertSliceNumbers(v)
82 }
83 if err != nil {
84 return err
85 }
86 }
87 return nil
88}
89
90// convertSliceNumbers traverses the slice, converting any json.Number values to int64 or float64.
91// values which are map[string]interface{} or []interface{} are recursively visited
92func convertSliceNumbers(s []interface{}) error {
93 var err error
94 for i, v := range s {
95 switch v := v.(type) {
96 case json.Number:
97 s[i], err = convertNumber(v)
98 case map[string]interface{}:
99 err = convertMapNumbers(v)
100 case []interface{}:
101 err = convertSliceNumbers(v)
102 }
103 if err != nil {
104 return err
105 }
106 }
107 return nil
108}
109
110// convertNumber converts a json.Number to an int64 or float64, or returns an error
111func convertNumber(n json.Number) (interface{}, error) {
112 // Attempt to convert to an int64 first
113 if i, err := n.Int64(); err == nil {
114 return i, nil
115 }
116 // Return a float64 (default json.Decode() behavior)
117 // An overflow will return an error
118 return n.Float64()
119}