# json: 调用 MyStruct 类型的 MarshalJSON 出错：自定义错误

- **ID:** `go/encoding-json-marshaler-error`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

自定义 MarshalJSON 方法返回错误，导致 json.Marshal 失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.20 | active | — | — |

## 解决方案

1. **** (85% 成功率)
   ```
   func (m MyStruct) MarshalJSON() ([]byte, error) { if m.Invalid { return nil, errors.New("invalid state") }; return json.Marshal(m.Alias) }
   ```

## 无效尝试

- **** — Loses custom serialization logic, may produce wrong output. (70% 失败率)
- **** — Panic propagates and crashes the program. (90% 失败率)
