unable to evaluate nil in core.network....

Hi dev,

I’m working with an api where each call return a “next” key containing info about the next page.

When the last page is reached, “next” has the value nil.

When doing this client side using the corona api, I am able to evaluate the “next” key as nil or not.

But when trying this serverside using core.network.request (with josn.decode) or core.network.getJson (without json.decode), this key refuses to evaluate as nil, and yet I cannot find any value for it I can use to catch that last page.

Is there some logic that you have built into the core api that somehow remove nil values and replace them? if so with what?

anaqim

Hi,

I’m not sure I 100% follow what you are describing, but if it’s for JSON comparisons, you could try core.null , which is to check for JSON null values.

-dev

Hi dev,

yeah that worked but how come?

let me show you a simple code example that evaluates differently on clientside vs serverside

local dcode=json.decode(response) if dcode.next then end

normally dcode.next will evaluate to false if the next field in the decoded json table is nil

however on ccore using core.network.request this doesnt work but always evaluates to true

I am curious as to why it is like that?

Hi,

I can’t explain it in totality, but Lua is just an interfacing language generally embedded in a C program or the like.

I don’t know the internal workings of Corona, so I can’t comment on how they handle JSON. As you know, JSON does not have a ‘nil’, but does have a ‘null’.

In ngx_lua (which is written in C), the JSON module is just the raw cJSON shared object, which has nothing to do with Lua. So in that case you must use the core.null, which is what is called ‘lightuserdata’ that does the comparison. Basically checking the ‘null’ as a ‘nil’.

The reason you get a truthy value on the server side is because your condition is only checking for the existence of the ‘next’ key, which will be true as long as it exists in the JSON. Hence the need to use the core.null to test for ‘nil’.

In short, that’s just the way it is. :wink:

-dev

Thanks for the explanation  :smiley:

Hi,

I’m not sure I 100% follow what you are describing, but if it’s for JSON comparisons, you could try core.null , which is to check for JSON null values.

-dev

Hi dev,

yeah that worked but how come?

let me show you a simple code example that evaluates differently on clientside vs serverside

local dcode=json.decode(response) if dcode.next then end

normally dcode.next will evaluate to false if the next field in the decoded json table is nil

however on ccore using core.network.request this doesnt work but always evaluates to true

I am curious as to why it is like that?

Hi,

I can’t explain it in totality, but Lua is just an interfacing language generally embedded in a C program or the like.

I don’t know the internal workings of Corona, so I can’t comment on how they handle JSON. As you know, JSON does not have a ‘nil’, but does have a ‘null’.

In ngx_lua (which is written in C), the JSON module is just the raw cJSON shared object, which has nothing to do with Lua. So in that case you must use the core.null, which is what is called ‘lightuserdata’ that does the comparison. Basically checking the ‘null’ as a ‘nil’.

The reason you get a truthy value on the server side is because your condition is only checking for the existence of the ‘next’ key, which will be true as long as it exists in the JSON. Hence the need to use the core.null to test for ‘nil’.

In short, that’s just the way it is. :wink:

-dev

Thanks for the explanation  :smiley: