Append acts like Marshal but appends the json representation to b instead of always reallocating a new slice.
(b []byte, x any, flags AppendFlags)
| 194 | // Append acts like Marshal but appends the json representation to b instead of |
| 195 | // always reallocating a new slice. |
| 196 | func Append(b []byte, x any, flags AppendFlags) ([]byte, error) { |
| 197 | if x == nil { |
| 198 | // Special case for nil values because it makes the rest of the code |
| 199 | // simpler to assume that it won't be seeing nil pointers. |
| 200 | return append(b, "null"...), nil |
| 201 | } |
| 202 | |
| 203 | t := reflect.TypeOf(x) |
| 204 | p := (*iface)(unsafe.Pointer(&x)).ptr |
| 205 | |
| 206 | cache := cacheLoad() |
| 207 | c, found := cache[typeid(t)] |
| 208 | |
| 209 | if !found { |
| 210 | c = constructCachedCodec(t, cache) |
| 211 | } |
| 212 | |
| 213 | b, err := c.encode(encoder{flags: flags}, b, p) |
| 214 | runtime.KeepAlive(x) |
| 215 | return b, err |
| 216 | } |
| 217 | |
| 218 | // Escape is a convenience helper to construct an escaped JSON string from s. |
| 219 | // The function escales HTML characters, for more control over the escape |
searching dependent graphs…