goccy / go-json

Fast JSON encoder/decoder compatible with encoding/json for Go
MIT License
3.12k stars 148 forks source link

Unexpected error expected colon after object key #456

Closed ccaneke closed 1 year ago

ccaneke commented 1 year ago

When calling Decode like this


for j.decoder.More() {
        if err = j.decoder.Decode(dst); err != nil {
            return j.count, err
        }
}       

I get the following error of type *errors.SyntaxError:

expected colon after object key

Cause of the error:

When the buffer is filled and being decoded it looks like this:

image

Then the stream cursor is on the nul byte: image

so the next n bytes are read into the stream's buffer by calling s.read(): image

Then s.read() returns false because of the error: http2: response body closed (Client.Timeout or context cancellation while reading body).

s.cursor is at byte 58 (:) but c (nul byte 0) is returned, maybe because s.read() was false: image

Then the error occurs: image

In short, the error happens because s.skipWhiteSpace() returns 0 (null) when it should return : after the key "eventDeliverySchema" is read.

To recreate this error, decode the json array below using these steps:

  1. Skip tokens until you get to the array. You can call j.decoder.Token() in a loop to skip the first n tokens (j.decoder holds a *json.Decoder value).
  2. decode array elements using the decoding code above

go-json version: github.com/goccy/go-json v0.10.0

json:

{"value":[{"properties":{"topic":"/subscriptions/134245-21f34-21gs/resourceGroups/ifm-dev/providers/Microsoft.EventGrid/topics/EG-WHINT-Dev-Topic","provisioningState":"Succeeded","destination":{"properties":{"resourceId":"/subscriptions/134245-21f34-21gs/resourceGroups/ifm-dev/providers/Microsoft.ServiceBus/namespaces/WHINT-Dev/topics/whint-topic-test"},"endpointType":"ServiceBusTopic"},"filter":{"subjectBeginsWith":"","subjectEndsWith":"","enableAdvancedFilteringOnArrays":true},"labels":[],"eventDeliverySchema":"EventGridSchema","retryPolicy":{"maxDeliveryAttempts":30,"eventTimeToLiveInMinutes":1440}},"systemData":null,"id":"/subscriptions/134245-21f34-21gs/resourceGroups/ifm-dev/providers/Microsoft.EventGrid/topics/EG-WHINT-Dev-Topic/providers/Microsoft.EventGrid/eventSubscriptions/EG-WHINT-Dev-EventSub","name":"EG-WHINT-Dev-EventSub","type":"Microsoft.EventGrid/eventSubscriptions"},{"properties":{"topic":"/subscriptions/134245-21f34-21gs/resourceGroups/ifm-dev/providers/Microsoft.EventGrid/topics/EG-WHINT-Dev-Topic","provisioningState":"Succeeded","destination":{"properties":{"resourceId":"/subscriptions/134245-21f34-21gs/resourceGroups/ifm-dev/providers/Microsoft.ServiceBus/namespaces/WHINT-SB-DEV/queues/whint-sb-queue-dev"},"endpointType":"ServiceBusQueue"},"filter":{"subjectBeginsWith":"","subjectEndsWith":"","enableAdvancedFilteringOnArrays":true},"labels":[],"eventDeliverySchema":"EventGridSchema","retryPolicy":{"maxDeliveryAttempts":30,"eventTimeToLiveInMinutes":1440}},"systemData":null,"id":"/subscriptions/134245-21f34-21gs/resourceGroups/ifm-dev/providers/Microsoft.EventGrid/topics/EG-WHINT-Dev-Topic/providers/Microsoft.EventGrid/eventSubscriptions/EG-WHINT-Dev-EventSub-queue","name":"EG-WHINT-Dev-EventSub-queue","type":"Microsoft.EventGrid/eventSubscriptions"}]}