• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

json_encode and arrays?

samspade

Member
I've been experimenting with JSON and GameMaker for an upcoming tutorial series. In doing so it seems like GM actually has no problem encoding not only arrays, but nested arrays in the JSON format and loading them back in (though when loaded in they are loaded as lists).

Does anyone know if this undocumented behavior official or a fluke of the current version?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Did you read the manual on the encode/decode functions?


Both pages mention that the JSON created or decoded will be based on DS lists and DS maps and that the DS list corresponds to an array.
 

chamaeleon

Member
Did you read the manual on the encode/decode functions?


Both pages mention that the JSON created or decoded will be based on DS lists and DS maps and that the DS list corresponds to an array.
Your answer does not address whether it is documented that a nested array, [[1, 2, 3]], would be necessarily be handled properly by json_encode(). Yes, json_decode() would return nested ds_lists, but is it guaranteed that starting with nested arrays would work properly using json_encode()? For the sake of argument, the json is sent to a server, not intended for json_decode() use later.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Afaik, all JSON created using the encode functions should be valid JSON, regardless of the GameMaker content (assuming the GameMaker content was added correctly, ie: lists marked as lists, maps marked as maps, etc...). Note that the docs can't cover every potential use... so test it and see!
 

samspade

Member
Did you read the manual on the encode/decode functions?


Both pages mention that the JSON created or decoded will be based on DS lists and DS maps and that the DS list corresponds to an array.
Multiple times and it is not at all obvious that that is what that means. It doesn't say it corresponds to an array, it says: "An ordered list of values, called a DS List in GameMaker Studio 2 but this can also be called an "array" or "sequence" in other programming languages." (emphasis added). Also, a ds_list is not an array and even saying that a ds_list corresponds to an array doesn't mean that they're interchangeable. Especially, since everywhere else in GameMaker they are definitely not interchangeable.

Moreover, the behavior of lists and arrays are actually different in two very important points. First, lists must actually be marked as such, but you don't need to, and in fact can't, do that with arrays. Second, that arrays get converted to lists when read back in.

I think the fact that arrays work natively in the json structure is great, especially if it is intended behavior and we can rely on it in the future. However, if it is the intended behavior, I think the manual should actually note that arrays work as well although upon importing they will be converted to a ds_list. Or something along those lines.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
I think the fact that arrays work natively in the json structure is great, especially if it is intended behavior and we can rely on it in the future. However, if it is the intended behavior, I think the manual should actually note that arrays work as well although upon importing they will be converted to a ds_list. Or something along those lines.
Ah, okay, I see what you mean! Yes, the manual should definitely be clearer on this. I am 99.99% sure this is intended behaviour but I'll check to make sure then add a task to update the manual. :)
 

samspade

Member
Afaik, all JSON created using the encode functions should be valid JSON, regardless of the GameMaker content (assuming the GameMaker content was added correctly, ie: lists marked as lists, maps marked as maps, etc...). Note that the docs can't cover every potential use... so test it and see!
Having tested all but pointers, several forms of JSON will be corrupt. NaN and Infinity will save out, but make it so the map can't be laoded it (it will result in -1). Undefined will save out (as null) but json_decode removes all null variables from the JSON, so it will just disappear when loaded in. Structs and method variables won't work at all, though they won't corrupt anything they just load in as empty maps. And hexadecimals, enums, etc, just save out and load in as numbers.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Sorry, but that is all intentional. Check this:

http://www.ecma-international.org/publications/standards/Ecma-404.htm
JSON is agnostic about numbers. In any programming language, there can be a variety of number types of various capacities and complements, fixed or floating, binary or decimal. That can make interchange between different programming languages difficult. JSON instead offers only the representation of numbers that humans use: a sequence of digits. All programming languages know how to make sense of digit sequences even if they disagree on internal representations. That is enough to allow interchange.
So, yeah, NaN, undefined, infinity, structs, methods, etc... shouldn't be expected to work.


EDIT: I'll look at adding this into the manual too, now I think about it.
 
Top