1 // Copyright 2013 Google Inc. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
13 // Int64s is a slice of int64s that marshal as quoted strings in JSON.
16 func (q *Int64s) UnmarshalJSON(raw []byte) error {
19 if err := json.Unmarshal(raw, &ss); err != nil {
22 for _, s := range ss {
23 v, err := strconv.ParseInt(s, 10, 64)
27 *q = append(*q, int64(v))
32 // Int32s is a slice of int32s that marshal as quoted strings in JSON.
35 func (q *Int32s) UnmarshalJSON(raw []byte) error {
38 if err := json.Unmarshal(raw, &ss); err != nil {
41 for _, s := range ss {
42 v, err := strconv.ParseInt(s, 10, 32)
46 *q = append(*q, int32(v))
51 // Uint64s is a slice of uint64s that marshal as quoted strings in JSON.
54 func (q *Uint64s) UnmarshalJSON(raw []byte) error {
57 if err := json.Unmarshal(raw, &ss); err != nil {
60 for _, s := range ss {
61 v, err := strconv.ParseUint(s, 10, 64)
65 *q = append(*q, uint64(v))
70 // Uint32s is a slice of uint32s that marshal as quoted strings in JSON.
73 func (q *Uint32s) UnmarshalJSON(raw []byte) error {
76 if err := json.Unmarshal(raw, &ss); err != nil {
79 for _, s := range ss {
80 v, err := strconv.ParseUint(s, 10, 32)
84 *q = append(*q, uint32(v))
89 // Float64s is a slice of float64s that marshal as quoted strings in JSON.
90 type Float64s []float64
92 func (q *Float64s) UnmarshalJSON(raw []byte) error {
95 if err := json.Unmarshal(raw, &ss); err != nil {
98 for _, s := range ss {
99 v, err := strconv.ParseFloat(s, 64)
103 *q = append(*q, float64(v))
108 func quotedList(n int, fn func(dst []byte, i int) []byte) ([]byte, error) {
109 dst := make([]byte, 0, 2+n*10) // somewhat arbitrary
110 dst = append(dst, '[')
111 for i := 0; i < n; i++ {
113 dst = append(dst, ',')
115 dst = append(dst, '"')
117 dst = append(dst, '"')
119 dst = append(dst, ']')
123 func (q Int64s) MarshalJSON() ([]byte, error) {
124 return quotedList(len(q), func(dst []byte, i int) []byte {
125 return strconv.AppendInt(dst, q[i], 10)
129 func (q Int32s) MarshalJSON() ([]byte, error) {
130 return quotedList(len(q), func(dst []byte, i int) []byte {
131 return strconv.AppendInt(dst, int64(q[i]), 10)
135 func (q Uint64s) MarshalJSON() ([]byte, error) {
136 return quotedList(len(q), func(dst []byte, i int) []byte {
137 return strconv.AppendUint(dst, q[i], 10)
141 func (q Uint32s) MarshalJSON() ([]byte, error) {
142 return quotedList(len(q), func(dst []byte, i int) []byte {
143 return strconv.AppendUint(dst, uint64(q[i]), 10)
147 func (q Float64s) MarshalJSON() ([]byte, error) {
148 return quotedList(len(q), func(dst []byte, i int) []byte {
149 return strconv.AppendFloat(dst, q[i], 'g', -1, 64)
153 // RawMessage is a raw encoded JSON value.
154 // It is identical to json.RawMessage, except it does not suffer from
155 // https://golang.org/issue/14493.
156 type RawMessage []byte
158 // MarshalJSON returns m.
159 func (m RawMessage) MarshalJSON() ([]byte, error) {
163 // UnmarshalJSON sets *m to a copy of data.
164 func (m *RawMessage) UnmarshalJSON(data []byte) error {
166 return errors.New("googleapi.RawMessage: UnmarshalJSON on nil pointer")
168 *m = append((*m)[:0], data...)
173 * Helper routines for simplifying the creation of optional fields of basic type.
176 // Bool is a helper routine that allocates a new bool value
177 // to store v and returns a pointer to it.
178 func Bool(v bool) *bool { return &v }
180 // Int32 is a helper routine that allocates a new int32 value
181 // to store v and returns a pointer to it.
182 func Int32(v int32) *int32 { return &v }
184 // Int64 is a helper routine that allocates a new int64 value
185 // to store v and returns a pointer to it.
186 func Int64(v int64) *int64 { return &v }
188 // Float64 is a helper routine that allocates a new float64 value
189 // to store v and returns a pointer to it.
190 func Float64(v float64) *float64 { return &v }
192 // Uint32 is a helper routine that allocates a new uint32 value
193 // to store v and returns a pointer to it.
194 func Uint32(v uint32) *uint32 { return &v }
196 // Uint64 is a helper routine that allocates a new uint64 value
197 // to store v and returns a pointer to it.
198 func Uint64(v uint64) *uint64 { return &v }
200 // String is a helper routine that allocates a new string value
201 // to store v and returns a pointer to it.
202 func String(v string) *string { return &v }