github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/json/json.go (about)

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