@@ -32,6 +32,14 @@ protocol ProtobufWrapper {
3232 init ( _ value: WrappedType . BaseType )
3333}
3434
35+ extension ProtobufWrapper {
36+ mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
37+ var v : WrappedType . BaseType ?
38+ try WrappedType . decodeSingular ( value: & v, from: & decoder)
39+ value = v ?? WrappedType . proto3DefaultValue
40+ }
41+ }
42+
3543extension Google_Protobuf_DoubleValue :
3644 ProtobufWrapper , ExpressibleByFloatLiteral , _CustomJSONCodable {
3745
@@ -48,15 +56,16 @@ extension Google_Protobuf_DoubleValue:
4856 }
4957
5058 func encodedJSONString( options: JSONEncodingOptions ) throws -> String {
51- var encoder = JSONEncoder ( )
52- encoder. putDoubleValue ( value: value)
53- return encoder. stringResult
54- }
55-
56- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
57- var v : WrappedType . BaseType ?
58- try WrappedType . decodeSingular ( value: & v, from: & decoder)
59- value = v ?? WrappedType . proto3DefaultValue
59+ if value. isFinite {
60+ // Swift 4.2 and later guarantees that this is accurate
61+ // enough to parse back to the exact value on the other end.
62+ return value. description
63+ } else {
64+ // Protobuf-specific handling of NaN and infinities
65+ var encoder = JSONEncoder ( )
66+ encoder. putDoubleValue ( value: value)
67+ return encoder. stringResult
68+ }
6069 }
6170}
6271
@@ -76,15 +85,16 @@ extension Google_Protobuf_FloatValue:
7685 }
7786
7887 func encodedJSONString( options: JSONEncodingOptions ) throws -> String {
79- var encoder = JSONEncoder ( )
80- encoder. putFloatValue ( value: value)
81- return encoder. stringResult
82- }
83-
84- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
85- var v : WrappedType . BaseType ?
86- try WrappedType . decodeSingular ( value: & v, from: & decoder)
87- value = v ?? WrappedType . proto3DefaultValue
88+ if value. isFinite {
89+ // Swift 4.2 and later guarantees that this is accurate
90+ // enough to parse back to the exact value on the other end.
91+ return value. description
92+ } else {
93+ // Protobuf-specific handling of NaN and infinities
94+ var encoder = JSONEncoder ( )
95+ encoder. putFloatValue ( value: value)
96+ return encoder. stringResult
97+ }
8898 }
8999}
90100
@@ -104,15 +114,7 @@ extension Google_Protobuf_Int64Value:
104114 }
105115
106116 func encodedJSONString( options: JSONEncodingOptions ) throws -> String {
107- var encoder = JSONEncoder ( )
108- encoder. putInt64 ( value: value)
109- return encoder. stringResult
110- }
111-
112- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
113- var v : WrappedType . BaseType ?
114- try WrappedType . decodeSingular ( value: & v, from: & decoder)
115- value = v ?? WrappedType . proto3DefaultValue
117+ return " \" " + String( value) + " \" "
116118 }
117119}
118120
@@ -132,15 +134,7 @@ extension Google_Protobuf_UInt64Value:
132134 }
133135
134136 func encodedJSONString( options: JSONEncodingOptions ) throws -> String {
135- var encoder = JSONEncoder ( )
136- encoder. putUInt64 ( value: value)
137- return encoder. stringResult
138- }
139-
140- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
141- var v : WrappedType . BaseType ?
142- try WrappedType . decodeSingular ( value: & v, from: & decoder)
143- value = v ?? WrappedType . proto3DefaultValue
137+ return " \" " + String( value) + " \" "
144138 }
145139}
146140
@@ -162,12 +156,6 @@ extension Google_Protobuf_Int32Value:
162156 func encodedJSONString( options: JSONEncodingOptions ) throws -> String {
163157 return String ( value)
164158 }
165-
166- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
167- var v : WrappedType . BaseType ?
168- try WrappedType . decodeSingular ( value: & v, from: & decoder)
169- value = v ?? WrappedType . proto3DefaultValue
170- }
171159}
172160
173161extension Google_Protobuf_UInt32Value :
@@ -188,12 +176,6 @@ extension Google_Protobuf_UInt32Value:
188176 func encodedJSONString( options: JSONEncodingOptions ) throws -> String {
189177 return String ( value)
190178 }
191-
192- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
193- var v : WrappedType . BaseType ?
194- try WrappedType . decodeSingular ( value: & v, from: & decoder)
195- value = v ?? WrappedType . proto3DefaultValue
196- }
197179}
198180
199181extension Google_Protobuf_BoolValue :
@@ -214,12 +196,6 @@ extension Google_Protobuf_BoolValue:
214196 func encodedJSONString( options: JSONEncodingOptions ) throws -> String {
215197 return value ? " true " : " false "
216198 }
217-
218- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
219- var v : WrappedType . BaseType ?
220- try WrappedType . decodeSingular ( value: & v, from: & decoder)
221- value = v ?? WrappedType . proto3DefaultValue
222- }
223199}
224200
225201extension Google_Protobuf_StringValue :
@@ -252,12 +228,6 @@ extension Google_Protobuf_StringValue:
252228 encoder. putStringValue ( value: value)
253229 return encoder. stringResult
254230 }
255-
256- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
257- var v : WrappedType . BaseType ?
258- try WrappedType . decodeSingular ( value: & v, from: & decoder)
259- value = v ?? WrappedType . proto3DefaultValue
260- }
261231}
262232
263233extension Google_Protobuf_BytesValue : ProtobufWrapper , _CustomJSONCodable {
@@ -274,10 +244,4 @@ extension Google_Protobuf_BytesValue: ProtobufWrapper, _CustomJSONCodable {
274244 encoder. putBytesValue ( value: value)
275245 return encoder. stringResult
276246 }
277-
278- mutating func decodeJSON( from decoder: inout JSONDecoder ) throws {
279- var v : WrappedType . BaseType ?
280- try WrappedType . decodeSingular ( value: & v, from: & decoder)
281- value = v ?? WrappedType . proto3DefaultValue
282- }
283247}
0 commit comments