-1

I am trying auto parsing an API with swift 4 codable. Some of the fields in the API are big integer which Codable does not support nor Swift 4.NSNumber is not supported by Codable and UInt64 is small for it to fit. I tried with a thrid party library and made my variable within the codable to that type,but that also did not work. I tried to to make a custom class or struct which will do the conversion with only one value in container but don't know how to make the container accept Big Int type or convert it to string. My code is below like this. Is there any solution to it?

import Foundation
import BigNumber

class PersonalizationLean:Codable {

    var hubId:String?
    var appId:UInt8?
    var nodeId:Int?
    var name:String?
    var ico:String?
    var icoBase64:String?
    var isBin:Bool?
    var lastModifiedAt:Int?
    var shouldShowInUi:Bool?
    var applianceType:String?
    var tags:[String]?
    var placeId:PlaceIdCodable?
    var roomId:String?
    var id:String?
    var key:String?


    enum CodingKeys:String,CodingKey {
        case hubId
        case appId
        case nodeId
        case name
        case ico
        case icoBase64
        case isBin
        case lastModifiedAt
        case shouldShowInUi
        case applianceType
        case tags
        case placeId
        case roomId
        case id
        case key
    }

//    required init(from decoder: Decoder) throws {
//        do {
//            let container = try decoder.container(keyedBy: CodingKeys.self)
//            self.hubId = try container.decode(String.self, forKey: .hubId)
//            self.appId = try container.decode(UInt8.self, forKey: .appId)
//            self.nodeId = try container.decode(Int.self, forKey: .nodeId)
//            self.name = try container.decode(String.self, forKey: .name)
//            self.ico = try container.decode(String.self, forKey: .ico)
//            self.icoBase64 = try container.decode(String.self, forKey: .icoBase64)
//            self.isBin = try container.decode(Bool.self, forKey: .isBin)
//            self.lastModifiedAt = try container.decode(Int.self, forKey: .lastModifiedAt)
//            self.shouldShowInUi = try container.decode(Bool.self, forKey: .shouldShowInUi)
//            self.applianceType = try container.decode(String.self,forKey: .applianceType)
//            self.tags = try container.decode([String].self,forKey: .tags)
//
//
//        }catch {
//            print(error)
//        }
//    }

}

class PlaceIdCodable:Codable {
    var placeId:String?

    required init(from decoder:Decoder) throws {
        do  {
            let container = try decoder.singleValueContainer()
            let placeIdBig = try container.decode(BInt.self) //this gives error
        }catch {
            print(error)
        }

    }

}

The library I am using is BigNumber

Anuran Barman
  • 1,556
  • 2
  • 16
  • 31

2 Answers2

2

Use built-in Decimal which derives from NSDecimalNumber. It adopts Codable

vadian
  • 274,689
  • 30
  • 353
  • 361
0

BInt do not conform to Codable OR Decodable

to use it here it should confirm to mentioned protocol

extension BInt: Decodable {
    public init(from decoder: Decoder) throws {
        // Initialization goes here
    }
}
Satish
  • 2,015
  • 1
  • 14
  • 22