Questions tagged [encodable]

Use this tag only for questions directly pertaining to the Swift standard Encodable protocol

It is the standardized protocol (defined by Swift standard library) for type that can encode itself to an external representation

References

104 questions
3
votes
2 answers

How to deal with empty array to be sent as json in swift?

I'm trying to encode data but struggling to deal with empty array with no type as the API's I am working with needs me to send [] if there is no entry. [ { "RequestId": "5B6E36D9-8759-41BB-A0C0-EDFB116DFBB7", "DataSources":…
nitpaxy
  • 67
  • 10
3
votes
1 answer

Type ' ' does not conform to protocol 'Encodable'

Can someone tell what's wrong here please? It send me this error: Type 'CityWeatherInfo' does not conform to protocol 'Encodable' struct CityWeatherInfo: Codable { var name: String var main: Main var weathers: [Weather] private enum…
3
votes
3 answers

How to Decode selected keys manually and rest with the automatic decoding with swift Decodable?

Here is the code I am using, struct CreatePostResponseModel : Codable{ var transcodeId:String? var id:String = "" enum TopLevelCodingKeys: String, CodingKey { case _transcode = "_transcode" case _transcoder =…
infiniteLoop
  • 2,135
  • 1
  • 25
  • 29
3
votes
1 answer

Codable and CodingKeys

I'm trying to implement a protocol with similar functionality to how Codable uses the CodingKeys enum. With Codable and CodingKeys, if you don't implement a case in the CodingKeys enum for every property on the Codable object, it causes a compiler…
RPK
  • 1,830
  • 14
  • 30
3
votes
2 answers

JSONEncoder's dateEncodingStrategy not working

I am trying to serialize a struct to a String using Swift 4's Encodable+JSONEncoder. The object can hold heterogenous values like String, Array, Date, Int etc. The used approach works fine with the exception of Date. JSONEncoder's…
justintime
  • 352
  • 3
  • 11
3
votes
2 answers

Store Encodables in a Swift Dictionary

I'm looking to store models objects in a Dictionary and would like to serialize the whole dictionary using JSONEncoder into data and subsequently into a string and save it. The idea is to use Swift 4's out of the box Encodable to ensure anything…
justintime
  • 352
  • 3
  • 11
2
votes
1 answer

Encode dictionary without adding the coding key enum in Swift

I want to encode a JSON that could be {"hw1":{"get_trouble":true},"seq":2,"session_id":1} or {"hw2":{"get_trouble":true},"seq":3,"session_id":2} the class for encoding looks like the following class Request: Codable { let sessionId,…
Muhammad Rafay
  • 133
  • 3
  • 7
2
votes
3 answers

Swift Encodable: encode nil as an empty object

How can I encode a nil property as an empty JSON object? struct Foo: Encodable { let id = 10 let bar: Bar? = nil } struct Bar: Encodable { let number: Int } let data = try! JSONEncoder().encode(Foo()) print(String(data: data,…
Silis Alin
  • 140
  • 2
  • 13
2
votes
1 answer

encode class to single value not dictionary swift

Given the classes: class ComplementApp: Codable{ let name: String let idSpring: String } class MasterClass: Encodable{ let complement: ComplementApp ///Other propierties } I want to get: //Where "Some ID" is the value of…
2
votes
0 answers

Why JSONEncoder works differently with Dictionary of enum keys?

When I use JSONEncoder for structure like this: struct A: Encodable { var items: [String : Int] } let a = A(items: ["a": 1, "b": 2, "c": 3]) I get items property as JSON object as expected: {"a": 1, "b": 2, "c": 3}. But when I use enumeration…
John Tracid
  • 3,836
  • 3
  • 22
  • 33
2
votes
1 answer

Why not conform to protocol encodable decodable?

I have the following structure of code. If I omit the codingkey part the code is running. I implemented StringConverter to convert a string to Int (from bySundell Swift Side) struct Video: Codable { var title: String var description: String …
Arnold Schmid
  • 163
  • 12
2
votes
1 answer

Are there any performance benefits of making a struct Encodable/Decodable instead of just Codable?

For an example struct Person that is to be serialized: struct Person { let firstName: String let lastName: String } We could make it conform to the Encodable, Decodable or Codable protocols. I understand that our choice between Encodable…
Swifty
  • 839
  • 2
  • 15
  • 40
2
votes
1 answer

Argument type does not conform to Encodable

I am trying to create a Struct for a POST request. The struct conforms, to the best of my knowledge to the Codable typealias but I keep getting the error Argument type 'RegisterUserRequest.Type' does not conform to expected type 'Encodable' " when…
Sebastian Nitu
  • 117
  • 1
  • 7
2
votes
3 answers

Force all attribute keys and values of a swift class in lowercase

I need to convert all keys and their values of a class in lowercase. For example, class Person : Encodable { var firstName: String var lastName: String var city: String } var person = Person(firstName: "David", lastName: "Gill",…
NChoudhary
  • 23
  • 3
2
votes
1 answer

Decoding an Array in a Swift model (Decodable)

I am retrieving a JSON from an API and I wanted to make a Model for each endpoints I use. All the endpoints use this format: { "id": "xxxxxx", "result": {…}, "error": null } The keys are: id is always a string error can be null or an object…
Anthony
  • 804
  • 3
  • 12
  • 32