IDictionary?

Hey!
Yesterday I completed the saving system implementation, and I went to the JSON tutos when I saw the IDictionary that maps the JObject.
Why IDictionary instead of the ordinary Dictionary?
I never saw IDictionary before :laughing:

The IDictionary is an interface which gives us certain mapping features when working with structures like a JObject.

It’s actually going to go away in the next version of the tutorial, as it turns out the functions we need most from the JObject (mainly Add, [], and iterating over the KeyValuePairs) are already featured in the JObject.

The same with JArrays. You can manipulate these directly without mapping them to an IList.

For example: This is the code used for a QuestStatus from the Tutorial:

        public QuestStatus(JToken objectState)
        {
            if (objectState is JObject state)
            {
                IDictionary<string, JToken> stateDict = state;
                quest = Quest.GetByName(stateDict["questName"].ToObject<string>());
                completedObjectives.Clear();
                if (stateDict["completedObjectives"] is JArray completedState)
                {
                    IList<JToken> completedStateArray = completedState;
                    foreach (JToken objective in completedStateArray)
                    {
                        completedObjectives.Add(objective.ToObject<string>());
                    }
                }
            }
        }
        public JToken CaptureAsJToken()
        {
            JObject state = new JObject();
            IDictionary<string, JToken> stateDict = state;
            stateDict["questName"] = quest.name;
            JArray completedState = new JArray();
            IList<JToken> completedStateArray = completedState;
            foreach (string objective in completedObjectives)
            {
                completedStateArray.Add(JToken.FromObject(objective));
            }
            stateDict["completedObjectives"] = completedState;
            return state;
        }

But it can be replaced with:

        public QuestStatus(JToken objectState)
        {
            if (objectState is JObject stateDict)
            {
                quest = Quest.GetByName(stateDict["questName"].ToObject<string>());
                completedObjectives.Clear();
                if (stateDict["completedObjectives"] is JArray completedStateArray)
                {
                    foreach (JToken objective in completedStateArray)
                    {
                        completedObjectives.Add(objective.ToObject<string>());
                    }
                }
            }
        }
        public JToken CaptureAsJToken()
        {
            JObject stateDict = new JObject();
            stateDict["questName"] = quest.name;
            JArray completedStateArray = new JArray();
            foreach (string objective in completedObjectives)
            {
                completedStateArray.Add(JToken.FromObject(objective));
            }
            stateDict["completedObjectives"] = completedState;
            return state;
        }

Privacy & Terms