Objectmapper: Null values are imported as int value 0

Created on 6 Jul 2017  路  1Comment  路  Source: tristanhimmelman/ObjectMapper

This behavior is a regression in 2.2.5. Works as expected in 2.2.4

Your JSON dictionary:

{
  "num": null,
}

Your model:

struct Model: Mappable {
  var num: Int = -1

  init(_ map: Map) {
    num <- map["num"]
  }
}

What you did:

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

What you expected:

I exepected something like:

Repo(num: -1)

What you got:

Repo(num: 0)

Most helpful comment

It's hard for me to understand if this is intended behavior or not. However, as a workaround you can do:

num <- map["num", ignoreNil: true]

>All comments

It's hard for me to understand if this is intended behavior or not. However, as a workaround you can do:

num <- map["num", ignoreNil: true]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AashishSapkota picture AashishSapkota  路  3Comments

VictorAlbertos picture VictorAlbertos  路  3Comments

mirzadelic picture mirzadelic  路  4Comments

YunyueLin picture YunyueLin  路  3Comments

nearspears picture nearspears  路  4Comments