XMLParsing icon indicating copy to clipboard operation
XMLParsing copied to clipboard

Incorrect nested array decoding

Open MihaelIsaev opened this issue 6 years ago • 2 comments

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 avatar Oct 13 '18 17:10 MihaelIsaev

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
    }
}

MihaelIsaev avatar Oct 14 '18 00:10 MihaelIsaev

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]?
}

michzio avatar Sep 23 '19 09:09 michzio