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

decoding a json with variable keys #195

Open
pdegouville opened this issue Jan 4, 2018 · 0 comments
Open

decoding a json with variable keys #195

pdegouville opened this issue Jan 4, 2018 · 0 comments

Comments

@pdegouville
Copy link

I am trying to decode a JSON like this one with Himotoki in Swift 3

next: [
          {
            question: 'Fréquence',
            condition: { '==': ['answer', 'Actions'] },
          },
          {
            question: 'Fréquence',
            condition: { '>=': ['answer', 'Stop'] },
          },
          {
            question: 'Risque',
            condition: { '<=': ['answer', 'nul'] }
          }
        ]

In the condition, there is an object that looks like a dictionary, since the field name changes ('==', '<=', '>=, ...). The value is then an array of string

I have tried to decode like this

typealias ConditionType = [String]

extension ConditionType: Decodable {}

struct NextItem {
    let question: String?
    let condition: [String : ConditionType]?
}

extension NextItem: Decodable {
    static func decode(_ e: Extractor) throws -> NextItem {

        return try NextItem(
            question:  e <|? "question",
            condition: e <|-|? "condition"
        )
    }
}

I do not not how to decode the ConditionType since there is no field name for the array

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

1 participant