Hello I found nice solution without array from here works OK
@propertyWrapper
struct EncodablePoint: Encodable {
var wrappedValue: CGPoint
enum CodingKeys: CodingKey {
case x
case y
}
func encode(to encoder: Encoder) throws {
var c = encoder.container(keyedBy: CodingKeys.self)
try c.encode(wrappedValue.x, forKey: .x)
try c.encode(wrappedValue.y, forKey: .y)
}
}
//usage:
struct Foo: Encodable {
@EncodablePoint var pt: CGPoint = CGPoint(x: 123, y: 456)
var other = "zxcv"
}
let foo = Foo()
let encoded = try! JSONEncoder().encode(foo)
print(String(bytes: encoded, encoding: .utf8) ?? "nil") // {"pt":{"x":123,"y":456},"other":"zxcv"}
Please I would like the similar solution with array, If anybody know please help me. I would like somethlig like:
@propertyWrapper
struct EncodablePointsArray: Encodable {
var wrappedValue: [CGPoint]
enum CodingKeys: CodingKey {
?????
}
func encode(to encoder: Encoder) throws {
??????
}
}
struct Foo2: Encodable {
@EncodablePointsArray var pt: [CGPoint] = [CGPoint]
var other = "zxcv"
}
let foo2 = Foo2()
let encoded = try! JSONEncoder().encode(foo2)
print(String(bytes: encoded, encoding: .utf8) ?? "nil")
// I wold like output like : {"ArrayPoints“:[{„x“:1.1,“y“:1.2},{„x“:2.1,“y“:3.4}]},"other":"zxcv"}