babalae/better-genshin-impact · critical · Exception

config.json 解析失败

Error message

config.json 解析失败

What it means

Thrown by LoadConfigData when JsonSerializer.Deserialize<AutoLeyLineConfigData> returns null after reading config.json. System.Text.Json returns null when the JSON content is literally 'null' or when the root token cannot be deserialized into the target type structure. This indicates the file exists but its content is not valid AutoLeyLineConfigData JSON.

Source

Thrown at BetterGenshinImpact/GameTask/AutoLeyLineOutcrop/AutoLeyLineOutcropTask.cs:231

        else
        {
            _taskParam.Timeout = _taskParam.FightConfig.Timeout;
        }
    }

    private void LoadConfigData()
    {
        // Load and validate the static ley line route config from disk.
        var workDir = Global.Absolute(@"GameTask\AutoLeyLineOutcrop");
        var configPath = Path.Combine(workDir, "Assets", "config.json");
        if (!File.Exists(configPath))
        {
            throw new FileNotFoundException("config.json 未找到", configPath);
        }

        var json = File.ReadAllText(configPath);
        _configData = JsonSerializer.Deserialize<AutoLeyLineConfigData>(json)
                      ?? throw new Exception("config.json 解析失败");
    }

    private void LoadRecognitionObjects()
    {
        // Template ROIs are tuned for the 1080p capture region.
        _openRo = BuildTemplate("Assets/icon/open.png");
        _closeRo = BuildTemplate("Assets/icon/close.png");
        _paimonMenuRo = BuildTemplate("Assets/icon/paimon_menu.png", new Rect(0, 0, ScaleTo1080(640), ScaleTo1080(216)));
        _boxIconRo = BuildTemplate("Assets/icon/box.png");
        _mapSettingButtonRo = BuildTemplate("Assets/icon/map_setting_button.bmp");
        _handbookTrackActionRo = BuildTemplate("Assets/icon/handbook_track_action_left.png", HandbookTrackActionButtonRoi, 0.72);

        _ocrRo1 = RecognitionObject.Ocr(ScaleTo1080(800), ScaleTo1080(200), ScaleTo1080(300), ScaleTo1080(100));
        _ocrRo2 = RecognitionObject.Ocr(ScaleTo1080(0), ScaleTo1080(200), ScaleTo1080(300), ScaleTo1080(300));
        _ocrRo3 = RecognitionObject.Ocr(ScaleTo1080(1200), ScaleTo1080(520), ScaleTo1080(300), ScaleTo1080(300));
    }

    private static int ScaleTo1080(int value)

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Restore config.json from a known-good backup or reinstall the application to get the bundled version.
  2. Open config.json in a JSON validator/linter to find and fix syntax errors.
  3. Verify the file is not empty and contains the expected root object with LeyLinePositions, MapPositions, and ErrorThreshold properties.
  4. If the schema changed after an update, replace the old file with the one from the new release.
Defensive patterns

Strategy: try-catch

Validate before calling

// Before deserializing, validate JSON is non-empty and non-null
var json = File.ReadAllText(configPath);
if (string.IsNullOrWhiteSpace(json) || json.Trim() == "null")
{
    Logger.LogError("config.json 内容为空或 null");
    return;
}

Try / catch

try
{
    _configData = JsonSerializer.Deserialize<AutoLeyLineConfigData>(json)
                 ?? throw new Exception("config.json 解析失败");
}
catch (JsonException ex)
{
    logger.LogError(ex, "config.json 格式错误,请恢复默认配置");
    throw new Exception("config.json 格式错误,请重新安装或恢复默认配置", ex);
}

Prevention

When it happens

Trigger: LoadConfigData reads config.json, deserializes it, and checks for null. Fires when the file is empty, contains only 'null', has a JSON syntax error that produces a null result, or has a schema mismatch (e.g. missing required root-level properties that cause the deserializer to return a default/null object).

Common situations: The config.json was corrupted by a failed write or manual edit; the file is empty (zero bytes); the JSON schema changed in a version update but an old config.json was left behind; a text editor saved the file with invalid JSON (trailing commas, unescaped characters).

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/a2215a6ac82b3deb. Report an issue: GitHub.