ID:2305098
 
Resolved
Trailing whitespace in JSON caused bad output from json_decode().
BYOND Version:512.1392
Operating System:Windows 7 Ultimate 64-bit
Web Browser:Firefox 56.0
Applies to:Dream Seeker
Status: Resolved (512.1393)

This issue has been resolved.
Descriptive Problem Summary:
Reading from json files with a trailing new line causes an "Unexpected character" runtime.

Numbered Steps to Reproduce Problem:
1. Compile applicable code with the json file in the indicated folder
2. Click the 'test json' verb

Code Snippet (if applicable) to Reproduce Problem:
The first causes no issues wheras the second throws the runtime when trying to read it.
{
"test": "test ok"
}


{
"test": "test ok"
}
//This comment is just whitespace in the actual file


/mob/verb/test_json()
var/json_file = file("data/json/test.json")
var/list/json = json_decode(file2text(json_file))
usr << "[json["test"]]"


Expected Results:
"test ok" Should be visible to the mob using the verb

Actual Results:
Nothing visible and an unexpected character runtime.

Does the problem occur:
Every time?
Yes
In other games?
Yes
In other user accounts?
Yes
On other computers?
Yes

When does the problem NOT occur?

Did the problem NOT occur in any earlier versions? If so, what was the last version that worked?
511 At least

Workarounds:
Remove the trailing newline from json files
Ah, this probably resulted from the earlier updates. Should be a fairly trivial fix. In the meantime, a good workaround prior to calling json_decode() would be this:

json = replacetext(json, regex(@"\n+\Z"), "")
luckily /tg/ already has a few trim() functions we can call upon
Lummox JR resolved issue with message:
Trailing whitespace in JSON caused bad output from json_decode().