codable

Invalid JSON while parsing \n (quoted) string

孤街醉人 提交于 2019-12-02 11:42:15
问题 I have a problem with a Codable parsing... that's my example code: class Test: Codable { let resultCount: Int? let quote: String? } var json = """ { "resultCount" : 42, "quote" : "My real quote" } """.data(using: .utf8)! var decoder = JSONDecoder() let testDecoded = try! decoder.decode(Test.self, from: json) Here everything works as expected and the Test object is created. Now my back end sends me the quote string with a quote in the middle... in this form (please note \"real\" ): class Test:

Implement Codable for an Array of Dictionary with failable responses

为君一笑 提交于 2019-12-02 09:37:56
问题 My JSON response is the following: { "data": [ { "unknown-key-c3e7f0": { "date_time": 1546944854000, "medication": "f4f25ea4-0607-4aac-b85a-edf40cc7d5b6", "record": { "status": "never" } }, "unknown-key-619d40": { "date_time": 1546944854000, "medication": "deef2278-f176-418f-ac34-c65fa54e712c", "record": { "status": "always" } }, "event": "06b445b9-dae0-48a1-85e4-b9f48c9a2349", "created": 1546949155020, "user": "8fb3fcd1-ffe6-4fd9-89b8-d22b1653cb6a", "id": "1546944855000", "type": "compliance

Enum with Raw value, Codable

点点圈 提交于 2019-12-02 07:23:18
The following code doesn't compile: enum Occupation: String { case designer = "Designer" case engineer = "Engineer" } public struct SteveJobs: Codable { let name: String let occupation: Occupation } On the other hand, it should compile since the Occupation is represented as a String which is Codable . Why can't I use enum with raw value in Codable structs? In particular, why automatic conformance isn't working in such a case. Automatic Codable synthesis is “opt-in,” i.e. you have to declare the conformance explicitly: enum Occupation: String, Codable { // <--- HERE case designer = "Designer"

Swift 4 codable: the keys are Int array in JSON data

橙三吉。 提交于 2019-12-02 06:07:59
{ "0": { "name": "legaldoc.pdf", "cmisid": "yib5C-w92PPtxTBlXl4UJ8oDBthDtAU9mKN5kh2_KrQ" }, "1": { "name": "persdoc.pdf", "cmisid": "dqAnrdNMXGTz1RbOMI37OY6tH9xMdxiTnz6wEl2m-VE" }, "2": { "name": "certdoc.pdf", "cmisid": "6d7DuhldQlnb0JSjXlZb9mMOjxV3E_ID-ynJ0QRPMOA" } } How do I use Swift 4 Codable to parse JSON data like that? the problem is that the keys are Int array. How do I set CodingKeys for this? As mentioned in the comments there is no array. All collection types are dictionaries. You can decode it as Swift dictionary. To get an array map the result to the values of the sorted keys

Convert Swift Encodable class typed as Any to dictionary

妖精的绣舞 提交于 2019-12-02 04:51:10
问题 In connection with my previous questions, I decided to subclass NSArrayController in order to achieve the desired behavior. class NSPresetArrayController: NSArrayController { override func addObject(_ object: Any) { if let preset = object as? Preset { super.addObject(["name": preset.name, "value": preset.value]) } else { super.addObject(object) } } } This works, but what if I wanted something that works for any Encodable class, and not just one with two properties called name and value ?

Implement Codable for an Array of Dictionary with failable responses

一曲冷凌霜 提交于 2019-12-02 04:25:47
My JSON response is the following: { "data": [ { "unknown-key-c3e7f0": { "date_time": 1546944854000, "medication": "f4f25ea4-0607-4aac-b85a-edf40cc7d5b6", "record": { "status": "never" } }, "unknown-key-619d40": { "date_time": 1546944854000, "medication": "deef2278-f176-418f-ac34-c65fa54e712c", "record": { "status": "always" } }, "event": "06b445b9-dae0-48a1-85e4-b9f48c9a2349", "created": 1546949155020, "user": "8fb3fcd1-ffe6-4fd9-89b8-d22b1653cb6a", "id": "1546944855000", "type": "compliance" }, { "unknown-key-619d40": { "date_time": 1546944975000, "medication": "deef2278-f176-418f-ac34

Swift 4 - Cannot invoke 'encode' with an argument list of type '(Codable)'

佐手、 提交于 2019-12-02 03:22:37
问题 I have built a set of API functions which encode an object (using a Struct which conforms to Codable ), then Posts the resulting JSON Data object to a server, then decodes the JSON response. All works fine - especially happy with the new method for JSON parsing in Swift 4.2. However, now I want to refactor the code so that I can reuse the code for various method calls - when I do I get a really annoying error. func encodeRequestJSON(apiRequestObject: Codable) -> Data { do { let encoder =

Protocol extending Encodable (or Codable) does not conform to it

天大地大妈咪最大 提交于 2019-12-02 01:09:48
问题 I have 2 protocols, Filters and Parameters , both of which extend Encodable protocol Filters: Encodable { var page: Int { get } } protocol Parameters: Encodable { var type: String { get } var filters: Filters { get } } I create structs conforming to these protocols, thusly… struct BankAccountFilters: Filters { var page: Int var isWithdrawal: Bool } struct BankAccountParamters: Parameters { let type: String = "Bank" var filters: Filters } let baf = BankAccountFilters(page: 1, isWithdrawal:

Swift 4 - Cannot invoke 'encode' with an argument list of type '(Codable)'

杀马特。学长 韩版系。学妹 提交于 2019-12-02 01:06:41
I have built a set of API functions which encode an object (using a Struct which conforms to Codable ), then Posts the resulting JSON Data object to a server, then decodes the JSON response. All works fine - especially happy with the new method for JSON parsing in Swift 4.2. However, now I want to refactor the code so that I can reuse the code for various method calls - when I do I get a really annoying error. func encodeRequestJSON(apiRequestObject: Codable) -> Data { do { let encoder = JSONEncoder() let jsonData = try encoder.encode(apiRequestObject) let jsonString = String(data: jsonData,

How to extend float3 or any other built-in type to conform to the Codable protocol?

北战南征 提交于 2019-12-01 21:14:01
问题 In trying to serialize an array of float3 objects with the basic JSONEncoder, it's revealed that float3 does not conform to the Codable protocol, so this cannot be done. I tried to write a basic extension as suggested in Encoding and Decoding Custom Types as seen below, but the error 'self' used before all stored properties are initialized is rendered for each of the assignment lines in the init. I assume this is because the compiler is not sure that Float.self is defined prior to the