github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/unversioned/duration.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors All rights reserved. 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 unversioned 18 19 import ( 20 "encoding/json" 21 "time" 22 ) 23 24 // Duration is a wrapper around time.Duration which supports correct 25 // marshaling to YAML and JSON. In particular, it marshals into strings, which 26 // can be used as map keys in json. 27 type Duration struct { 28 time.Duration 29 } 30 31 // UnmarshalJSON implements the json.Unmarshaller interface. 32 func (d *Duration) UnmarshalJSON(b []byte) error { 33 var str string 34 json.Unmarshal(b, &str) 35 36 pd, err := time.ParseDuration(str) 37 if err != nil { 38 return err 39 } 40 d.Duration = pd 41 return nil 42 } 43 44 // MarshalJSON implements the json.Marshaler interface. 45 func (d Duration) MarshalJSON() ([]byte, error) { 46 return json.Marshal(d.String()) 47 }