mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-02 18:38:19 +00:00
Adds JSON encoding to swift (#6874)
Updates generated code & removes unneeded protocol Updates cpp to only generate code when flag is passed Updates code gen script
This commit is contained in:
@@ -16,6 +16,17 @@ public enum ABC: Int32, Enum, Verifiable {
|
||||
public static var min: ABC { return .a }
|
||||
}
|
||||
|
||||
extension ABC: Encodable {
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
switch self {
|
||||
case .a: try container.encode("A")
|
||||
case .b: try container.encode("B")
|
||||
case .c: try container.encode("C")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct MoreDefaults: FlatBufferObject, Verifiable, ObjectAPIPacker {
|
||||
|
||||
static func validateVersion() { FlatBuffersVersion_2_0_0() }
|
||||
@@ -130,6 +141,39 @@ public struct MoreDefaults: FlatBufferObject, Verifiable, ObjectAPIPacker {
|
||||
}
|
||||
}
|
||||
|
||||
extension MoreDefaults: Encodable {
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case ints = "ints"
|
||||
case floats = "floats"
|
||||
case emptyString = "empty_string"
|
||||
case someString = "some_string"
|
||||
case abcs = "abcs"
|
||||
case bools = "bools"
|
||||
}
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
if intsCount > 0 {
|
||||
try container.encodeIfPresent(ints, forKey: .ints)
|
||||
}
|
||||
if floatsCount > 0 {
|
||||
try container.encodeIfPresent(floats, forKey: .floats)
|
||||
}
|
||||
try container.encodeIfPresent(emptyString, forKey: .emptyString)
|
||||
try container.encodeIfPresent(someString, forKey: .someString)
|
||||
if abcsCount > 0 {
|
||||
var contentEncoder = container.nestedUnkeyedContainer(forKey: .abcs)
|
||||
for index in 0..<abcsCount {
|
||||
guard let type = abcs(at: index) else { continue }
|
||||
try contentEncoder.encode(type)
|
||||
}
|
||||
}
|
||||
if boolsCount > 0 {
|
||||
try container.encodeIfPresent(bools, forKey: .bools)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class MoreDefaultsT: NativeObject {
|
||||
|
||||
public var ints: [Int32]
|
||||
|
||||
Reference in New Issue
Block a user