{"record":{"id":"dd09ac119849a557","repo":"babalae/better-genshin-impact","slug":"failed-to-deserialize-json-dd09ac","errorCode":null,"errorMessage":"Failed to deserialize JSON.","messagePattern":"Failed to deserialize JSON\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"BetterGenshinImpact/GameTask/Common/Map/Maps/Base/BaseMapLayerByTemplateMatch.cs","lineNumber":59,"sourceCode":"        var grayMapPath = Path.Combine(layerDir, grayMapFileName);\n        FineGrayMap = Bv.ImRead(grayMapPath, ImreadModes.Grayscale)?? throw new Exception($\"灰度分层地图 {LayerId} 读取失败\");\n        speedTimer.Record(\"粗匹配用灰度图\");\n        speedTimer.DebugPrint();\n    }\n\n    public static List<BaseMapLayerByTemplateMatch> LoadLayers(SceneBaseMapByTemplateMatch sceneBaseMap)\n    {\n        var layers = new List<BaseMapLayerByTemplateMatch>();\n        var layerDir = Path.Combine(Global.Absolute(@\"Assets\\Map\\\"), sceneBaseMap.Type.ToString());\n        if (!Directory.Exists(layerDir))\n        {\n            return layers;\n        }\n        var jsonFiles = Directory.GetFiles(layerDir, \"*.json\", SearchOption.AllDirectories);\n        foreach (var jsonFile in jsonFiles)\n        {\n            var json = File.ReadAllText(jsonFile);\n            var tempLayers = JsonSerializer.Deserialize<List<BaseMapLayerByTemplateMatch>>(json) ?? throw new Exception(\"Failed to deserialize JSON.\");\n            layers.AddRange(tempLayers);\n        }\n        foreach (var layer in layers)\n        {\n            layer.LoadLayer(layerDir);\n        }\n        return layers;\n    }\n    \n    public (Point2f, double) RoughMatch(Mat[] maskedMiniMaps, Mat maskF)\n    {\n        var (pos, val) = CoarseColorMatcher.Match(maskedMiniMaps, maskF);\n        return (MapToWorld(pos, RoughZoom, RoughSize), val);\n    }\n\n    public (Point2f, double) RoughMatch(Mat[] maskedMiniMaps, Mat maskF, Point2f preLoc, int[]? channels = null)\n    {\n        var roughPos = WorldToMap(preLoc, RoughZoom);","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/babalae/better-genshin-impact/blob/a7cb36712dcb409be610257d877fcea3597e9d6b/BetterGenshinImpact/GameTask/Common/Map/Maps/Base/BaseMapLayerByTemplateMatch.cs#L41-L77","documentation":"Thrown in BaseMapLayerByTemplateMatch.LoadLayers when JsonSerializer.Deserialize<List<BaseMapLayerByTemplateMatch>>(json) returns null. System.Text.Json returns null when the JSON payload is the literal 'null' token or an empty/malformed file deserializes to null for a reference type. An empty array '[]' deserializes to an empty list (not null), so this specifically indicates a null-bearing document.","triggerScenarios":"LoadLayers reads every *.json under layerDir (recursive) and deserializes each. If any JSON file contains just 'null' (e.g. a placeholder, a mis-saved file, or an editor that wrote null), the result is null and this throws.","commonSituations":"A hand-edited or auto-generated JSON file contains the literal null; a file was truncated to empty and a default/null was written; an unrelated .json (e.g. a config) was placed in the map directory and happens to deserialize to null.","solutions":["Open each *.json under Assets\\Map\\<Type> and find the one that is 'null' or empty; replace it with a valid layer array.","Restrict the JSON search to files matching the layer naming scheme instead of all *.json.","Treat null as an empty list rather than fatal: use 'tempLayers ?? new()' or skip the file with a warning."],"exampleFix":"// before\nvar tempLayers = JsonSerializer.Deserialize<List<BaseMapLayerByTemplateMatch>>(json) ?? throw new Exception(\"Failed to deserialize JSON.\");\n\n// after — tolerate a null document and report which file is bad\nvar tempLayers = JsonSerializer.Deserialize<List<BaseMapLayerByTemplateMatch>>(json);\nif (tempLayers == null || tempLayers.Count == 0)\n{\n    Logger.LogWarning(\"地图层 JSON 文件内容为空，已跳过: {File}\", jsonFile);\n    continue;\n}\nlayers.AddRange(tempLayers);","handlingStrategy":"validation","validationCode":"var json = File.ReadAllText(jsonFile);\nif (string.IsNullOrWhiteSpace(json) || json.Trim() == \"null\")\n{\n    Logger.LogWarning(\"跳过空的地图层 JSON: {File}\", jsonFile);\n    continue;\n}","typeGuard":null,"tryCatchPattern":"try { var tempLayers = JsonSerializer.Deserialize<List<BaseMapLayerByTemplateMatch>>(json); }\ncatch (JsonException ex) { Logger.LogError(ex, \"JSON 反序列化失败: {File}\", jsonFile); throw; }","preventionTips":["Keep map directories free of unrelated *.json files.","Validate JSON is a non-null array before deserializing.","Name layer JSON files consistently and filter by pattern."],"tags":["map-data","json-deserialization","asset-loading","config"],"backgroundTag":null,"analyzedSha":"a7cb36712dcb409be610257d877fcea3597e9d6b","analyzedAt":"2026-08-13T16:44:57.548Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}