babalae/better-genshin-impact · error · Exception

解析配置组JSON配置失败

Error message

解析配置组JSON配置失败

What it means

Thrown by ScriptGroup.FromJson when Newtonsoft.Json JsonConvert.DeserializeObject<ScriptGroup> returns null, indicating the input JSON could not be deserialized into a ScriptGroup object. Note this uses Newtonsoft.Json while ToJson uses System.Text.Json — a mixed-serializer pattern that can cause subtle serialization mismatches.

Source

Thrown at BetterGenshinImpact/Core/Script/Group/ScriptGroup.cs:51

    public ScriptGroup()
    {
        Projects.CollectionChanged += ProjectsCollectionChanged;
    }

    private void ProjectsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        OnPropertyChanged(nameof(Projects));
    }

    public string ToJson()
    {
        return JsonSerializer.Serialize(this, ConfigService.JsonOptions);
    }

    public static ScriptGroup FromJson(string json)
    {
        var group = JsonConvert.DeserializeObject<ScriptGroup>(json) ?? throw new Exception("解析配置组JSON配置失败");
        ResetGroupInfo(group);
        return group;
    }

    public static void ResetGroupInfo(ScriptGroup group)
    {
        foreach (var project in group.Projects)
        {
            project.GroupInfo = group;
        }
    }

    public void AddProject(ScriptGroupProject project)
    {
        project.GroupInfo = this;
        Projects.Add(project);
    }
}

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Validate the JSON is well-formed using a JSON linter before calling FromJson.
  2. Ensure the JSON root is an object with ScriptGroup's expected properties (Index, Name, Config, Projects).
  3. If the JSON is valid but yields null, check for serializer-specific issues (e.g. [JsonObject] attributes, missing parameterless constructor).
  4. Wrap FromJson in try-catch to also handle JsonReaderException from malformed JSON.
Defensive patterns

Strategy: try-catch

Validate before calling

if (string.IsNullOrWhiteSpace(json))
    throw new ArgumentException("JSON content is empty");
// Quick syntax check
JsonConvert.DeserializeObject(json); // throws on malformed JSON

Try / catch

try
{
    var group = ScriptGroup.FromJson(json);
}
catch (Exception ex) when (ex.Message.Contains("解析配置组JSON配置失败"))
{
    TaskControl.Logger.LogError(ex, "Failed to parse script group config");
    // Offer to reset or restore default config
}
catch (Newtonsoft.Json.JsonException ex)
{
    TaskControl.Logger.LogError(ex, "Malformed JSON in script group config");
}

Prevention

When it happens

Trigger: Calling ScriptGroup.FromJson(json) with malformed JSON, empty string, JSON representing a different type, or JSON whose structure doesn't match ScriptGroup's expected properties. The null-coalescing throw only fires when deserialization yields null (not when it throws a JsonException — that propagates separately).

Common situations: Corrupted or manually-edited configuration group file. Version mismatch where the config file format changed but old files weren't migrated. Empty file content passed to FromJson.

Related errors


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