tristanhimmelman / ObjectMapper

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

Undefined vs. nil #1127

Open jpmcglone opened 2 years ago

jpmcglone commented 2 years ago

When I write to JSON, I'd sometimes like to write:

"test": null

But other times just omit "test".

I made a simple struct that has undefined, null, or defined(Value) -- but I cannot for the life of me figure out how to make it generate some keys with "null" and omit others.

clearbrian commented 2 years ago

I found this but i tried true/false and it doesnt seem to work

ignoreNil

if my field is null its never output as JSON no matter if ignoreNil is true or false

func mapping(map: Map){
    name <- map["name", ignoreNil: true]
}

see IgnoreNilTests.swift in the repo for example

it is required because I call an API for another dev team in our company and they complain because my request isnt EXACTLY the same as the one they use on the web. So i need to send ALL fields as JSOn event the null ones.