Skip to content

Commit 2cfe25d

Browse files
committed
Split enum visiting based on custom json support.
This should only do the check once per repeated enum, which should make the cost much lower on the common case since enums don't normally support this.
1 parent a3d5c02 commit 2cfe25d

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

Sources/SwiftProtobuf/JSONEncodingVisitor.swift

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -280,17 +280,23 @@ internal struct JSONEncodingVisitor: Visitor {
280280
}
281281

282282
mutating func visitRepeatedEnumField<E: Enum>(value: [E], fieldNumber: Int) throws {
283-
let options = self.options
284-
let alwaysPrintEnumsAsInts = options.alwaysPrintEnumsAsInts
285-
try _visitRepeated(value: value, fieldNumber: fieldNumber) {
286-
(encoder: inout JSONEncoder, v: E) throws in
287-
if let e = v as? _CustomJSONCodable {
283+
if let _ = E.self as? _CustomJSONCodable.Type {
284+
let options = self.options
285+
try _visitRepeated(value: value, fieldNumber: fieldNumber) {
286+
(encoder: inout JSONEncoder, v: E) throws in
287+
let e = v as! _CustomJSONCodable
288288
let json = try e.encodedJSONString(options: options)
289289
encoder.append(text: json)
290-
} else if !alwaysPrintEnumsAsInts, let n = v.name {
291-
encoder.appendQuoted(name: n)
292-
} else {
293-
encoder.putEnumInt(value: v.rawValue)
290+
}
291+
} else {
292+
let alwaysPrintEnumsAsInts = options.alwaysPrintEnumsAsInts
293+
try _visitRepeated(value: value, fieldNumber: fieldNumber) {
294+
(encoder: inout JSONEncoder, v: E) throws in
295+
if !alwaysPrintEnumsAsInts, let n = v.name {
296+
encoder.appendQuoted(name: n)
297+
} else {
298+
encoder.putEnumInt(value: v.rawValue)
299+
}
294300
}
295301
}
296302
}

0 commit comments

Comments
 (0)