Skip to content

Commit

Permalink
handle unexpected token
Browse files Browse the repository at this point in the history
  • Loading branch information
sumukhbhat2701 authored and nelio2k committed May 2, 2024
1 parent 9c0fcfc commit 4570c90
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions jsonComposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
ErrInvalidJSON error = fmt.Errorf("invalid JSON object")
ErrUnexpectedEOF error = fmt.Errorf("unexpected EOF")
ErrInsufficientMemory error = fmt.Errorf("insufficient memory allocated for dst, cannot proceed")
ErrUnrecognisedToken error = fmt.Errorf("unrecognised token in the JSON object")
)

type jsonObjComposer struct {
Expand Down Expand Up @@ -101,7 +102,7 @@ func MatchAndRemoveItemsFromJsonObject(src []byte, remove []string, dst []byte,
}
tknType = tknType1

switch tknType {
switch tknType1 {
case tknString:
// string token can be a JSON key, need to process
case tknObjectStart:
Expand All @@ -127,9 +128,11 @@ func MatchAndRemoveItemsFromJsonObject(src []byte, remove []string, dst []byte,
continue
case tknEnd:
continue
case tknUnknown:
return handleError(ErrUnrecognisedToken)
default:
// can be tknObjectKeyDelim, tknListDelim, tknEscString, tknInteger, tknNumber,
// tknNull, tknTrue, tknFalse, tknUnknown
// tknNull, tknTrue, tknFalse
err = composer.Write(potentialKey, tknType1)
if err != nil {
return handleError(err)
Expand Down Expand Up @@ -161,6 +164,10 @@ func MatchAndRemoveItemsFromJsonObject(src []byte, remove []string, dst []byte,
tknType = tknType2

if tknType2 != tknObjectKeyDelim {
if tknType2 == tknUnknown {
return handleError(ErrUnrecognisedToken)
}

// potentialKey is not a JSON key
err = composer.Write(potentialKey, tknType1)
if err != nil {
Expand Down Expand Up @@ -197,6 +204,8 @@ func MatchAndRemoveItemsFromJsonObject(src []byte, remove []string, dst []byte,
valEnd = tokenizer.Position()

switch tknType3 {
case tknUnknown:
return handleError(ErrUnrecognisedToken)
case tknObjectStart:
fallthrough
case tknArrayStart:
Expand Down Expand Up @@ -229,7 +238,7 @@ func MatchAndRemoveItemsFromJsonObject(src []byte, remove []string, dst []byte,
case tknEnd:
return handleError(ErrUnexpectedEOF)
default:
// can be tknListDelim, tknObjectKeyDelim, tknUnknown
// can be tknListDelim, tknObjectKeyDelim
}
}
removed[keyToRemove] = src[valStart:valEnd]
Expand All @@ -239,7 +248,7 @@ func MatchAndRemoveItemsFromJsonObject(src []byte, remove []string, dst []byte,
// if it is tknListDelim, don't write it
tknType4, tkn, _, err = tokenizer.Step()
if err != nil || (tknType4 != tknObjectEnd && tknType4 != tknListDelim) {
err = fmt.Errorf("error stepping to next token, expecting separator or objectEnd, got=%s, src=%s, pos=%v", tkn, src, tokenizer.Position())
err = fmt.Errorf("error stepping to next token, expecting separator or objectEnd, got=%s, src=%s, pos=%v, err=%v", tkn, src, tokenizer.Position(), err)
return handleError(err)
}
tknType = tknType4
Expand All @@ -253,6 +262,8 @@ func MatchAndRemoveItemsFromJsonObject(src []byte, remove []string, dst []byte,
if depth < 0 {
return handleError(ErrInvalidJSON)
}
} else if tknType4 == tknUnknown {
return handleError(ErrUnrecognisedToken)
}
}

Expand Down

0 comments on commit 4570c90

Please sign in to comment.