Scenario: I have a schemaless database API that infers type based on json passed into it. Once the database infers schema however, all subsequent entries added to the same table must follow the existing schema. I'm pushing json-formatted data to a REST endpoint from my Swift app.
I'm using ObjectMapper to define schema/serialization for data within the Swift app, e.g.:
class ExampleDatum: Mappable {
value: Float?
init() {
}
required init?(map: Map) {
}
func mapping(map: Map) {
value <- map["value"]
}
}
The issue comes that if value is a whole number, json is formatted without the decimal point, leading to inconsistent interpretation of this field as an integer/float:
let datum = ExampleDatum()
datum.value = 5.0
print(datum.toJSONString())
>> {"value": 5}
What's the best practice to embed this type information in JSON, e.g. to enforce the decimal point always be included? Ideally something like {"value": 5.0}