MarshalWithExtras is used to marshal a struct with additional properties. Stability for the API of MarshalWithExtras is not guaranteed.
(f T, underlying any, extras map[string]R)
| 41 | // |
| 42 | // Stability for the API of MarshalWithExtras is not guaranteed. |
| 43 | func MarshalWithExtras[T ParamStruct, R any](f T, underlying any, extras map[string]R) ([]byte, error) { |
| 44 | if f.null() { |
| 45 | return []byte("null"), nil |
| 46 | } else if len(extras) > 0 { |
| 47 | bytes, err := shimjson.Marshal(underlying) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | for k, v := range extras { |
| 52 | var a any = v |
| 53 | if a == Omit { |
| 54 | // Errors when handling ForceOmitted are ignored. |
| 55 | if b, e := sjson.DeleteBytes(bytes, k); e == nil { |
| 56 | bytes = b |
| 57 | } |
| 58 | continue |
| 59 | } |
| 60 | bytes, err = sjson.SetBytes(bytes, EscapeSJSONKey(k), v) |
| 61 | if err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | } |
| 65 | return bytes, nil |
| 66 | } else if ovr, ok := f.Overrides(); ok { |
| 67 | return shimjson.Marshal(ovr) |
| 68 | } else { |
| 69 | return shimjson.Marshal(underlying, shimjson.WithSkipCompaction(true)) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // MarshalUnion uses a shimmed 'encoding/json' from Go 1.24, to support the 'omitzero' tag |
| 74 | // |
searching dependent graphs…