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

Function getType

parser.go:1176–1238  ·  view source on GitHub ↗

SYS-REQ-001, SYS-REQ-027

(data []byte, offset int)

Source from the content-addressed store, hash-verified

1174
1175// SYS-REQ-001, SYS-REQ-027
1176func getType(data []byte, offset int) ([]byte, ValueType, int, error) {
1177 var dataType ValueType
1178 endOffset := offset
1179
1180 // if string value
1181 if data[offset] == '"' {
1182 dataType = String
1183 if idx, _ := stringEnd(data[offset+1:]); idx != -1 {
1184 endOffset += idx + 1
1185 } else {
1186 return nil, dataType, offset, MalformedStringError
1187 }
1188 } else if data[offset] == '[' { // if array value
1189 dataType = Array
1190 // break label, for stopping nested loops
1191 endOffset = blockEnd(data[offset:], '[', ']')
1192
1193 if endOffset == -1 {
1194 return nil, dataType, offset, MalformedArrayError
1195 }
1196
1197 endOffset += offset
1198 } else if data[offset] == '{' { // if object value
1199 dataType = Object
1200 // break label, for stopping nested loops
1201 endOffset = blockEnd(data[offset:], '{', '}')
1202
1203 if endOffset == -1 {
1204 return nil, dataType, offset, MalformedObjectError
1205 }
1206
1207 endOffset += offset
1208 } else {
1209 // Number, Boolean or None
1210 // tokenEnd returns len(data) when no delimiter is found, never -1,
1211 // so the old end == -1 guard was dead code and has been removed.
1212 end := tokenEnd(data[endOffset:])
1213
1214 value := data[offset : endOffset+end]
1215
1216 switch data[offset] {
1217 case 't', 'f': // true or false
1218 if bytes.Equal(value, trueLiteral) || bytes.Equal(value, falseLiteral) {
1219 dataType = Boolean
1220 } else {
1221 return nil, Unknown, offset, UnknownValueTypeError
1222 }
1223 case 'u', 'n': // undefined or null
1224 if bytes.Equal(value, nullLiteral) {
1225 dataType = Null
1226 } else {
1227 return nil, Unknown, offset, UnknownValueTypeError
1228 }
1229 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
1230 dataType = Number
1231 default:
1232 return nil, Unknown, offset, UnknownValueTypeError
1233 }

Callers 3

internalGetFunction · 0.85

Calls 3

stringEndFunction · 0.85
blockEndFunction · 0.85
tokenEndFunction · 0.85