Android Question JSONGenerator Issue

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All,

I am just wondering why the 2 cases below are different.

Case #1
B4X:
    Dim ClosedByID As String = Null
    Dim ShiftEnd As String = Null
    
    Dim Map1 As Map = CreateMap(  "ClosedByToken": ClosedByID, _
                                  "ShiftEnd": ShiftEnd )
    

    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Map1)   
    Log( JSONGenerator.ToString )

output
B4X:
{"ClosedByToken":"null","ShiftEnd":"null"}

Case #2:
B4X:
    Dim Map2 As Map = CreateMap(  "ClosedByToken": Null, _
                                  "ShiftEnd": Null )
   
    Dim JSONGenerator2 As JSONGenerator
    JSONGenerator2.Initialize(Map2)   
    Log( JSONGenerator2.ToString )
output

B4X:
{"ClosedByToken":null,"ShiftEnd":null}


I would like to see Case #1 output identical to Case#2

Thanks,
iCAB
 

emexes

Expert
Licensed User
Longtime User
Case #0 might give a clue

B4X:
    Dim ClosedByID As Object = Null
    Dim ShiftEnd As Object = Null.As(String)
 
    Dim Map1 As Map = CreateMap(  "ClosedByToken": ClosedByID, _
                                  "ShiftEnd": ShiftEnd )

    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Map1)
    Log( JSONGenerator.ToString )
 
Last edited:
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Case #0 might give a clue

B4X:
    Dim ClosedByID As Object = Null
    Dim ShiftEnd As Object = Null.As(String)
 
    Dim Map1 As Map = CreateMap(  "ClosedByToken": ClosedByID, _
                                  "ShiftEnd": ShiftEnd )

    Dim JSONGenerator As JSONGenerator
    JSONGenerator.Initialize(Map1)
    Log( JSONGenerator.ToString )
Thanks for your reply, but I am not sure I can use the above code for my actual code.
In the actual code ClosedByID and ShiftEnd are part of a defined type. Something like:

B4X:
Type TRegisterShiftRecord( ClosedByID As String, ShiftEnd As String )
 
Upvote 0

emexes

Expert
Licensed User
Longtime User
ClosedByID and ShiftEnd are part of a defined type.

If that is the case and you are only constructing the Map from those, and not directly like you have in Case #2, then you'll never have actual nulls like you have in Case #2.

You'd do better changing Case #1 to be = "" rather than = Null

When you say String variable = Null what it actually becomes behind the scenes is String variable = Null.As(String)

A String variable can only hold a string (or, at least, should only be able to hold a string... but I do remember an oddball case a few months back where a Null was passed via a function parameter, and it somehow retained its nullness and didn't act like a string - but it shouldn't do that... if a variable is a String, then it should be a String).
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Hey Emexes,

I tried not to post the actual code to make it easier to see the issue. Here is the actual code:

Original Code:
    Dim ClosedByID As String = Null
    Dim ShiftEnd As String = Null   


    If TRegisterShiftRecord1.ClosedByID <> "" Then
        ClosedByID = TRegisterShiftRecord1.ClosedByID
    End If
    
    If TRegisterShiftRecord1.ShiftEnd <> "" Then
        ShiftEnd = TRegisterShiftRecord1.ShiftEnd
    End If
        

    Dim Map1 As Map = CreateMap(  "TerminalToken": TRegisterShiftRecord1.ShiftTerminalID, _
                                  "RegisterShiftID": TRegisterShiftRecord1.ShiftID, _
                                  "BatchNumber": TRegisterShiftRecord1.iBatchNumber, _
                                  "OpenedByToken": TRegisterShiftRecord1.OpenedByID, _
                                  "ShiftStart":  GeneralLib.GL_AIDTF_ConvertToHost_CheckValid( TRegisterShiftRecord1.ShiftStart, True ), _
                                  "ClosedByToken": ClosedByID, _
                                  "ShiftEnd": ShiftEnd, _
                                  "ShiftStatus": TRegisterShiftRecord1.iStatus, _
                                  "TerminalSyncCall": HTTPShared.CHTTP_TRUE, _
                                  "LastUpdatedTicks": TRegisterShiftRecord1.lngLastUpdated )
    
    
    Dim JSONGenerator As JSONGenerator
    Dim JSONstring As String
    
    JSONGenerator.Initialize(Map1)   
    JSONstring = JSONGenerator.ToString

The above produces:
B4X:
ClosedByToken:"null", ShiftEnd:"null"

The workaround is

Workaround:
    Dim Map1 As Map = CreateMap(  "TerminalToken": TRegisterShiftRecord1.ShiftTerminalID, _
                                  "RegisterShiftID": TRegisterShiftRecord1.ShiftID, _
                                  "BatchNumber": TRegisterShiftRecord1.iBatchNumber, _
                                  "OpenedByToken": TRegisterShiftRecord1.OpenedByID, _
                                  "ShiftStart":  GeneralLib.GL_AIDTF_ConvertToHost_CheckValid( TRegisterShiftRecord1.ShiftStart, True ), _
                                  "ClosedByToken": TRegisterShiftRecord1.ClosedByID, _
                                  "ShiftEnd": TRegisterShiftRecord1.ShiftEnd, _
                                  "ShiftStatus": TRegisterShiftRecord1.iStatus, _
                                  "TerminalSyncCall": HTTPShared.CHTTP_TRUE, _
                                  "LastUpdatedTicks": TRegisterShiftRecord1.lngLastUpdated )
    
    

    If TRegisterShiftRecord1.ClosedByID = "" Then
        Map1.Put("ClosedByToken",Null)
    End If
    
    If TRegisterShiftRecord1.ShiftEnd = "" Then
        Map1.Put("ShiftEnd", Null)
    End If

    
    Dim JSONGenerator As JSONGenerator
    Dim JSONstring As String
    
    JSONGenerator.Initialize(Map1)   
    JSONstring = JSONGenerator.ToString

The workaround produces
B4X:
ClosedByToken:null, ShiftEnd:null

Thanks for your help!!!
 
Upvote 0
Top