コード例 #1
0
    public void Load(string json)
    {
        var settings = new JsonSerializerSettings {
            TypeNameHandling = TypeNameHandling.Auto
        };
        BatteryConfig result = null;

        // catch all the JsonConvert exceptions into one exception.
        try
        {
            result = JsonConvert.DeserializeObject <BatteryConfig>(json, settings);
        }
        catch
        {
            throw new BadConfigException();
        }

        // empty json will return a null object
        if (result == null)
        {
            throw new EmptyConfigException();
        }

        // check scenes exist
        int           num_of_scenes = result.Games.Count;
        int           valid         = 0;
        List <string> valid_scenes  = new List <string>(GameList.Keys);

        foreach (GameConfig game in result.Games)
        {
            if (valid_scenes.Contains(game.Scene))
            {
                // Checks to make sure the correct scene is matched with it's
                // correct config class.
                var a = GameList[game.Scene].GetType();
                var b = game.GetType();
                if (a == b)
                {
                    valid++;
                }
            }
        }

        // are all the scenes valid?
        if (valid != num_of_scenes)
        {
            throw new InvalidScenesException();
        }

        Config = result;
    }
コード例 #2
0
    public string Generate()
    {
        var config = new BatteryConfig();

        config.Games = new List <GameConfig>();

        foreach (var game in GameList)
        {
            var g = game.Value;
            g.Scene = game.Key;
            config.Games.Add(game.Value);
        }

        string json = JsonConvert.SerializeObject(config, Formatting.Indented, new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.All
        });

        return(json);
    }