vendor/gopkg.in/yaml.v2/encode.go
changeset 246 0998f404dd31
parent 242 2a9ec03fe5a1
equal deleted inserted replaced
245:910f00ab2799 246:0998f404dd31
    11 	"strings"
    11 	"strings"
    12 	"time"
    12 	"time"
    13 	"unicode/utf8"
    13 	"unicode/utf8"
    14 )
    14 )
    15 
    15 
       
    16 // jsonNumber is the interface of the encoding/json.Number datatype.
       
    17 // Repeating the interface here avoids a dependency on encoding/json, and also
       
    18 // supports other libraries like jsoniter, which use a similar datatype with
       
    19 // the same interface. Detecting this interface is useful when dealing with
       
    20 // structures containing json.Number, which is a string under the hood. The
       
    21 // encoder should prefer the use of Int64(), Float64() and string(), in that
       
    22 // order, when encoding this type.
       
    23 type jsonNumber interface {
       
    24 	Float64() (float64, error)
       
    25 	Int64() (int64, error)
       
    26 	String() string
       
    27 }
       
    28 
    16 type encoder struct {
    29 type encoder struct {
    17 	emitter yaml_emitter_t
    30 	emitter yaml_emitter_t
    18 	event   yaml_event_t
    31 	event   yaml_event_t
    19 	out     []byte
    32 	out     []byte
    20 	flow    bool
    33 	flow    bool
    87 		e.nilv()
   100 		e.nilv()
    88 		return
   101 		return
    89 	}
   102 	}
    90 	iface := in.Interface()
   103 	iface := in.Interface()
    91 	switch m := iface.(type) {
   104 	switch m := iface.(type) {
       
   105 	case jsonNumber:
       
   106 		integer, err := m.Int64()
       
   107 		if err == nil {
       
   108 			// In this case the json.Number is a valid int64
       
   109 			in = reflect.ValueOf(integer)
       
   110 			break
       
   111 		}
       
   112 		float, err := m.Float64()
       
   113 		if err == nil {
       
   114 			// In this case the json.Number is a valid float64
       
   115 			in = reflect.ValueOf(float)
       
   116 			break
       
   117 		}
       
   118 		// fallback case - no number could be obtained
       
   119 		in = reflect.ValueOf(m.String())
    92 	case time.Time, *time.Time:
   120 	case time.Time, *time.Time:
    93 		// Although time.Time implements TextMarshaler,
   121 		// Although time.Time implements TextMarshaler,
    94 		// we don't want to treat it as a string for YAML
   122 		// we don't want to treat it as a string for YAML
    95 		// purposes because YAML has special support for
   123 		// purposes because YAML has special support for
    96 		// timestamps.
   124 		// timestamps.