Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect nested array decoding #28

Open
MihaelIsaev opened this issue Oct 13, 2018 · 2 comments
Open

Incorrect nested array decoding #28

MihaelIsaev opened this issue Oct 13, 2018 · 2 comments

Comments

@MihaelIsaev
Copy link

I have XML like this

<search-results>
    <page-size type="integer">50</page-size>
    <ids type="array">
        <item>6yrscx</item>
        <item>88kw7x</item>
    </ids>
</search-results>

trying to decode it into this struct

struct Response: Codable {
    let pageSize: Int
    struct Item: Codable {
        let item: String?
        let type: String
    }
    let ids: [Item]
    
    private enum CodingKeys : String, CodingKey {
        case pageSize = "page-size", ids
    }
}

but ids.count is always 1 and contains only last 88kw7x value

in XMLDecoder.swift on line #153 I'm printing print("topLevel: \(topLevel)") and it always showing me ["page-size": "50", "ids": ["item": "88kw7x", "type": "array"]]

@ShawnMoore Is there a way to decode ids correctly using this lib or it's a bug?

@MihaelIsaev
Copy link
Author

Looks like I found a way to parse nested array correctly
now it looks like
topLevel: ["ids": ["type": "array", "item": ["6yrscx", "88kw7x"]], "page-size": "50"]
and finally I can decode it like this

struct Response: Codable {
    let pageSize: Int
    struct Ids: Codable {
        let type: String
        let item: [String]
    }
    let ids: Ids
    
    private enum CodingKeys : String, CodingKey {
        case pageSize = "page-size", ids
    }
}

@michzio
Copy link

michzio commented Sep 23, 2019

Yeas it must be done something like this. I have tried to use just [String] but it does throw parse error. It is a little odd syntax but can be handled.

final public class CustomerInfo {
    
    // Customer Messages
    var customerMessages: ArrayOfString?

}

final public class ArrayOfString  {
    
    // String
    var string: [String]?
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants