Skip to content

Commit

Permalink
detect top-level JSON arrays in zio/anyio.Reader (#3124)
Browse files Browse the repository at this point in the history
  • Loading branch information
nwt authored Sep 29, 2021
1 parent cc83c8f commit 5407d33
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion service/ztests/load-garbage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ outputs:
zson: identifier "This" must be enum and requires decorator
zng: malformed zng record
csv: auto-detection not supported
json: auto-detection not supported
json: invalid character 'T' looking for beginning of value
parquet: auto-detection not supported
zst: auto-detection not supported
status code 400: no records in request
2 changes: 1 addition & 1 deletion zio/anyio/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func lookupReader(r io.Reader, zctx *zson.Context, opts ReaderOpts) (zio.Reader,
case "zeek":
return zeekio.NewReader(r, zctx)
case "json":
return jsonio.NewReader(r, zctx)
return jsonio.NewReader(r, zctx), nil
case "zjson":
return zjsonio.NewReader(r, zctx), nil
case "zng":
Expand Down
11 changes: 10 additions & 1 deletion zio/anyio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/brimdata/zed/zio"
"github.com/brimdata/zed/zio/jsonio"
"github.com/brimdata/zed/zio/tzngio"
"github.com/brimdata/zed/zio/zeekio"
"github.com/brimdata/zed/zio/zjsonio"
Expand Down Expand Up @@ -57,6 +58,15 @@ func NewReaderWithOpts(r io.Reader, zctx *zson.Context, opts ReaderOpts) (zio.Re
}
track.Reset()

// JSON comes after ZSON because we want the ZSON reader to handle
// top-level JSON objects and the JSON reader to handle top-level
// JSON arrays.
jsonErr := match(jsonio.NewReader(track, zson.NewContext()), "json")
if jsonErr == nil {
return jsonio.NewReader(recorder, zctx), nil
}
track.Reset()

// For the matching reader, force validation to true so we are extra
// careful about auto-matching ZNG. Then, once matched, relaxed
// validation to the user setting in the actual reader returned.
Expand All @@ -77,7 +87,6 @@ func NewReaderWithOpts(r io.Reader, zctx *zson.Context, opts ReaderOpts) (zio.Re
//track.Reset()

csvErr := errors.New("csv: auto-detection not supported")
jsonErr := errors.New("json: auto-detection not supported")
parquetErr := errors.New("parquet: auto-detection not supported")
zstErr := errors.New("zst: auto-detection not supported")
return nil, joinErrs([]error{tzngErr, zeekErr, zjsonErr, zsonErr, zngErr, csvErr, jsonErr, parquetErr, zstErr})
Expand Down
2 changes: 1 addition & 1 deletion zio/anyio/ztests/fake-zng.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ outputs:
zson: zson syntax error
zng: zng type ID out of range
csv: auto-detection not supported
json: auto-detection not supported
json: invalid character '\x00' looking for beginning of value
parquet: auto-detection not supported
zst: auto-detection not supported
9 changes: 9 additions & 0 deletions zio/anyio/ztests/json-array.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
zed: '*'

input: |
[{"a":1,"b":2},{"b":3,"a":4}]
output: |
{a:1,b:2}
{a:4,b:3}
4 changes: 2 additions & 2 deletions zio/jsonio/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Reader struct {
decoder *json.Decoder
}

func NewReader(r io.Reader, zctx *zson.Context) (*Reader, error) {
func NewReader(r io.Reader, zctx *zson.Context) *Reader {
d := json.NewDecoder(r)
// Prime d's buffer so we can check for an array.
d.More()
Expand All @@ -26,7 +26,7 @@ func NewReader(r io.Reader, zctx *zson.Context) (*Reader, error) {
return &Reader{
zctx: zctx,
decoder: d,
}, nil
}
}

func (r *Reader) Read() (*zed.Record, error) {
Expand Down

0 comments on commit 5407d33

Please sign in to comment.