JSON and PoolVector2Array

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Atsy

When I serialize PoolVector2Array by to_json function, this array converted to string like this:“[(0, 0), (0, 1), (0, 2), (0, 3)]”

parse_json return this value as String, str2val also return String

Can I convert this value to PoolVector2Array by native Godot function, or I need to parce this string by myself?

:bust_in_silhouette: Reply From: Zylann

The JSON format standard does not have a data type to specifically store Vector2 values (and Vector3, and Rect2, and AABB, and Color, and poolvectors). So the information will be downgraded to something else, here a string.
You may have to parse the value manually indeed.

I usually parse JSON files manually anyways: before saving, I convert the data into a JSON-compatible dictionary and then save that ; and after loading from JSON I convert back into Godot types. Sometimes I use an automated script to format the data in and out, which works in some cases: https://github.com/Zylann/godot_texture_generator/blob/master/addons/zylann.tgen/util/jsonify.gd
Although, there is no way to differenciate an Array and a PoolIntArray for example since Array can be represented in exactly the same way. Same for int and float, JSON will always give you float, since the JSON standard only has number.
So despite its popularity, JSON is actually quite shitty if you want to work with Godot-specific data types^^"

Another option is to use a schema, i.e a specification of the data types in what you save. This is trivial in C# since you can save a class without embedding type information, and get it back easily since the language is strongly typed. But in GDScript, typing is dynamic so you may rely on a different strategy.

Using store_var and get_var with the File class should properly store Godot types, but it’s a binary format.