Flight-School / AnyCodable

Type-erased wrappers for Encodable, Decodable, and Codable values

Home Page:https://flight.school/books/codable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash on encoding decoded value from nested structuture

DominatorVbN opened this issue · comments

{
    "aps": {
        "alert": {
            "title": "Test Title",
            "subtitle": "Test Subtitle",
            "body": "Test body content"
        },
    "badge": 1,
    "sound": "default",
    "category": "CUSTOM_UNNotificationCategory",
    "interruption-level": "active",
    "relevance-score": 0.0
    }
}

Decoded this successfully using

JSONDecoder().decode(AnyCodable.self, from: payloadData)

Causing error while

JSONEncoder().encode(_payloadEncodable)

Stacktrace:

* thread #20, queue = 'com.apple.root.default-qos.cooperative', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18a773698)
    frame #0: 0x000000018a773698 Foundation`Foundation.__JSONEncoder.encode<τ_0_0 where τ_0_0: Swift.Encodable>(τ_0_0) throws -> () + 292
    frame #1: 0x000000018a773ae8 Foundation`protocol witness for Swift.SingleValueEncodingContainer.encode<τ_0_0 where τ_1_0: Swift.Encodable>(τ_1_0) throws -> () in conformance Foundation.__JSONEncoder : Swift.SingleValueEncodingContainer in Foundation + 24
    frame #2: 0x0000000198f26588 libswiftCore.dylib`dispatch thunk of Swift.SingleValueEncodingContainer.encode<τ_0_0 where τ_1_0: Swift.Encodable>(τ_1_0) throws -> () + 32
  * frame #3: 0x000000010594d3e4 NotifyHub`AnyCodable.encode(encoder=Swift.Encoder @ 0x000000016b261e58, self=Codability.AnyCodable @ 0x000000016b261ae0) at AnyCodable.swift:87:27
    frame #4: 0x000000010594de30 NotifyHub`protocol witness for Encodable.encode(to:) in conformance AnyCodable at <compiler-generated>:0
    frame #5: 0x0000000198f2489c libswiftCore.dylib`dispatch thunk of Swift.Encodable.encode(to: Swift.Encoder) throws -> () + 32
    frame #6: 0x0000000104eaed18

Closing this,
Seems like the issue arise only when the encodable object is passed to swiftapns, which uses https://github.com/swift-server-community/APNSwift/blob/915f4d0e5c4441388a7d95a072bd0b6cfdccb7f4/Sources/APNS/Coding/APNSJSONEncoder.swift#L21

Might be something to do with buffer.writeData(data).

The implementation of
https://github.com/googleapis/google-api-swift-client/blob/a7a7b3c0ea6a55f51f306e3d29df86cb9fe1ff83/Sources/GoogleAPIRuntime/JSONAny.swift#L98

Works for the SwiftNIO based JSONEncoder, might be good idea to incorporate this.