firstJSONObject returns the first complete JSON object found in s, or nil. encoding/json handles escaped quotes and braces inside string values. Limits parsing to 1MB to prevent memory exhaustion from malicious or accidentally huge error responses. To handle '{' characters in URLs or status text (e
(s string)
| 737 | // To handle '{' characters in URLs or status text (e.g., "param={value}"), |
| 738 | // we try parsing from each '{' position until we find valid JSON. |
| 739 | func firstJSONObject(s string) []byte { |
| 740 | const maxJSONSize = 1 << 20 // 1MB |
| 741 | |
| 742 | pos := 0 |
| 743 | for { |
| 744 | idx := strings.IndexByte(s[pos:], '{') |
| 745 | if idx < 0 { |
| 746 | return nil |
| 747 | } |
| 748 | start := pos + idx |
| 749 | |
| 750 | reader := io.LimitReader(strings.NewReader(s[start:]), maxJSONSize) |
| 751 | var raw json.RawMessage |
| 752 | if err := json.NewDecoder(reader).Decode(&raw); err == nil { |
| 753 | // Successfully decoded JSON |
| 754 | return raw |
| 755 | } |
| 756 | // Try next '{' position |
| 757 | pos = start + 1 |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | // scalarString renders a JSON scalar as text. JSON numbers decode to float64; |
| 762 | // whole numbers are rendered without a trailing ".0" so a code of 400 prints |
no outgoing calls
no test coverage detected