You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varstackbuf [unescapeStackBufSize]byte// stack-allocated array for allocation-free unescaping of small strings
546
+
offset:=0
547
+
548
+
// Descend to the desired key, if requested
549
+
iflen(keys) >0 {
550
+
ifoff:=searchKeys(data, keys...); off==-1 {
551
+
returnKeyPathNotFoundError
552
+
} else {
553
+
offset=off
554
+
}
555
+
}
556
+
557
+
// Validate and skip past opening brace
558
+
ifoff:=nextToken(data[offset:]); off==-1 {
559
+
returnMalformedObjectError
560
+
} elseifoffset+=off; data[offset] !='{' {
561
+
returnMalformedObjectError
562
+
} else {
563
+
offset++
564
+
}
565
+
566
+
// Skip to the first token inside the object, or stop if we find the ending brace
567
+
ifoff:=nextToken(data[offset:]); off==-1 {
568
+
returnMalformedJsonError
569
+
} elseifoffset+=off; data[offset] =='}' {
570
+
returnnil
571
+
}
572
+
573
+
// Loop pre-condition: data[offset] points to what should be either the next entry's key, or the closing brace (if it's anything else, the JSON is malformed)
574
+
foroffset<len(data) {
575
+
// Step 1: find the next key
576
+
varkey []byte
577
+
578
+
// Check what the the next token is: start of string, end of object, or something else (error)
579
+
switchdata[offset] {
580
+
case'"':
581
+
offset++// accept as string and skip opening quote
582
+
case'}':
583
+
returnnil// we found the end of the object; stop and return success
// Step 4: skip over the next comma to the following token, or stop if we hit the ending brace
625
+
ifoff:=nextToken(data[offset:]); off==-1 {
626
+
returnMalformedArrayError
627
+
} else {
628
+
offset+=off
629
+
switchdata[offset] {
630
+
case'}':
631
+
returnnil// Stop if we hit the close brace
632
+
case',':
633
+
offset++// Ignore the comma
634
+
default:
635
+
returnMalformedObjectError
636
+
}
637
+
}
638
+
639
+
// Skip to the next token after the comma
640
+
ifoff:=nextToken(data[offset:]); off==-1 {
641
+
returnMalformedArrayError
642
+
} else {
643
+
offset+=off
644
+
}
645
+
}
646
+
647
+
returnMalformedObjectError// we shouldn't get here; it's expected that we will return via finding the ending brace
648
+
}
649
+
522
650
// GetUnsafeString returns the value retrieved by `Get`, use creates string without memory allocation by mapping string to slice memory. It does not handle escape symbols.
0 commit comments