-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON - loading null as undefined, so saved struct/array would have same values as when saving #2749
Comments
Another idea (by @Alphish ) is to add: var _json = json_parse_ext(string, function(_value) {
... // make any additional processing you wish
return _value;
}); |
More complete example, with both stringify and parse sides covered: var _json = json_stringify(room_data, /* pretty print */ false, function(_value) {
if (!is_handle(_value))
return _value;
if (sprite_exists(_value))
return "#ASSET:" + sprite_get_name(_value);
else if (object_exists(_value))
return "#ASSET:" + object_get_name(_value);
else if (room_exists(_value))
return "#ASSET:" + room_get_name(_value);
else
return _value;
}); And the parse side: var _struct = json_parse_ext(room_json, function(_value) {
if (_value == pointer_null)
return undefined; // this handles the original point of parsing JSON nulls as undefined values
else if (is_string(_value) && string_starts_with(_value, "#ASSET:"))
return asset_get_index(string_delete(_value, 1, string_length("#ASSET:")));
else
return _value;
}); (note: apparently in 2024.2 assets will be magically handled by JSON functions, but there are other potential uses too: e.g. making structs store their constructor type and static_setting that type on load etc.) |
mentioned 2024.2 change: #2808 |
Definitely would be nice functionality to have! Personally, I would prefer it just to be handled like assets in #2808 (where |
Russell already confirmed he will work something out, so we need to wait which solution would be taken. Keep fingers crossed for 2024.2 ;) |
…ssues/1540 * new arguments to `json_parse` and `json_stringify` to allow user to change values as they come in. * new functions are added as optional parameters and have 2 arguments the `key` and the `value`, they return the value and can modify if it they wish.
Feature added in 2024.2
|
* Updated json_stringify() and json_parse() * Filter function added (YoYoGames/GameMaker-Bugs#2749) * null->undefined updated * handle conversion mentioned (YoYoGames/GameMaker-Bugs#2808) * Data Types -> Handles resource list updated
Feature verified in IDE v2024.200.0.472 Runtime v2024.200.0.488 |
Is your feature request related to a problem?
Gamemaker have inconsistency in
json_parse()
andjson_stringify()
functions.json_stringify()
saves GMLundefined
as JSONnull
json_parse()
loads JSONnull
aspointer_null
This means, that when we're saving struct in which some values are
undefined
because they are either not set, or developer marks some variable as "not set" in this way, on loading it back, we need to pass it to convert allpointer_null
s back toundefined
s.This is not that easy and time consuming in case of nested structures and arrays.
I understand that GML saves
undefined
asnull
because JSON doesn't offerundefined
(while JS does), so there's no other value, but I don't understand why it reads it back as different value (indeedpointer_null
is maybe more close to JSONnull
, but generallypointer_null
is rather not often used for storing empty values in structs/lists/maps/arrays in favour ofundefined
).It's rather usual, that GM developers are using
undefined
in GML to mark "not set" things, like:and it's rather expected, that after saving and loading such struct,
last_clicked
would be stillundefined
notpointer_null
.Describe the solution you'd like
As changing
json_parse()
so it givesundefined
insteadpointer_null
could break existing code, my proposal is to add additional argument, hownull
should be treated:In that case:
would result in:
while:
would result in:
Describe alternatives you've considered
We already have special prefixes/suffixes for some values for json_stringify:
@i64@<NUMBER>$i64$
infinity
is saved as@@infinity$$
/@@-infinity$$
NaN
is saved as@@nan$$
So,
undefined
could be saved as:@@undefined$$
However, this again could break existing code and existing functionality, so that would require:
This way, saving data which contains
undefined
that can be read back asundefined
would look this way:Additional context
No response
The text was updated successfully, but these errors were encountered: