MarshalJSON implements the [json.Marshaler] interface.
()
| 333 | |
| 334 | // MarshalJSON implements the [json.Marshaler] interface. |
| 335 | func (l FieldsList) MarshalJSON() ([]byte, error) { |
| 336 | if l == nil { |
| 337 | l = []Field{} // always init to ensure that it is serialized as empty array |
| 338 | } |
| 339 | |
| 340 | wrapper := make([]map[string]any, 0, len(l)) |
| 341 | |
| 342 | for _, f := range l { |
| 343 | // precompute the json into a map so that we can append the type to a flatten object |
| 344 | raw, err := json.Marshal(f) |
| 345 | if err != nil { |
| 346 | return nil, err |
| 347 | } |
| 348 | |
| 349 | data := map[string]any{} |
| 350 | if err := json.Unmarshal(raw, &data); err != nil { |
| 351 | return nil, err |
| 352 | } |
| 353 | data["type"] = f.Type() |
| 354 | |
| 355 | wrapper = append(wrapper, data) |
| 356 | } |
| 357 | |
| 358 | return json.Marshal(wrapper) |
| 359 | } |
| 360 | |
| 361 | // Value implements the [driver.Valuer] interface. |
| 362 | func (l FieldsList) Value() (driver.Value, error) { |