-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
Ability to convert native engine types to JSON and back. #91503
Conversation
Pushed an update with some style nitpicks, but also flagging some bugs in the implementation based on a first code review. Diff of my update: akien-mga@d06f058 |
core/io/json.cpp
Outdated
#if 0 // Uncomment after GH-85474 is merged. | ||
case Variant::PACKED_VECTOR4_ARRAY: { | ||
Dictionary d; | ||
PackedVector4Array arr; | ||
Array values; | ||
for (int i = 0; i < arr.size(); i++) { | ||
Vector4 v = arr[i]; | ||
values.push_back(v.x); | ||
values.push_back(v.y); | ||
values.push_back(v.z); | ||
values.push_back(v.w); | ||
} | ||
d["values"] = values; | ||
d[GDTYPE] = Variant::get_type_name(p_variant.get_type()); | ||
return d; | ||
} break; | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends on #85474.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merged, so enabled this code.
core/io/json.cpp
Outdated
} else if (type == Variant::get_type_name(Variant::COLOR)) { | ||
// TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You seem to have forgotten Color, I added this TODO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the missing implementation.
core/io/json.cpp
Outdated
} else if (type == Variant::get_type_name(Variant::ARRAY)) { | ||
// TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dictionary was there twice with the same code, seems like you forgot to edit it after copying to handle Array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, Array doesn't use GDTYPE so it doesn't need to be handled here (it's handled below).
I added an error check in case this changes, but we could also just remove this branch.
a4b175e
to
75171ed
Compare
This looks very interesting, I'll test it out and go through the code this weekend hopefully |
Did some testing locally with this project, which could be used as a base for further testing and unit tests: Some issues I noticed:
"material": {
"__gdtype": "Object"
}, but the
It needs better handling of null Objects as a valid format.
@export var _array: Array = [42.0, 10, Vector2.AXIS_X
@export var _packed_byte_array: PackedByteArray = PackedByteArray([10, 20, 30, 40])
@export var _dictionary: Dictionary = {_packed_byte_array: 1.0} Output: "_dictionary": {
"pairs": [
{
"key": {
"values": [],
"__gdtype": "PackedByteArray"
},
"value": 1
}
],
"__gdtype": "Dictionary"
}, The |
@akien-mga Ah thanks for the feedback and the fixes, I did not test the code much because I wanted to ask for implementation feedback first, then I was going to do the unit testing to ensure everything works properly. |
Vector3i v; | ||
v.x = values[0]; | ||
v.y = values[1]; | ||
v.z = values[1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
v.z = values[1]; | |
v.z = values[2]; |
} break; | ||
case Variant::PACKED_BYTE_ARRAY: { | ||
Dictionary d; | ||
PackedByteArray arr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should arr be assigned to p_variant here? Same for the other packed array types
Tested and works as expected, at a glance the code looks good too, with some cases pointed out above |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I support this pr enhancement. What is needed to get this mergable for a future release?
Reduz wrote unit tests, but traditionally that has not been a blocker.
Edited:
Did some thinking, if we know the types of the Variant, we can coherce the float into int and int into floats. The minimal epsilon between floats is small until we use the exponential scientific notation in floats to scale.
I don't necessarily see anything that's needed save for the code errors that need resolving |
I did some review, the entire Variant section is failing to convert. I'll probably write some unit tests. |
Probably the code errors reviewed above, try fixing those and see if it works then |
Superseded by #92656. |
Closes godotengine/godot-proposals#9510
TODO: Unit tests.