Skip to content

Commit 0b5770a

Browse files
committed
Wire JSONEncodingOptions into the encoding api.
- Make it explicit in all the support methods. - Default it at the public api points so it is only needed when wanting custom behaviors.
1 parent 46119ee commit 0b5770a

14 files changed

+83
-50
lines changed

Sources/SwiftProtobuf/AnyMessageStorage.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@ private let i_2166136261 = Int(bitPattern: 2166136261)
2020
private let i_16777619 = Int(16777619)
2121
#endif
2222

23-
fileprivate func serializeAnyJSON(for message: Message, typeURL: String) throws -> String {
24-
var visitor = try JSONEncodingVisitor(message: message)
23+
fileprivate func serializeAnyJSON(
24+
for message: Message,
25+
typeURL: String,
26+
options: JSONEncodingOptions
27+
) throws -> String {
28+
var visitor = try JSONEncodingVisitor(message: message, options: options)
2529
visitor.startObject()
2630
visitor.encodeField(name: "@type", stringValue: typeURL)
2731
if let m = message as? _CustomJSONCodable {
28-
let value = try m.encodedJSONString()
32+
let value = try m.encodedJSONString(options: options)
2933
visitor.encodeField(name: "value", jsonText: value)
3034
} else {
3135
try message.traverse(visitor: &visitor)
@@ -381,7 +385,7 @@ extension AnyMessageStorage {
381385
// * The protobuf field we were deserialized from.
382386
// The last case requires locating the type, deserializing
383387
// into an object, then reserializing back to JSON.
384-
func encodedJSONString() throws -> String {
388+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
385389
switch state {
386390
case .binary(let valueData):
387391
// Transcode by decoding the binary data to a message object
@@ -394,13 +398,13 @@ extension AnyMessageStorage {
394398
throw JSONEncodingError.anyTranscodeFailure
395399
}
396400
let m = try messageType.init(serializedData: valueData, partial: true)
397-
return try serializeAnyJSON(for: m, typeURL: _typeURL)
401+
return try serializeAnyJSON(for: m, typeURL: _typeURL, options: options)
398402

399403
case .message(let msg):
400404
// We should have been initialized with a typeURL, but
401405
// ensure it wasn't cleared.
402406
let url = !_typeURL.isEmpty ? _typeURL : buildTypeURL(forMessage: msg, typePrefix: defaultAnyTypeURLPrefix)
403-
return try serializeAnyJSON(for: msg, typeURL: url)
407+
return try serializeAnyJSON(for: msg, typeURL: url, options: options)
404408

405409
case .contentJSON(let contentJSON, _):
406410
var jsonEncoder = JSONEncoder()
@@ -409,6 +413,8 @@ extension AnyMessageStorage {
409413
jsonEncoder.putStringValue(value: _typeURL)
410414
if !contentJSON.isEmpty {
411415
jsonEncoder.append(staticText: ",")
416+
// NOTE: This doesn't really take `options` into account since it is
417+
// just reflecting out what was taken in originally.
412418
jsonEncoder.append(utf8Data: contentJSON)
413419
}
414420
jsonEncoder.endObject()

Sources/SwiftProtobuf/CustomJSONCodable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/// Allows WKTs to provide their custom JSON encodings.
1616
internal protocol _CustomJSONCodable {
17-
func encodedJSONString() throws -> String
17+
func encodedJSONString(options: JSONEncodingOptions) throws -> String
1818
mutating func decodeJSON(from: inout JSONDecoder) throws
1919

2020
/// Called when the JSON `null` literal is encountered in a position where

Sources/SwiftProtobuf/Google_Protobuf_Any+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ extension Google_Protobuf_Any: _CustomJSONCodable {
131131
}
132132
}
133133

134-
internal func encodedJSONString() throws -> String {
135-
return try _storage.encodedJSONString()
134+
internal func encodedJSONString(options: JSONEncodingOptions) throws -> String {
135+
return try _storage.encodedJSONString(options: options)
136136
}
137137

138138
internal mutating func decodeJSON(from decoder: inout JSONDecoder) throws {

Sources/SwiftProtobuf/Google_Protobuf_Duration+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ extension Google_Protobuf_Duration: _CustomJSONCodable {
133133
let s = try decoder.scanner.nextQuotedString()
134134
(seconds, nanos) = try parseDuration(text: s)
135135
}
136-
func encodedJSONString() throws -> String {
136+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
137137
if let formatted = formatDuration(seconds: seconds, nanos: nanos) {
138138
return "\"\(formatted)\""
139139
} else {

Sources/SwiftProtobuf/Google_Protobuf_FieldMask+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ extension Google_Protobuf_FieldMask: _CustomJSONCodable {
159159
}
160160
}
161161

162-
func encodedJSONString() throws -> String {
162+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
163163
// Note: Proto requires alphanumeric field names, so there
164164
// cannot be a ',' or '"' character to mess up this formatting.
165165
var jsonPaths = [String]()

Sources/SwiftProtobuf/Google_Protobuf_ListValue+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ extension Google_Protobuf_ListValue: ExpressibleByArrayLiteral {
2626
}
2727

2828
extension Google_Protobuf_ListValue: _CustomJSONCodable {
29-
internal func encodedJSONString() throws -> String {
29+
internal func encodedJSONString(options: JSONEncodingOptions) throws -> String {
3030
var jsonEncoder = JSONEncoder()
3131
jsonEncoder.append(text: "[")
3232
var separator: StaticString = ""
3333
for v in values {
3434
jsonEncoder.append(staticText: separator)
35-
try v.serializeJSONValue(to: &jsonEncoder)
35+
try v.serializeJSONValue(to: &jsonEncoder, options: options)
3636
separator = ","
3737
}
3838
jsonEncoder.append(text: "]")

Sources/SwiftProtobuf/Google_Protobuf_Struct+Extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ extension Google_Protobuf_Struct: ExpressibleByDictionaryLiteral {
2828
}
2929

3030
extension Google_Protobuf_Struct: _CustomJSONCodable {
31-
internal func encodedJSONString() throws -> String {
31+
internal func encodedJSONString(options: JSONEncodingOptions) throws -> String {
3232
var jsonEncoder = JSONEncoder()
3333
jsonEncoder.startObject()
34-
var mapVisitor = JSONMapEncodingVisitor(encoder: jsonEncoder)
34+
var mapVisitor = JSONMapEncodingVisitor(encoder: jsonEncoder, options: options)
3535
for (k,v) in fields {
3636
try mapVisitor.visitSingularStringField(value: k, fieldNumber: 1)
3737
try mapVisitor.visitSingularMessageField(value: v, fieldNumber: 2)

Sources/SwiftProtobuf/Google_Protobuf_Timestamp+Extensions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ extension Google_Protobuf_Timestamp: _CustomJSONCodable {
225225
(seconds, nanos) = try parseTimestamp(s: s)
226226
}
227227

228-
func encodedJSONString() throws -> String {
228+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
229229
if let formatted = formatTimestamp(seconds: seconds, nanos: nanos) {
230230
return "\"\(formatted)\""
231231
} else {

Sources/SwiftProtobuf/Google_Protobuf_Value+Extensions.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ extension Google_Protobuf_Value: ExpressibleByNilLiteral {
6969
}
7070

7171
extension Google_Protobuf_Value: _CustomJSONCodable {
72-
internal func encodedJSONString() throws -> String {
72+
internal func encodedJSONString(options: JSONEncodingOptions) throws -> String {
7373
var jsonEncoder = JSONEncoder()
74-
try serializeJSONValue(to: &jsonEncoder)
74+
try serializeJSONValue(to: &jsonEncoder, options: options)
7575
return jsonEncoder.stringResult
7676
}
7777

@@ -146,14 +146,17 @@ extension Google_Protobuf_Value {
146146
}
147147

148148
/// Writes out the JSON representation of the value to the given encoder.
149-
internal func serializeJSONValue(to encoder: inout JSONEncoder) throws {
149+
internal func serializeJSONValue(
150+
to encoder: inout JSONEncoder,
151+
options: JSONEncodingOptions
152+
) throws {
150153
switch kind {
151154
case .nullValue?: encoder.putNullValue()
152155
case .numberValue(let v)?: encoder.putDoubleValue(value: v)
153156
case .stringValue(let v)?: encoder.putStringValue(value: v)
154157
case .boolValue(let v)?: encoder.putBoolValue(value: v)
155-
case .structValue(let v)?: encoder.append(text: try v.jsonString())
156-
case .listValue(let v)?: encoder.append(text: try v.jsonString())
158+
case .structValue(let v)?: encoder.append(text: try v.jsonString(options: options))
159+
case .listValue(let v)?: encoder.append(text: try v.jsonString(options: options))
157160
case nil: throw JSONEncodingError.missingValue
158161
}
159162
}

Sources/SwiftProtobuf/Google_Protobuf_Wrappers+Extensions.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension Google_Protobuf_DoubleValue:
4747
self.init(floatLiteral)
4848
}
4949

50-
func encodedJSONString() throws -> String {
50+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
5151
var encoder = JSONEncoder()
5252
encoder.putDoubleValue(value: value)
5353
return encoder.stringResult
@@ -75,7 +75,7 @@ extension Google_Protobuf_FloatValue:
7575
self.init(floatLiteral)
7676
}
7777

78-
func encodedJSONString() throws -> String {
78+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
7979
var encoder = JSONEncoder()
8080
encoder.putFloatValue(value: value)
8181
return encoder.stringResult
@@ -103,7 +103,7 @@ extension Google_Protobuf_Int64Value:
103103
self.init(integerLiteral)
104104
}
105105

106-
func encodedJSONString() throws -> String {
106+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
107107
var encoder = JSONEncoder()
108108
encoder.putInt64(value: value)
109109
return encoder.stringResult
@@ -131,7 +131,7 @@ extension Google_Protobuf_UInt64Value:
131131
self.init(integerLiteral)
132132
}
133133

134-
func encodedJSONString() throws -> String {
134+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
135135
var encoder = JSONEncoder()
136136
encoder.putUInt64(value: value)
137137
return encoder.stringResult
@@ -159,7 +159,7 @@ extension Google_Protobuf_Int32Value:
159159
self.init(integerLiteral)
160160
}
161161

162-
func encodedJSONString() throws -> String {
162+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
163163
return String(value)
164164
}
165165

@@ -185,7 +185,7 @@ extension Google_Protobuf_UInt32Value:
185185
self.init(integerLiteral)
186186
}
187187

188-
func encodedJSONString() throws -> String {
188+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
189189
return String(value)
190190
}
191191

@@ -211,7 +211,7 @@ extension Google_Protobuf_BoolValue:
211211
self.init(booleanLiteral)
212212
}
213213

214-
func encodedJSONString() throws -> String {
214+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
215215
return value ? "true" : "false"
216216
}
217217

@@ -247,7 +247,7 @@ extension Google_Protobuf_StringValue:
247247
self.init(unicodeScalarLiteral)
248248
}
249249

250-
func encodedJSONString() throws -> String {
250+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
251251
var encoder = JSONEncoder()
252252
encoder.putStringValue(value: value)
253253
return encoder.stringResult
@@ -269,7 +269,7 @@ extension Google_Protobuf_BytesValue: ProtobufWrapper, _CustomJSONCodable {
269269
self.value = value
270270
}
271271

272-
func encodedJSONString() throws -> String {
272+
func encodedJSONString(options: JSONEncodingOptions) throws -> String {
273273
var encoder = JSONEncoder()
274274
encoder.putBytesValue(value: value)
275275
return encoder.stringResult

0 commit comments

Comments
 (0)