tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.16k stars 1.03k forks source link

keep the original order mapping and array #1122

Closed kjoe07 closed 2 years ago

kjoe07 commented 3 years ago

Your JSON dictionary:

 {
      "key": "producto",
      "type": "dropdown",
      "name": "Producto",
      "required": true,
      "options": [
        {
          "text": "Aéreo",
          "value": "aereo"
        },
        {
          "text": "Hotel",
          "value": "hotel"
        },
        {
          "text": "Paquete",
          "value": "paquete"
        },
        {
          "text": "Auto",
          "value": "auto"
        },
        {
          "text": "Crucero",
          "value": "crucero"
        },
        {
          "text": "Otro",
          "value": "otro"
        }
      ]
    }

Your model:

struct ProspectAdditionalField: Mappable {

    // MARK: Props
    var key: String?
    var type: AdditionalFieldType?
    var name: String?
    var hint: String?
    var required: Bool?
    var validationRegex: String?
    var validationError: String?
    var validationCustom: String?
    var value: String?
    var minValue: String?
    var maxValue: String?
    var isForQuote: Bool?
    var prospectField: String?
    var options: [Option]?

    // MARK: Constructors
    init() {
    }

    init(key: String) {
        self.key = key
    }

    init(key: String, required: Bool) {
        self.key = key
        self.required = required
    }

    init(type: AdditionalFieldType?, value: String?) {
        self.type = type
        self.value = value
        self.required = false
        self.isForQuote = false
    }

    init?(map: Map) {
    }

    // MARK: Mapping
    mutating func mapping(map: Map) {
        key <- map["key"]
        type <- map["type"]
        name <- map["name"]
        hint <- map["hint"]
        required <- map["required"]
        validationRegex <- map["validationRegex"]
        validationError <- map["validationError"]
        validationCustom <- map["validationCustom"]
        value <- map["value"]
        minValue <- map["minValue"]
        maxValue <- map["maxValue"]
        isForQuote <- map["isForQuote"]
        prospectField <- map["prospectField"]
        options <- map["options"]
    }

    func getValueFromOptions(key: String) -> String?{
        guard let options = options else {
            return nil
        }
        for option in options {
            if option.value == key {
                return option.text
            }
        }
        return nil
    }
}
struct Option: Mappable {

    var text: String?
    var value: String?

    // MARK: Constructors
    init() {
    }

    init?(map: Map) {
    }

    // MARK: Mapping
    mutating func mapping(map: Map) {
        text <- map["text"]
        value <- map["value"]
    }
}

What you did:

decode the json where this json is part of a very long json file that is decoded correct all values but the array does not keep the original order of the json.

let repo = Mapper<Repo>().map(myJSONDictionary)

use the values in options to show a alert with multiples actions like this

for option in field.options! {
        let action = UIAlertAction(title: option.text, style: .default) { (action) in
            row.value = option.text
            row.valueKey = option.value
        }

        alertController.addAction(action)
  }

What you expected:

show actions in the order of the json.

 "crucero","otro","aereo","paquete","hotel","auto"
![Sin título](https://user-images.githubusercontent.com/3653060/131754783-a988ade1-9853-4a51-9799-505366181a16.jpg)

What you got:

  "aereo","hotel","paquete","auto","crucero","otro"