EiTaNBaRiBoA / JsonClassConverter

Simplify your JSON and class interactions! jsonClassConverter effortlessly stores JSON data, converts between JSON and class objects, and instantly loads JSON into usable classes. Streamline your development and data handling with this essential tool.
MIT License
42 stars 2 forks source link

Support for vectors, @export variables only and other small changes #2

Closed mikemike37 closed 1 month ago

mikemike37 commented 1 month ago

(I'm new to github, so apologies if I'm missing some best practice here!)

I've just started using your JsonClassConverter - thanks for sharing! I noticed it suffers from the very old problem that json serialising/deserialising of vectors just doesn't work: https://github.com/godotengine/godot/issues/11866

I have implemented a fix for that, that I thought could be interesting to take to mainline.

Another significant change is the requirement that the variable have the @export annotation. At least in my case bringing over all the variables was not needed and was causing me problems.

I also split the load function into two parts: json_file_to_dict and json_file_to_class, since I needed to handle that a bit specially.

Take or leave any parts of this, but in any case thanks for your work :)

EiTaNBaRiBoA commented 1 month ago

Hi @mikemike37 ,

Thanks for the PR! I appreciate your contribution.

I have a quick question regarding the property.usage & PROPERTY_USAGE_STORAGE > 0 condition you added in the class_to_json() function. Could you elaborate on the specific use cases where this check is beneficial?

While reviewing your code, I made a couple of minor adjustments:

  1. Null Safety: I enhanced the code to ensure null safety. It should now consistently return non-null values, except in edge cases where the provided GDScript is invalid or null. This helps prevent unexpected errors and improves robustness.

  2. Color Type Handling (TYPE_COLOR): I addressed an edge case where the TYPE_COLOR property was returning a Vector4 without explicitly indicating it in the name. I added additional checks to handle this situation correctly:

    # Edge case where the property type is color but doesn't have "Vector" in its name
    if property.type == TYPE_COLOR:
       value = str_to_var(value)
    _class.set(property.name, value)

I've attached the modified file for your review. Please take a look and let me know if you have any suggestions or if you think further improvements can be made. JsonClassConverter.zip

Thanks again for your work on this!

EiTaNBaRiBoA commented 1 month ago

Thank you so much for your contribution! I've implemented some fixes and improvements, including handling colors and Array[Vector3] (which were previously saved as strings like "(3,4)"). You'll find a special thanks to you in the release notes!