How to parse a string a cast it into a Dictionnary

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

I have this string:

string line = {"Filename":"res://Nodes/Bodies/Berry.tscn","Parent":"/root/Beach/BerrySpawner","PositionX":19.064032,"PositionY":25.237549}

and I want to cast it into a dictionnary

So I followed this tutorial:
docs.godotengine.org/en/stable/tutorials/io/saving_games.html#serializing
And I wrote the following line of code:

var nodeData = (Godot.Collections.Dictionary<string, object>) (JSON.Parse (line).Result);

Which gave me the following error:

Unhandled Exception: System.InvalidCastException: Specified cast is not valid.

Am I doing something wrong here?

:bust_in_silhouette: Reply From: jjmontes

It seems that Godot does not resolve the templated type of a serialized collection, nor with JSON nor with File serialization methods. I think this comes down to the fact that Godot Variant internally only understands plain Dictionary objects.

We can work around this using plain Godot.Collections.Dictionary objects:

Godot.Collections.Dictionary nodeData = (Godot.Collections.Dictionary) (JSON.Parse (line).Result);