-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson.go
55 lines (47 loc) · 1.24 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
// Copyright 2018-2023 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package semver
import (
"encoding/json"
)
// MarshalJSON implements json.Marshaler
func (v *Version) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
// UnmarshalJSON implements json.Unmarshaler
func (v *Version) UnmarshalJSON(data []byte) error {
var versionString string
if err := json.Unmarshal(data, &versionString); err != nil {
return err
}
parsed, err := Parse(versionString)
if err != nil {
return err
}
v.raw = parsed.raw
v.bytes = []byte(v.raw)
v.major = parsed.major
v.minor = parsed.minor
v.patch = parsed.patch
v.prerelease = parsed.prerelease
v.build = parsed.build
return nil
}
// MarshalJSON implements json.Marshaler
func (v *RelaxedVersion) MarshalJSON() ([]byte, error) {
return json.Marshal(v.String())
}
// UnmarshalJSON implements json.Unmarshaler
func (v *RelaxedVersion) UnmarshalJSON(data []byte) error {
var versionString string
if err := json.Unmarshal(data, &versionString); err != nil {
return err
}
parsed := ParseRelaxed(versionString)
v.customversion = parsed.customversion
v.version = parsed.version
return nil
}