I'm calling an external API that i'm unmarshalling into a struct.
In the response most fields are integer but as it's json there are several edge cases where it could return a string but still be a valid / useful information : "NaN" , "N/A"
My struct is looks like this :
type Example struct {
Field1 *int64 `json:"field_1,omitempty"`
Field2 *int64 `json:"field_2,omitempty"`
Field3 *int64 `json:"field_3,omitempty"`
}
We have several requirement :
If the api returns NaN or N/A I should display an error to my user in the FE so I'm thinking to replace the values with null while "catching" the error beforehand that's why I've chosen a pointer value.
If no value is returned , omit the value altogether when re-marshalling.
In order to do so I'm trying to replace the "NaN" value with JSON null
doing
b = bytes.Replace(b, []byte("NaN"), []byte("null"), -1) `
but it doesn't work as "null"
is not equal to null
and that's problem number 1.
2nd problem is that the omitempty also doesn't distinguish between nil, 0 and empty values when remarshalling.
So the remarshalling also fails. I know it's a "common" problem in go that is being fixed but is there a work around for now?
Because if I pass nil for " N/A " and "NaN" and use omitempty it will remove them. If I pass 0 it won't make sense ( business wise as 0 have meaning other than "not initialized" ) and if I remove Omitempty it will have the whole struct marshalled everytime ( lots of unnecessary data ) and no way to differentiate between nil ( NA / NaN ) and nil ( no value ).
Last option would be to build a custom type and marshall / unmarshaller like this :
type JSONint64 struct {
value *int64
error string
}
but that would require me to check every number in my json response , every time , when in fact NaN and N/A are very rare occurrences and adds " complexity " on the front end.
I'm assuming it's a common problem as JSON is untyped , how is this generally fixed ?