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

Function getType

parser.go:1150–1212  ·  view source on GitHub ↗

SYS-REQ-001, SYS-REQ-027

(data []byte, offset int)

Source from the content-addressed store, hash-verified

1148
1149// SYS-REQ-001, SYS-REQ-027
1150func getType(data []byte, offset int) ([]byte, ValueType, int, error) {
1151 var dataType ValueType
1152 endOffset := offset
1153
1154 // if string value
1155 if data[offset] == '"' {
1156 dataType = String
1157 if idx, _ := stringEnd(data[offset+1:]); idx != -1 {
1158 endOffset += idx + 1
1159 } else {
1160 return nil, dataType, offset, MalformedStringError
1161 }
1162 } else if data[offset] == '[' { // if array value
1163 dataType = Array
1164 // break label, for stopping nested loops
1165 endOffset = blockEnd(data[offset:], '[', ']')
1166
1167 if endOffset == -1 {
1168 return nil, dataType, offset, MalformedArrayError
1169 }
1170
1171 endOffset += offset
1172 } else if data[offset] == '{' { // if object value
1173 dataType = Object
1174 // break label, for stopping nested loops
1175 endOffset = blockEnd(data[offset:], '{', '}')
1176
1177 if endOffset == -1 {
1178 return nil, dataType, offset, MalformedObjectError
1179 }
1180
1181 endOffset += offset
1182 } else {
1183 // Number, Boolean or None
1184 // tokenEnd returns len(data) when no delimiter is found, never -1,
1185 // so the old end == -1 guard was dead code and has been removed.
1186 end := tokenEnd(data[endOffset:])
1187
1188 value := data[offset : endOffset+end]
1189
1190 switch data[offset] {
1191 case 't', 'f': // true or false
1192 if bytes.Equal(value, trueLiteral) || bytes.Equal(value, falseLiteral) {
1193 dataType = Boolean
1194 } else {
1195 return nil, Unknown, offset, UnknownValueTypeError
1196 }
1197 case 'u', 'n': // undefined or null
1198 if bytes.Equal(value, nullLiteral) {
1199 dataType = Null
1200 } else {
1201 return nil, Unknown, offset, UnknownValueTypeError
1202 }
1203 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
1204 dataType = Number
1205 default:
1206 return nil, Unknown, offset, UnknownValueTypeError
1207 }

Callers 3

internalGetFunction · 0.85

Calls 3

stringEndFunction · 0.85
blockEndFunction · 0.85
tokenEndFunction · 0.85