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

Function Delete

parser.go:894–1028  ·  view source on GitHub ↗

* Del - Receives existing data structure, path to delete. Returns: `data` - return modified data */ SYS-REQ-010, SYS-REQ-033, SYS-REQ-034, SYS-REQ-035, SYS-REQ-048, SYS-REQ-049, SYS-REQ-050, SYS-REQ-056

(data []byte, keys ...string)

Source from the content-addressed store, hash-verified

892*/
893// SYS-REQ-010, SYS-REQ-033, SYS-REQ-034, SYS-REQ-035, SYS-REQ-048, SYS-REQ-049, SYS-REQ-050, SYS-REQ-056
894func Delete(data []byte, keys ...string) []byte {
895 lk := len(keys)
896 if lk == 0 {
897 // Deleting the root produces an empty document whose backing array
898 // must not alias the caller's input.
899 return make([]byte, 0)
900 }
901
902 array := false
903 if len(keys[lk-1]) > 0 && string(keys[lk-1][0]) == "[" {
904 array = true
905 }
906
907 var startOffset, keyOffset int
908 endOffset := len(data)
909 var err error
910 if !array {
911 if len(keys) > 1 {
912 _, _, startOffset, endOffset, err = internalGet(data, keys[:lk-1]...)
913 if err != nil {
914 // problem parsing the data
915 return data
916 }
917 }
918
919 keyOffset, err = findKeyStart(data[startOffset:endOffset], keys[lk-1])
920 if err == KeyPathNotFoundError {
921 // problem parsing the data
922 return data
923 }
924 keyOffset += startOffset
925 var subEndOffset int
926 _, _, _, subEndOffset, err = internalGet(data[startOffset:endOffset], keys[lk-1])
927 if err != nil {
928 return data
929 }
930 endOffset = startOffset + subEndOffset
931 tokEnd := tokenEnd(data[endOffset:])
932 tokStart := findTokenStart(data[:keyOffset], ","[0])
933
934 if endOffset+tokEnd >= len(data) {
935 // tokenEnd sentinel: no delimiter found, input is truncated
936 return data
937 }
938
939 // guard: tokenEnd sentinel may return -1 on truncated input; bounds-check before deref.
940 idx := endOffset + tokEnd
941 // Scan forward from idx through any JSON whitespace to find the next
942 // real token. The original check only matched a single ' ' byte
943 // before the comma, so inputs like '{"a":1,\n"b":2}' or '[0,0 ,0]'
944 // (multiple whitespace bytes) bypassed the cleanup and left a
945 // dangling comma sequence. Found by FuzzPathMutation.
946 nextTokIdx := idx
947 for nextTokIdx < len(data) && isJSONWhitespace(data[nextTokIdx]) {
948 nextTokIdx++
949 }
950 if len(data) > idx && data[idx] == ',' {
951 endOffset += tokEnd + 1

Callers 15

BenchmarkDeleteSmallFunction · 0.92
BenchmarkDeleteNestedFunction · 0.92
BenchmarkDeleteLargeFunction · 0.92
TestMutationNilSafetyFunction · 0.85
TestMutationUnicodeKeysFunction · 0.85
FuzzPathMutationFunction · 0.85
applySequenceStepFunction · 0.85
FuzzDeleteFunction · 0.85

Calls 7

internalGetFunction · 0.85
findKeyStartFunction · 0.85
tokenEndFunction · 0.85
findTokenStartFunction · 0.85
isJSONWhitespaceFunction · 0.85
lastTokenFunction · 0.85
nextTokenFunction · 0.85

Tested by 15

BenchmarkDeleteSmallFunction · 0.74
BenchmarkDeleteNestedFunction · 0.74
BenchmarkDeleteLargeFunction · 0.74
TestMutationNilSafetyFunction · 0.68
TestMutationUnicodeKeysFunction · 0.68
FuzzPathMutationFunction · 0.68
applySequenceStepFunction · 0.68
TestDeleteFunction · 0.68