babalae/better-genshin-impact · error · InvalidOperationException
无法解析 RecognitionObject 配置文件: {filePath}
Error message
无法解析 RecognitionObject 配置文件: {filePath} What it means
RecognitionObjectJsonLoader.LoadFromFile reads the JSON and deserializes it into a RecognitionObjectJsonFile. If the deserialized result is null (empty file, a literal null, or content that does not map to the expected object shape), it throws InvalidOperationException with the file path. Malformed JSON instead throws a Newtonsoft.Json reader exception earlier; this specific throw is for a successfully parsed but null result.
Source
Thrown at BetterGenshinImpact/Core/Recognition/RecognitionObjectJsonLoader.cs:44
}
public static class RecognitionObjectJsonLoader
{
private sealed class LoggerTag;
private static readonly ILogger Logger = App.GetLogger<LoggerTag>();
public static RecognitionObject LoadFromFile(string filePath, string objectName, RecognitionObjectJsonLoadContext context)
{
ArgumentNullException.ThrowIfNull(filePath);
ArgumentNullException.ThrowIfNull(objectName);
ArgumentNullException.ThrowIfNull(context);
try
{
var json = File.ReadAllText(filePath, Encoding.UTF8);
var config = JsonConvert.DeserializeObject<RecognitionObjectJsonFile>(json)
?? throw new InvalidOperationException($"无法解析 RecognitionObject 配置文件: {filePath}");
return Load(config, objectName, context);
}
catch (Exception ex)
{
Logger.LogError(ex,
"Recognition 加载失败: {ObjectName} @ {CaptureWidth}x{CaptureHeight}, file={FilePath}",
objectName,
context.CaptureWidth,
context.CaptureHeight,
filePath);
throw;
}
}
public static RecognitionObject Load(RecognitionObjectJsonFile config, string objectName, RecognitionObjectJsonLoadContext context)
{
ArgumentNullException.ThrowIfNull(config);View on GitHub (pinned to a7cb36712d)
Solutions
- Restore a valid Recognition.json containing an object with at least an empty "objects" map.
- Check the file is not empty and that its root is a JSON object, not an array or scalar.
- Run the JSON through a validator/linter before deploying.
- Ensure merge conflicts are fully resolved (no leftover conflict markers).
Example fix
// before
var ro = RecognitionObjectJsonLoader.LoadFromFile(path, name, ctx); // file is empty/null
// after
var raw = File.ReadAllText(path, Encoding.UTF8);
if (string.IsNullOrWhiteSpace(raw))
throw new InvalidOperationException($"Recognition.json is empty: {path}");
var ro = RecognitionObjectJsonLoader.LoadFromFile(path, name, ctx); Defensive patterns
Strategy: validation
Validate before calling
static bool IsValidRecognitionJson(string path)
{
if (!File.Exists(path)) return false;
var raw = File.ReadAllText(path, Encoding.UTF8);
if (string.IsNullOrWhiteSpace(raw)) return false;
try { return JsonConvert.DeserializeObject<RecognitionObjectJsonFile>(raw) is not null; }
catch { return false; }
}
if (!IsValidRecognitionJson(filePath))
throw new InvalidOperationException($"Recognition config invalid or null: {filePath}"); Try / catch
try { return RecognitionObjectJsonLoader.LoadFromFile(filePath, objectName, context); }
catch (InvalidOperationException ex) when (ex.Message.Contains("无法解析 RecognitionObject 配置文件"))
{ Logger.LogError(ex, "Recognition.json parsed to null: {Path}", filePath); throw; } Prevention
- Never commit an empty or null Recognition.json.
- Run a JSON schema validation pass on Recognition.json during CI.
- Resolve merge conflicts fully so the file stays a valid object.
When it happens
Trigger: Calling LoadFromFile where Recognition.json is empty, contains the literal null, or is a JSON array/scalar rather than the expected object with objects/templates/vars fields.
Common situations: File truncated to zero bytes by a bad save or merge conflict; file overwritten with an array; encoding BOM causing a parse edge case; hand-edited file that became invalid.
Related errors
- 节点数据解析失败
- 未找到名称为 {objectName} 的 RecognitionObject 配置
- {fieldName} 不能为空
- Failed to deserialize macro
- 解析配置组JSON配置失败
AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13).
Data as JSON: /api/errors/100063a876a87514.
Report an issue: GitHub.