This class encodes and decodes JSON strings. Spec. details, see http://www.json.org/ JSON uses Arrays and Objects. These correspond here to the datatypes ArrayList and Hashtable. All numbers are parsed to doubles.
Beispiel #1
0
        private void LoadAchievementDescriptions(string descriptionJSON)
        {
            Hashtable jsonTable = MiniJSON.jsonDecode(descriptionJSON) as Hashtable;

            if (jsonTable != null)
            {
                ArrayList achievementDefinitions = jsonTable["achievementDescriptions"] as ArrayList;
                if (achievementDefinitions != null)
                {
                    foreach (Hashtable achievementDefinition in achievementDefinitions)
                    {
                        string id     = achievementDefinition["id"] as string;
                        string title  = achievementDefinition["title"] as string;
                        int    points = (achievementDefinition["points"] is long?(int)achievementDefinition["points"] : 0);
                        string achievedDescription   = achievementDefinition["achievedDescription"] as string;
                        string unachievedDescription = achievementDefinition["unachievedDescription"] as string;
                        bool   hidden = achievementDefinition["hidden"] is bool?(bool)achievementDefinition["hidden"] : false;

                        m_AchievementDescriptions.Add(new AchievementDescription(id, title, null, achievedDescription, unachievedDescription, hidden, points));
                    }
                }
            }
            else
            {
                if (!MiniJSON.lastDecodeSuccessful())
                {
                    Debug.LogWarning(string.Format("[LocalPlatform] Unable to decode JSON definition file. Error: {0}", MiniJSON.getLastErrorSnippet()));
                }
                else
                {
                    Debug.LogWarning("[LocalPlatform] Unable to decode JSON definition file.");
                }
            }
        }
Beispiel #2
0
        private void LoadDefinitions(string definitionJSON)
        {
            Hashtable jsonTable = MiniJSON.jsonDecode(definitionJSON) as Hashtable;

            if (jsonTable != null)
            {
                ArrayList statDefinitions = jsonTable["statDefinitions"] as ArrayList;
                if (statDefinitions != null)
                {
                    foreach (Hashtable statDefinition in statDefinitions)
                    {
                        string id         = statDefinition["id"] as string;
                        string desc       = statDefinition["description"] as string;
                        string typeString = statDefinition["type"] as string;

                        if (typeString != null)
                        {
                            try
                            {
                                StatisticType statisticType = (StatisticType)Enum.Parse(typeof(StatisticType), typeString, true);
                                if (Enum.IsDefined(typeof(StatisticType), statisticType))
                                {
                                    if (id == null)
                                    {
                                        Debug.LogWarning("[Stats] Missing id");
                                        continue;
                                    }
                                    if (desc == null)
                                    {
                                        Debug.LogWarning("[Stats] Missing description");
                                        continue;
                                    }

                                    AddDefinition(id, desc, statisticType);
                                }
                                else
                                {
                                    Debug.LogWarning(string.Format("[Stats] {0} is not an underlying value of the StatisticType enumeration.", typeString));
                                }
                            }
                            catch (ArgumentException)
                            {
                                Debug.LogWarning(string.Format("[Stats] {0} is not a member of the StatisticType enumeration.", typeString));
                            }
                        }
                    }
                }
            }
            else
            {
                if (!MiniJSON.lastDecodeSuccessful())
                {
                    Debug.LogWarning(string.Format("[Stats] Unable to decode JSON definition file. Error: {0}", MiniJSON.getLastErrorSnippet()));
                }
                else
                {
                    Debug.LogWarning("[Stats] Unable to decode JSON definition file.");
                }
            }
        }