iOS Question Change in JSON handling of null value in v5?

nwhitfield

Active Member
Licensed User
Longtime User
Has there been a tweak in how JSON - particularly null values - are handled?

In the API that my iOS project talks to, a field may be blank, or not present; in the returned data from the server, it's usually present, but may have the value set to null, (not quoted) in the JSON, rather than an empty string. For reasons I haven't dug into too much, if a null value was then posted back via the B4i app, it would be converted to a string value of "null" somewhere along the way.

That was straightforward enough to work around, and I thought I'd fixed everything up, then I recompiled the app with v5, and a bug related to null values reappeared. It seems that now, a null value in JSON will be converted to the string value "<null>"

Am I right that that's changed? Certainly tweaking my code to treat both "null" and "<null>" the same has fixed the problem. I guess it's a small change, but if you're flinging JSON around a lot, it could cause odd behaviour.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I see the cause of this issue. I don't think that it worked differently in previous versions. It does work differently in debug mode (depending on some parameters).

Use this code to check whether a value field is null:
B4X:
Sub IsNull(m As Map, key As String) As Boolean
   Dim value As Object = m.Get("key")
   If value = Null Or GetType(value) = "NSNull" Then Return True
   Return False
End Sub

B4X:
If IsNull(MyMap, "SomeKey") Then ...
 
Upvote 0
Top