traefik/vendor/github.com/pborman/uuid/json.go

31 lines
694 B
Go
Raw Normal View History

2017-02-07 21:33:23 +00:00
// Copyright 2014 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package uuid
import "errors"
func (u UUID) MarshalJSON() ([]byte, error) {
2017-04-07 09:09:57 +00:00
if len(u) == 0 {
2017-02-07 21:33:23 +00:00
return []byte(`""`), nil
}
2017-04-07 09:09:57 +00:00
return []byte(`"` + u.String() + `"`), nil
2017-02-07 21:33:23 +00:00
}
func (u *UUID) UnmarshalJSON(data []byte) error {
2017-04-07 09:09:57 +00:00
if len(data) == 0 || string(data) == `""` {
2017-02-07 21:33:23 +00:00
return nil
}
2017-04-07 09:09:57 +00:00
if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' {
2017-02-07 21:33:23 +00:00
return errors.New("invalid UUID format")
}
data = data[1 : len(data)-1]
uu := Parse(string(data))
if uu == nil {
return errors.New("invalid UUID format")
}
*u = uu
return nil
}