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

35 lines
673 B
Go
Raw Normal View History

2015-11-13 19:11:28 +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) {
2016-06-07 15:28:01 +00:00
if len(u) != 16 {
2015-11-13 19:11:28 +00:00
return []byte(`""`), nil
}
2016-06-07 15:28:01 +00:00
var js [38]byte
js[0] = '"'
encodeHex(js[1:], u)
js[37] = '"'
return js[:], nil
2015-11-13 19:11:28 +00:00
}
func (u *UUID) UnmarshalJSON(data []byte) error {
2016-06-07 15:28:01 +00:00
if string(data) == `""` {
2015-11-13 19:11:28 +00:00
return nil
}
2016-06-07 15:28:01 +00:00
if data[0] != '"' {
2015-11-13 19:11:28 +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
}