Dynamic update of C# class object from JSON

Hi All,

I am a C# newbie, and I would like to understand how to create (and update) a C# class object from a JSON file while in play mode. So far, I have used QuickType to convert the file into a C# class. Nevertheless, this is an issue when I need to update my c# class if I use cloud-based multi-user applications that modify the original JSON structure.

Is there a workaround that allows me to update a c# class from a JSON (with no pre-determined structure) without opening the browser, converting the JSON file, importing it into VS code, and restarting the game?

I don’t believe there is an easy way to do this, as you would basically need to compile code at runtime (since we don’t know the class structure beforehand). Would you mind sharing what you’re trying to accomplish long-term? We might be able to supply a different solution to your problem.

You can try dynamic classes, but you will lose autocomplete. You just need to download the new json and convert it to a string.

          using System.Dynamic;
          using Newtonsoft.Json;

          ...

          string json = @"{
          'Name': 'Bad Boys',
          'ReleaseDate': '1995-4-7T00:00:00',
          'Genres': [
            'Action',
            'Comedy'
          ],
          'Test': 25
        }";

        dynamic m = JsonConvert.DeserializeObject<dynamic>(json, new JsonSerializerSettings()
        {
            Formatting = Formatting.Indented,
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
        });

        Debug.Log($"{m.Name} - {m.ReleaseDate} - {m.Test}")

I understand. I decided to fix the structure of the json and avoid any changes in the file. I will use QuickType to map the class object from the text file once before compiling and building the app. Thank you for the info.