Skip to content

Commit

Permalink
codec: json: decode json null as a string
Browse files Browse the repository at this point in the history
Previously, we only treated "..." as a string. Now we also look for
and handle null as a string, and return an empty string or a nil byte,
depending on the context.

Fixes #182 #180
  • Loading branch information
ugorji committed Nov 30, 2016
1 parent 8b94642 commit 9c7f9b7
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions codec/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,11 @@ func (d *jsonDecDriver) DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut [
if isstring {
return d.bs
}
// if appendStringAsBytes returned a zero-len slice, then treat as nil.
// This should only happen for null, and "".
if len(d.bs) == 0 {
return nil
}
bs0 := d.bs
slen := base64.StdEncoding.DecodedLen(len(bs0))
if slen <= cap(bs) {
Expand Down Expand Up @@ -968,6 +973,14 @@ func (d *jsonDecDriver) appendStringAsBytes() {
}
d.tok = b
}

// handle null as a string
if d.tok == 'n' {
d.readStrIdx(10, 13) // ull
d.bs = d.bs[:0]
return
}

if d.tok != '"' {
d.d.errorf("json: expect char '%c' but got char '%c'", '"', d.tok)
}
Expand Down

0 comments on commit 9c7f9b7

Please sign in to comment.