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

Function getType

parser.go:1416–1478  ·  view source on GitHub ↗

SYS-REQ-001, SYS-REQ-027

(data []byte, offset int)

Source from the content-addressed store, hash-verified

1414
1415// SYS-REQ-001, SYS-REQ-027
1416func getType(data []byte, offset int) ([]byte, ValueType, int, error) {
1417 var dataType ValueType
1418 endOffset := offset
1419
1420 // if string value
1421 if data[offset] == '"' {
1422 dataType = String
1423 if idx, _ := stringEnd(data[offset+1:]); idx != -1 {
1424 endOffset += idx + 1
1425 } else {
1426 return nil, dataType, offset, MalformedStringError
1427 }
1428 } else if data[offset] == '[' { // if array value
1429 dataType = Array
1430 // break label, for stopping nested loops
1431 endOffset = blockEnd(data[offset:], '[', ']')
1432
1433 if endOffset == -1 {
1434 return nil, dataType, offset, MalformedArrayError
1435 }
1436
1437 endOffset += offset
1438 } else if data[offset] == '{' { // if object value
1439 dataType = Object
1440 // break label, for stopping nested loops
1441 endOffset = blockEnd(data[offset:], '{', '}')
1442
1443 if endOffset == -1 {
1444 return nil, dataType, offset, MalformedObjectError
1445 }
1446
1447 endOffset += offset
1448 } else {
1449 // Number, Boolean or None
1450 // tokenEnd returns len(data) when no delimiter is found, never -1,
1451 // so the old end == -1 guard was dead code and has been removed.
1452 end := tokenEnd(data[endOffset:])
1453
1454 value := data[offset : endOffset+end]
1455
1456 switch data[offset] {
1457 case 't', 'f': // true or false
1458 if bytes.Equal(value, trueLiteral) || bytes.Equal(value, falseLiteral) {
1459 dataType = Boolean
1460 } else {
1461 return nil, Unknown, offset, UnknownValueTypeError
1462 }
1463 case 'u', 'n': // undefined or null
1464 if bytes.Equal(value, nullLiteral) {
1465 dataType = Null
1466 } else {
1467 return nil, Unknown, offset, UnknownValueTypeError
1468 }
1469 case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-':
1470 dataType = Number
1471 default:
1472 return nil, Unknown, offset, UnknownValueTypeError
1473 }

Callers 3

internalGetFunction · 0.85

Calls 3

stringEndFunction · 0.85
blockEndFunction · 0.85
tokenEndFunction · 0.85