InjectJSONPropertiesFromBytes takes the given JSON byte slice, and for each KV pair, inserts into b under the given key. This has the potential to create duplicate keys, which whilst adhering to the spec, are ambiguous with how they get read... usually "last key wins" - although there is no standar
(b []byte, kvPairs ...KVPairBytes)
| 1376 | // This has the potential to create duplicate keys, which whilst adhering to the spec, are ambiguous with how they get read... |
| 1377 | // usually "last key wins" - although there is no standardized way of handling JSON with non-unique keys. |
| 1378 | func InjectJSONPropertiesFromBytes(b []byte, kvPairs ...KVPairBytes) (new []byte, err error) { |
| 1379 | if len(kvPairs) == 0 { |
| 1380 | // noop |
| 1381 | return b, nil |
| 1382 | } |
| 1383 | |
| 1384 | b = bytes.TrimSpace(b) |
| 1385 | |
| 1386 | bIsJSONObject, bIsEmpty := isJSONObject(b) |
| 1387 | if !bIsJSONObject { |
| 1388 | return nil, errors.New("b is not a JSON object") |
| 1389 | } |
| 1390 | |
| 1391 | return injectJSONPropertyFromBytes(b, bIsEmpty, kvPairs), nil |
| 1392 | } |
| 1393 | |
| 1394 | // isJSONObject checks if the given bytes are a JSON object, |
| 1395 | // and also whether it's an empty object or not. |