Skip to content
This repository was archived by the owner on Oct 1, 2020. It is now read-only.
This repository was archived by the owner on Oct 1, 2020. It is now read-only.

Exception from deep within when decoding a derived class inside an object #3

@jkufver

Description

@jkufver

Hi thanks for the updated version 1.0.9.

I just stumbled upon another issue with Juice.

I have a class B which inherits from a super class A:

class A: Juice.Decodable {
    var a: String?

    required init(fromJson json: Juice.JSONDictionary) throws {
        a = try? json.decode(["a"])
    }
}

class B: A {
    var b: String?

    required init(fromJson json: Juice.JSONDictionary) throws {
        b = try? json.decode(["b"])
        try super.init(fromJson: json)
    }
}

When I decode B (in my case an array of B's (not shown in this reduced example)) I get an exception from deep inside of Juice.

Example code:

        // Decoding simple object B -> Works
        do {
            let string = "{\"a\": \"Apple 0\", \"b\": \"Brother 0\"}"
            let dict = try toStrictlyTypedJSON(toLooselyTypedJSON(string)) as! JSONDictionary
            let o = try B(fromJson: dict)
            print(o.a, o.b)
        } catch {
            print(error.localizedDescription)
        }

        // B as a part of an object -> Fails
        do {
            let string = "{\"object\": {\"a\": \"Apple 1\", \"b\": \"Brother 1\"}}"
            let dict = try toStrictlyTypedJSON(toLooselyTypedJSON(string)) as! JSONDictionary

            // This works
            let oa: A = try dict.decode("object")
            print(oa.a)

            // This fails with exception "Could not cast value of type 'A' to 'B'"
            let ob: B = try dict.decode("object")
            print(ob.a, ob.b)
        } catch {
            print(error.localizedDescription)
        }

My knowledge of generics is a bit to shallow to find the reason.

All the best

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions