Skip to content

Commit

Permalink
Assume Names stack is always populated (#69)
Browse files Browse the repository at this point in the history
In #65, we always populate the JSON pointer stack.
Thus, there is no need to use the fallback logic of encoding
the missing name with the member index.
  • Loading branch information
dsnet authored Dec 8, 2024
1 parent d0ded75 commit bc21dd9
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions jsontext/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ func (p Pointer) Tokens() iter.Seq[string] {
}

// appendStackPointer appends a JSON Pointer (RFC 6901) to the current value.
// The returned pointer is only accurate if s.names is populated,
// otherwise it uses the numeric index as the object member name.
//
// Invariant: Must call s.names.copyQuotedBuffer beforehand.
func (s state) appendStackPointer(b []byte) []byte {
Expand All @@ -100,23 +98,16 @@ func (s state) appendStackPointer(b []byte) []byte {
b = append(b, '/')
switch {
case e.isObject():
if objectDepth < s.Names.length() {
for _, c := range s.Names.getUnquoted(objectDepth) {
// Per RFC 6901, section 3, escape '~' and '/' characters.
switch c {
case '~':
b = append(b, "~0"...)
case '/':
b = append(b, "~1"...)
default:
b = append(b, c)
}
for _, c := range s.Names.getUnquoted(objectDepth) {
// Per RFC 6901, section 3, escape '~' and '/' characters.
switch c {
case '~':
b = append(b, "~0"...)
case '/':
b = append(b, "~1"...)
default:
b = append(b, c)
}
} else {
// Since the names stack is unpopulated, the name is unknown.
// As a best-effort replacement, use the numeric member index.
// While inaccurate, it produces a syntactically valid pointer.
b = strconv.AppendUint(b, uint64((e.Length()-1)/2), 10)
}
objectDepth++
case e.isArray():
Expand Down

0 comments on commit bc21dd9

Please sign in to comment.