4

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"}
gcharita
  • 7,729
  • 3
  • 20
  • 37
Baldo
  • 41
  • 4

3 Answers3

2

You're going off of antiquated information. [CGPoint] is Codable without any extensions now.

struct Foo2: Encodable {
  var pt: [CGPoint] = []
  var other = "zxcv"
}

If you actually need a wrapper, please post the code that requires it. Your question suggests that you don't need a wrapper.

  • Although this is true, it's not what OP asks for. Encoding `Foo2` will have as result this `{"pt":[[1.1,1.2],[2.1,3.4]],"other":"zxcv"}`. Clearly OP asks for something like this `{"pt":[{"x":1.1,"y":1.2},{"x":2.1,"y":3.4}]},"other":"zxcv"}` – gcharita Nov 16 '20 at 18:11
1

Using unkeyedContainer() and nestedContainer(keyedBy:) functions will do the trick:

@propertyWrapper
struct EncodablePointsArray: Encodable {
    var wrappedValue: [CGPoint]
    
    enum CodingKeys: CodingKey {
        case x
        case y
    }
    
    func encode(to encoder: Encoder) throws {
        var unkeyedContainer = encoder.unkeyedContainer()
        try wrappedValue.forEach {
            var container = unkeyedContainer.nestedContainer(keyedBy: CodingKeys.self)
            try container.encode($0.x, forKey: .x)
            try container.encode($0.y, forKey: .y)
        }
    }
}

Using you example:

struct Foo2: Encodable {
    @EncodablePointsArray var pt: [CGPoint] = [CGPoint(x: 1.1, y: 1.2), CGPoint(x: 2.1, y: 3.4)]
    var other = "zxcv"
}

let foo2 = Foo2()
do {
    let encoded = try JSONEncoder().encode(foo2)
    print(String(bytes: encoded, encoding: .utf8) ?? "nil")
} catch {
    print(error)
}

This will have as a result this:

{
    "pt": [
        {
            "x": 1.1000000000000001,
            "y": 1.2
        },
        {
            "x": 2.1000000000000001,
            "y": 3.3999999999999999
        }
    ],
    "other": "zxcv"
}
gcharita
  • 7,729
  • 3
  • 20
  • 37
1

EncodablePoint acts here like a wrapper for CGPoint, so from the client perspective EncodablePoint is a new type with CGPoint property.

If used as a property wrapper, it acts like array of CGPoint. But after encoding it turns it into a custom object with x and y coordinates.

Based on the results you want to achieve after decoding such an object, there are two options here.

Encoding array of EncodablePoint

Let's build property wrapper like this

@propertyWrapper
struct EncodablePointArray: Encodable {
    var wrappedValue: [EncodablePoint]
}

Then client of the wrapper should create instances of EncodablePoint

struct Points: Encodable {
    @EncodablePointArray var pt: [EncodablePoint] = [
        EncodablePoint(wrappedValue: CGPoint(x: 123, y: 456)),
        EncodablePoint(wrappedValue: CGPoint(x: 789, y: 123)),
        EncodablePoint(wrappedValue: CGPoint(x: 456, y: 789))
    ]
    var other = "zxcv"
}

Decoding that will give you results you wanted

{"pt":{"values":[{"x":123,"y":456},{"x":789,"y":123},{"x":456,"y":789}]},"other":"zxcv"}

But I think you want to use array of CGPoint, so...

Encoding array of CGPoint

If you want to use CGPoints directly like this

struct Points: Encodable {
    @EncodablePointArray var pt: [CGPoint] = [
        CGPoint(x: 123, y: 456),
        CGPoint(x: 789, y: 123),
        CGPoint(x: 456, y: 789)
    ]
    var other = "zxcv"
}

You have to build your wrapper with CGPoints

@propertyWrapper
struct EncodablePointArray: Encodable {
    var wrappedValue: [CGPoint]
}

But the decoded String will be different:

{"pt":{"values":[[123,456],[789,123],[456,789]]},"other":"zxcv"}
krlbsk
  • 1,051
  • 1
  • 13
  • 24