JSON encoding event from network request

For some reason my program is just hanging when I try to json.encode the response in a callback function from a network.request?!?

Bizarrely it works if I just encode the event.responseHeaders?!?

Does anybody know if this is a known limitation?

[lua]

local data = json.encode(event)
print("ItemManager: getItemData() event - "…data)

[/lua]

I think the reason is that the network request event has a field that is not JSON-encodable – http://docs.coronalabs.com/api/event/networkRequest/index.html – specifically the requestId field, which is a userdata type (essentially a pointer to a memory block for an object being managed in C/Java, not in Lua).

It looks like you’re trying to encode to JSON as a quick way of printing a table to the terminal for inspection.  If that’s what you’re doing, then I’d suggest you instead try using a print_r implementation like http://developer.coronalabs.com/code/printr-implementation-dumps-everything-human-readable-text or any of the implementations at http://lua-users.org/wiki/TableSerialization.

  • Andrew

Thanks Andrew - it was indeed just a quick way of seeing what was coming back and wasn’t causing me any problems, just a little confused by what was happening.

Thanks for solving the quandary for me.

I think the reason is that the network request event has a field that is not JSON-encodable – http://docs.coronalabs.com/api/event/networkRequest/index.html – specifically the requestId field, which is a userdata type (essentially a pointer to a memory block for an object being managed in C/Java, not in Lua).

It looks like you’re trying to encode to JSON as a quick way of printing a table to the terminal for inspection.  If that’s what you’re doing, then I’d suggest you instead try using a print_r implementation like http://developer.coronalabs.com/code/printr-implementation-dumps-everything-human-readable-text or any of the implementations at http://lua-users.org/wiki/TableSerialization.

  • Andrew

Thanks Andrew - it was indeed just a quick way of seeing what was coming back and wasn’t causing me any problems, just a little confused by what was happening.

Thanks for solving the quandary for me.