ID:2049269
 
Resolved
json_decode() was subject to reference count errors when decoding object literals.
BYOND Version:510
Operating System:Windows 10 Pro 64-bit
Web Browser:Firefox 44.0
Applies to:Dream Daemon
Status: Resolved (510.1329)

This issue has been resolved.
Descriptive Problem Summary:
json_decode.. is very wrong at decoding JSON.

Code Snippet (if applicable) to Reproduce Problem:
/client/verb/testDecode()
var/decoded = json_decode("{\"data\":{\"number\":\"10\"},\"status\":\"ok\"}")
for(var/k in decoded)
world << "[k] = [decoded[k]] (\ref[decoded[k]])"
if( istype( decoded[k], /list ) )
world << "{"
for(var/k2 in decoded[k])
world << "\t[k2] = [decoded[k][k2]] (\ref[decoded[k][k2]])"
world << "}"


Expected Results:
data = /list ([0xf000002])
{
number = 10 ([0x6b200000])
}
status = ok ([0x6000158])

Actual Results:
ok = /list ([0xf000003])
{

}
status = ok ([0x6000239])

I've also gotten the equivalent of list("data" = list("ok" = 10), status = "ok") as a result
Does the problem occur:
Every time
When does the problem NOT occur?
Never?

Workarounds:
Use a DM library
I don't know what convoluted setup Lummox has, but if you add a json_encode() statement before your codes execution, it works as expected.

/client/verb/testDecode()
json_encode(list("data" = list("number"=10), "status"="ok"))
var/decoded = json_decode("{\"data\":{\"number\":\"10\"},\"status\":\"ok\"}")
for(var/k in decoded)
world << "[k] = [decoded[k]] (\ref[decoded[k]])"
if( istype( decoded[k], /list ) )
world << "{"
for(var/k2 in decoded[k])
world << "\t[k2] = [decoded[k][k2]] (\ref[decoded[k][k2]])"
world << "}"
I believe I have also encountered this bug in a similar form

Test case:
#define DEBUG
world {
fps = 11;
icon_size = 32;
view = 6;
}
var/mob/M;
mob {
verb/loadndumpfile() {
set name="Load json file";
set category="json";
world << "starting json load";
var/raw = file2text("brain_damage_lines.json")
var/values = json_decode(raw)
world << json_encode(values)
}

}


json file in question:
https://file.house/hqtY.json

When you compile this test case and run it you get a resulting json_dump to world that doesn't even look remotely close to what went in

and in the byond log
Welcome BYOND! (5.0 Beta Version 510.1327)
The BYOND hub reports that port 45000 is reachable.
BUG: Bad ref (6:534) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:534) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:534) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:534) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:534) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:534) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:531) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:478) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:478) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:478) in DecRefCount(DM world.dm:16)
BUG: Bad ref (6:535) in DecRefCount(DM world.dm:16)


Apologies if this is not caused by the same issue
Lummox JR resolved issue with message:
json_decode() was subject to reference count errors when decoding object literals.
there is a good chance this solves ID:2033406 i'll let you know after 1329 comes out and we try that
In response to MrStonedOne
MrStonedOne wrote:
there is a good chance this solves ID:2033406 i'll let you know after 1329 comes out and we try that

I suspect it doesn't. The kinds of objects that your crash happened with don't get created by a json_decode() call.