29

With Swift 4's Codable protocol there's a great level of under the hood date and data conversion strategies.

Given the JSON:

{
    "name": "Bob",
    "age": 25,
    "tax_rate": "4.25"
}

I want to coerce it into the following structure

struct ExampleJson: Decodable {
    var name: String
    var age: Int
    var taxRate: Float

    enum CodingKeys: String, CodingKey {
       case name, age 
       case taxRate = "tax_rate"
    }
}

The Date Decoding Strategy can convert a String based date into a Date.

Is there something that does that with a String based Float

Otherwise I've been stuck with using CodingKey to bring in a String and use a computing get:

    enum CodingKeys: String, CodingKey {
       case name, age 
       case sTaxRate = "tax_rate"
    }
    var sTaxRate: String
    var taxRate: Float { return Float(sTaxRate) ?? 0.0 }

This sort of strands me doing more maintenance than it seems should be needed.

Is this the simplest manner or is there something similar to DateDecodingStrategy for other type conversions?

Update: I should note: I've also gone the route of overriding

init(from decoder:Decoder)

But that is in the opposite direction as it forces me to do it all for myself.

pkamb
  • 33,281
  • 23
  • 160
  • 191
Dru Freeman
  • 1,766
  • 3
  • 19
  • 41

8 Answers8

20

Using Swift 5.1, you may choose one of the three following ways in order to solve your problem.


#1. Using Decodable init(from:) initializer

Use this strategy when you need to convert from String to Float for a single struct, enum or class.

import Foundation

struct ExampleJson: Decodable {

    var name: String
    var age: Int
    var taxRate: Float

    enum CodingKeys: String, CodingKey {
        case name, age, taxRate = "tax_rate"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        name = try container.decode(String.self, forKey: CodingKeys.name)
        age = try container.decode(Int.self, forKey: CodingKeys.age)
        let taxRateString = try container.decode(String.self, forKey: CodingKeys.taxRate)
        guard let taxRateFloat = Float(taxRateString) else {
            let context = DecodingError.Context(codingPath: container.codingPath + [CodingKeys.taxRate], debugDescription: "Could not parse json key to a Float object")
            throw DecodingError.dataCorrupted(context)
        }
        taxRate = taxRateFloat
    }

}

Usage:

import Foundation

let jsonString = """
{
  "name": "Bob",
  "age": 25,
  "tax_rate": "4.25"
}
"""

let data = jsonString.data(using: String.Encoding.utf8)!
let decoder = JSONDecoder()
let exampleJson = try! decoder.decode(ExampleJson.self, from: data)
dump(exampleJson)
/*
 prints:
 ▿ __lldb_expr_126.ExampleJson
   - name: "Bob"
   - age: 25
   - taxRate: 4.25
 */

#2. Using an intermediate model

Use this strategy when you have many nested keys in your JSON or when you need to convert many keys (e.g. from String to Float) from your JSON.

import Foundation

fileprivate struct PrivateExampleJson: Decodable {

    var name: String
    var age: Int
    var taxRate: String

    enum CodingKeys: String, CodingKey {
        case name, age, taxRate = "tax_rate"
    }

}

struct ExampleJson: Decodable {

    var name: String
    var age: Int
    var taxRate: Float

    init(from decoder: Decoder) throws {
        let privateExampleJson = try PrivateExampleJson(from: decoder)

        name = privateExampleJson.name
        age = privateExampleJson.age
        guard let convertedTaxRate = Float(privateExampleJson.taxRate) else {
            let context = DecodingError.Context(codingPath: [], debugDescription: "Could not parse json key to a Float object")
            throw DecodingError.dataCorrupted(context)
        }
        taxRate = convertedTaxRate
    }

}

Usage:

import Foundation

let jsonString = """
{
  "name": "Bob",
  "age": 25,
  "tax_rate": "4.25"
}
"""

let data = jsonString.data(using: String.Encoding.utf8)!
let decoder = JSONDecoder()
let exampleJson = try! decoder.decode(ExampleJson.self, from: data)
dump(exampleJson)
/*
 prints:
 ▿ __lldb_expr_126.ExampleJson
   - name: "Bob"
   - age: 25
   - taxRate: 4.25
 */

#3. Using a KeyedDecodingContainer extension method

Use this strategy when converting from some JSON keys' types to your model's property types (e.g. String to Float) is a common pattern in your application.

import Foundation

extension KeyedDecodingContainer  {

    func decode(_ type: Float.Type, forKey key: Key) throws -> Float {
        if let stringValue = try? self.decode(String.self, forKey: key) {
            guard let floatValue = Float(stringValue) else {
                let context = DecodingError.Context(codingPath: codingPath, debugDescription: "Could not parse json key to a Float object")
                throw DecodingError.dataCorrupted(context)
            }
            return floatValue
        } else {
            let doubleValue = try self.decode(Double.self, forKey: key)
            return Float(doubleValue)
        }
    }

}

struct ExampleJson: Decodable {

    var name: String
    var age: Int
    var taxRate: Float

    enum CodingKeys: String, CodingKey {
        case name, age, taxRate = "tax_rate"
    }

}

Usage:

import Foundation

let jsonString = """
{
    "name": "Bob",
    "age": 25,
    "tax_rate": "4.25"
}
"""

let data = jsonString.data(using: String.Encoding.utf8)!
let decoder = JSONDecoder()
let exampleJson = try! decoder.decode(ExampleJson.self, from: data)
dump(exampleJson)
/*
 prints:
 ▿ __lldb_expr_126.ExampleJson
 - name: "Bob"
 - age: 25
 - taxRate: 4.25
 */
Imanou Petit
  • 89,880
  • 29
  • 256
  • 218
  • 1
    The `KeyedDecodingContainer` option is good as long as **all** of your floats are represented as strings. If the JSON includes a float that doesn't have quotes, you'll get a decoding error, because `KeyedDecodingContainer` will be expecting a string. – Tom Harrington Jan 02 '18 at 19:21
  • 1
    @TomHarrington Totally true. I'll update my answer later to fix this issue. Thanks. – Imanou Petit Jan 04 '18 at 13:13
  • The first option worked for me only after taking the enum out of the struct declaration. Thank you! – ScottyBlades Jan 22 '18 at 02:01
  • I have the same problem and it is a bit of a surprise that a fix for this is not yet available in Swift 5. I tried the KeyedDecodingContainer extension method and it works very well for my problem. – Guy Middleton Feb 02 '23 at 15:01
  • #3 was very helpful. 1. `decode` has a sibling `decodeIfPresent` for optional values. 2. After overriding the function, to access its "super" implementation, use `return try superDecoder(forKey: key).singleValueContainer().decode(Bool.self)` – BrianHenryIE Jun 11 '23 at 13:49
18

Unfortunately, I don't believe such an option exists in the current JSONDecoder API. There only exists an option in order to convert exceptional floating-point values to and from a string representation.

Another possible solution to decoding manually is to define a Codable wrapper type for any LosslessStringConvertible that can encode to and decode from its String representation:

struct StringCodableMap<Decoded : LosslessStringConvertible> : Codable {

    var decoded: Decoded

    init(_ decoded: Decoded) {
        self.decoded = decoded
    }

    init(from decoder: Decoder) throws {

        let container = try decoder.singleValueContainer()
        let decodedString = try container.decode(String.self)

        guard let decoded = Decoded(decodedString) else {
            throw DecodingError.dataCorruptedError(
                in: container, debugDescription: """
                The string \(decodedString) is not representable as a \(Decoded.self)
                """
            )
        }

        self.decoded = decoded
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        try container.encode(decoded.description)
    }
}

Then you can just have a property of this type and use the auto-generated Codable conformance:

struct Example : Codable {

    var name: String
    var age: Int
    var taxRate: StringCodableMap<Float>

    private enum CodingKeys: String, CodingKey {
        case name, age
        case taxRate = "tax_rate"
    }
}

Although unfortunately, now you have to talk in terms of taxRate.decoded in order to interact with the Float value.

However you could always define a simple forwarding computed property in order to alleviate this:

struct Example : Codable {

    var name: String
    var age: Int

    private var _taxRate: StringCodableMap<Float>

    var taxRate: Float {
        get { return _taxRate.decoded }
        set { _taxRate.decoded = newValue }
    }

    private enum CodingKeys: String, CodingKey {
        case name, age
        case _taxRate = "tax_rate"
    }
}

Although this still isn't as a slick as it really should be – hopefully a later version of the JSONDecoder API will include more custom decoding options, or else have the ability to express type conversions within the Codable API itself.

However one advantage of creating the wrapper type is that it can also be used in order to make manual decoding and encoding simpler. For example, with manual decoding:

struct Example : Decodable {

    var name: String
    var age: Int
    var taxRate: Float

    private enum CodingKeys: String, CodingKey {
        case name, age
        case taxRate = "tax_rate"
    }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        self.name = try container.decode(String.self, forKey: .name)
        self.age = try container.decode(Int.self, forKey: .age)
        self.taxRate = try container.decode(StringCodableMap<Float>.self,
                                            forKey: .taxRate).decoded
    }
}
Hamish
  • 78,605
  • 19
  • 187
  • 280
  • So does this then become a Swift Proposal? – Dru Freeman Jun 19 '17 at 17:24
  • 3
    @LordAndrei I would recommend raising it on the [swift evolution mailing list](https://lists.swift.org/mailman/listinfo/swift-evolution). My initial feeling is that it would be better to just have it as an extra option for `JSONDecoder`/`JSONEncoder`, rather than as a big overhaul of `Codable`. Given the existing option for decoding and encoding exceptional floating-point values to strings, it seems like a natural place for it to go. – Hamish Jun 19 '17 at 17:46
16

You can always decode manually. So, given:

{
    "name": "Bob",
    "age": 25,
    "tax_rate": "4.25"
}

You can do:

struct Example: Codable {
    let name: String
    let age: Int
    let taxRate: Float

    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        name = try values.decode(String.self, forKey: .name)
        age = try values.decode(Int.self, forKey: .age)
        guard let rate = try Float(values.decode(String.self, forKey: .taxRate)) else {
            throw DecodingError.dataCorrupted(.init(codingPath: [CodingKeys.taxRate], debugDescription: "Expecting string representation of Float"))
        }
        taxRate = rate
    }

    enum CodingKeys: String, CodingKey {
        case name, age
        case taxRate = "tax_rate"
    }
}

See Encode and Decode Manually in Encoding and Decoding Custom Types.

But I agree, that it seems like there should be a more elegant string conversion process equivalent to DateDecodingStrategy given how many JSON sources out there incorrectly return numeric values as strings.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • I appreciate this response. I've edited my original query, that I had gone this route; but that is in the opposite direction of my goal. This is good info to those still learning this new API. – Dru Freeman Jun 16 '17 at 18:28
3

I know that this is a really late answer, but I started working on Codable couple of days back only. And I bumped into a similar issue.

In order to convert the string to floating number, you can write an extension to KeyedDecodingContainer and call the method in the extension from init(from decoder: Decoder){}

For the problem mentioned in this issue, see the extension I wrote below;

extension KeyedDecodingContainer {

    func decodeIfPresent(_ type: Float.Type, forKey key: K, transformFrom: String.Type) throws -> Float? {

        guard let value = try decodeIfPresent(transformFrom, forKey: key) else {
            return nil
        }
        return Float(value)
    }

    func decode(_ type: Float.Type, forKey key: K, transformFrom: String.Type) throws -> Float {

        guard let valueAsString = try? decode(transformFrom, forKey: key),
            let value = Float(valueAsString) else {

            throw DecodingError.typeMismatch(
                type, 
                DecodingError.Context(
                    codingPath: codingPath, 
                    debugDescription: "Decoding of \(type) from \(transformFrom) failed"
                )
            )
        }
        return value
    }
}

You can call this method from init(from decoder: Decoder) method. See an example below;

init(from decoder: Decoder) throws {

    let container = try decoder.container(keyedBy: CodingKeys.self)

    taxRate = try container.decodeIfPresent(Float.self, forKey: .taxRate, transformFrom: String.self)
}

In fact, you can use this approach to convert any type of data to any other type. You can convert string to Date, string to bool, string to float, float to int etc.

Actually to convert a string to Date object, I will prefer this approach over JSONEncoder().dateEncodingStrategy because if you write it properly, you can include different date formats in the same response.

Hope I helped.

Updated the decode method to return non-optional on suggestion from @Neil.

Suran
  • 1,209
  • 1
  • 17
  • 21
  • I found this to be the most elegant solution. However, the `decode()` version should not return an optional. I will post the non-optional version as a new answer. – Neil Nov 18 '19 at 20:41
2

I used Suran's version, but updated it to return non-optional value for decode(). To me this is the most elegant version. Swift 5.2.

extension KeyedDecodingContainer {

func decodeIfPresent(_ type: Float.Type, forKey key: K, transformFrom: String.Type) throws -> Float? {
    guard let value = try decodeIfPresent(transformFrom, forKey: key) else {
        return nil
    }
    return Float(value)
}

func decode(_ type: Float.Type, forKey key: K, transformFrom: String.Type) throws -> Float {
    guard let str = try? decode(transformFrom, forKey: key),
        let value = Float(str) else {
            throw DecodingError.typeMismatch(Int.self, DecodingError.Context(codingPath: codingPath, debugDescription: "Decoding of \(type) from \(transformFrom) failed"))
    }
    return value
}
}
Neil
  • 601
  • 7
  • 16
  • This looks good. How would this work for encoding as well as decoding? And could I create a bunch of typealiases (HexA, HexB, HexC, etc.) tied to String to force different kinds of conversion to Int? I have a question with more details about my use case: https://stackoverflow.com/questions/65314663/using-codable-to-encode-decode-from-strings-to-ints-with-a-function-in-between – Ribena Dec 15 '20 at 22:49
1

You can use lazy var to convert the property to another type:

struct ExampleJson: Decodable {
    var name: String
    var age: Int
    lazy var taxRate: Float = {
        Float(self.tax_rate)!
    }()

    private var tax_rate: String
}

One disadvantage of this approach is that you cannot define a let constant if you want to access taxRate, since the first time you access it, you are mutating the struct.

// Cannot use `let` here
var example = try! JSONDecoder().decode(ExampleJson.self, from: data)
Code Different
  • 90,614
  • 16
  • 144
  • 163
1

The options above only deal with the situation that the given field is always String. Many times I've met APIs where the output was once a string, other times number. So this is my suggestion to solve this. It is up to you to alter this to throw exception or set the decoded value to nil.

var json = """
{
"title": "Apple",
"id": "20"
}
""";
var jsonWithInt = """
{
"title": "Apple",
"id": 20
}
""";

struct DecodableNumberFromStringToo<T: LosslessStringConvertible & Decodable & Numeric>: Decodable {
    var value: T
    init(from decoder: Decoder) {
        print("Decoding")
        if let container = try? decoder.singleValueContainer() {
            if let val = try? container.decode(T.self) {
                value = val
                return
            }

            if let str = try? container.decode(String.self) {
                value = T.init(str) ?? T.zero
                return
            }

        }
        value = T.zero
    }
}


struct MyData: Decodable {
    let title: String
    let _id: DecodableNumberFromStringToo<Int>

    enum CodingKeys: String, CodingKey {
        case title, _id = "id"
    }

    var id: Int {
        return _id.value
    }
}

do {
    let parsedJson = try JSONDecoder().decode(MyData.self, from: json.data(using: .utf8)!)

    print(parsedJson.id)

} catch {
    print(error as? DecodingError)
}


do {
    let parsedJson = try JSONDecoder().decode(MyData.self, from: jsonWithInt.data(using: .utf8)!)

    print(parsedJson.id)

} catch {
    print(error as? DecodingError)
}
Géza Mikló
  • 81
  • 1
  • 6
  • Thank you thank you. This functionality should be built into the decoder (although don't ask me why the server sometimes puts a number in quotes and sometimes not). – David Feb 08 '20 at 04:34
-6

How to used JSONDecodable in Swift 4:

  1. Get the JSON Response and Create Struct
  2. Conform Decodable class in Struct
  3. Other steps in this GitHub project, a simple example
pkamb
  • 33,281
  • 23
  • 160
  • 191
Ananda Aiwale
  • 309
  • 3
  • 3