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
- Validate the JSON is well-formed using a JSON linter before calling FromJson.
- Ensure the JSON root is an object with ScriptGroup's expected properties (Index, Name, Config, Projects).
- If the JSON is valid but yields null, check for serializer-specific issues (e.g. [JsonObject] attributes, missing parameterless constructor).
- 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
- Validate JSON syntax with a linter before deserialization.
- Back up script group config files before modifying.
- Ensure the JSON structure matches ScriptGroup's property names exactly.
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
- 无法解析 RecognitionObject 配置文件: {filePath}
- Failed to deserialize macro
- Failed to deserialize JSON.
- JSON 战斗策略格式错误:{ex.Message}
- config.json 解析失败
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/0ff26d8787a586a3.
Report an issue: GitHub.