MCPcopy Create free account
hub / github.com/buger/jsonparser / objectEachConfig

Function objectEachConfig

parser.go:1855–1970  ·  view source on GitHub ↗

SYS-REQ-115

(config Config, data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string)

Source from the content-addressed store, hash-verified

1853
1854// SYS-REQ-115
1855func objectEachConfig(config Config, data []byte, callback func(key []byte, value []byte, dataType ValueType, offset int) error, keys ...string) (err error) {
1856 offset := 0
1857
1858 // Descend to the desired key, if requested
1859 if len(keys) > 0 {
1860 if off := searchKeysConfig(config, data, keys...); off == -1 {
1861 return KeyPathNotFoundError
1862 } else {
1863 offset = off
1864 }
1865 }
1866
1867 // Validate and skip past opening brace
1868 if off := nextTokenConfig(config, data[offset:]); off == -1 {
1869 return MalformedObjectError
1870 } else if offset += off; data[offset] != '{' {
1871 return MalformedObjectError
1872 } else {
1873 offset++
1874 }
1875
1876 // Skip to the first token inside the object, or stop if we find the ending brace
1877 if off := nextTokenConfig(config, data[offset:]); off == -1 {
1878 return MalformedJsonError
1879 } else if offset += off; data[offset] == '}' {
1880 return nil
1881 }
1882
1883 // Loop pre-condition: data[offset] points to what should be either the next entry's key,
1884 // or the closing brace (if it's anything else, the JSON is malformed).
1885 // Every iteration either returns or advances offset past a token, so the loop
1886 // always exits via return; the former `offset < len(data)` guard was structurally
1887 // always true because internal nextToken/stringEnd calls return errors before
1888 // offset can reach len(data).
1889 for {
1890 // Step 1: find the next key
1891 var key []byte
1892
1893 // Check what the the next token is: start of string, end of object, or something else (error)
1894 switch data[offset] {
1895 case '"':
1896 offset++ // accept as string and skip opening quote
1897 case '\'':
1898 if !config.AllowSingleQuotes {
1899 return MalformedObjectError
1900 }
1901 offset++ // accept as string and skip opening quote
1902 case '}':
1903 return nil // we found the end of the object; stop and return success
1904 default:
1905 return MalformedObjectError
1906 }
1907
1908 // Find the end of the key string
1909 var keyEscaped bool
1910 quote := data[offset-1]
1911 if off, esc := stringEndConfig(config, data[offset:], quote); off == -1 {
1912 return MalformedJsonError

Callers 2

ObjectEachFunction · 0.85
ObjectEachMethod · 0.85

Calls 5

searchKeysConfigFunction · 0.85
nextTokenConfigFunction · 0.85
stringEndConfigFunction · 0.85
unescapeConfigFunction · 0.85
GetMethod · 0.45

Tested by

no test coverage detected