-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
88 lines (75 loc) · 1.92 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package timestamp
import (
"strconv"
"time"
jsoniter "github.com/aperturerobotics/json-iterator-lite"
json "github.com/aperturerobotics/protobuf-go-lite/json"
)
// MarshalProtoJSON marshals the Timestamp message to JSON.
func (x *Timestamp) MarshalProtoJSON(s *json.MarshalState) {
if x == nil {
s.WriteNil()
return
}
s.WriteObjectStart()
var wroteField bool
if x.TimeUnixMs != 0 || s.HasField("timeUnixMs") {
s.WriteMoreIf(&wroteField)
s.WriteObjectField("timeUnixMs")
s.WriteUint64(x.TimeUnixMs)
}
s.WriteObjectEnd()
}
// MarshalJSON marshals the Timestamp to JSON.
func (x *Timestamp) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(x.ToRFC3339())), nil
}
// UnmarshalProtoJSON unmarshals the Timestamp message from JSON.
//
// Supports string (unix milliseconds large value or RFC3339 timestamp), number (unix milliseconds)
func (x *Timestamp) UnmarshalProtoJSON(s *json.UnmarshalState) {
if s.ReadNil() {
return
}
nextTok := s.WhatIsNext()
if nextTok == jsoniter.StringValue {
str := s.ReadString()
// try to parse as RFC3339
tt, err := time.Parse(time.RFC3339, str)
if err == nil {
x.TimeUnixMs = ToUnixMs(tt)
return
}
timeMs, err := strconv.ParseUint(str, 10, 64)
if err == nil {
x.TimeUnixMs = timeMs
return
}
// otherwise set error
s.SetError(err)
return
}
if nextTok == jsoniter.NumberValue {
x.TimeUnixMs = s.ReadUint64()
return
}
s.ReadObject(func(key string) {
switch key {
default:
s.Skip() // ignore unknown field
case "time_unix_ms", "timeUnixMs":
s.AddField("time_unix_ms")
// note: this also supports string encoding!
x.TimeUnixMs = s.ReadUint64()
}
})
}
// UnmarshalJSON unmarshals the Timestamp from JSON.
func (x *Timestamp) UnmarshalJSON(b []byte) error {
return json.DefaultUnmarshalerConfig.Unmarshal(b, x)
}
// _ is a type assertion
var (
_ json.Marshaler = ((*Timestamp)(nil))
_ json.Unmarshaler = ((*Timestamp)(nil))
)