Skip to content

Commit

Permalink
Use 0 for zero value json.Number and check against empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost committed Sep 30, 2024
1 parent 07a8540 commit eaf6fc4
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 14 deletions.
1 change: 1 addition & 0 deletions _generated/def.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,5 @@ type NumberJSONSample struct {
Single json.Number
Array []json.Number
Map map[string]json.Number
OE json.Number `msg:",omitempty"`
}
2 changes: 1 addition & 1 deletion gen/elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ func (s *BaseElem) ZeroExpr() string {
case Time:
return "(time.Time{})"
case JsonNumber:
return "(json.Number{})"
return `""`

}

Expand Down
5 changes: 0 additions & 5 deletions msgp/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -1306,11 +1306,6 @@ func (m *Reader) ReadJSONNumber() (n json.Number, err error) {
return json.Number(strconv.FormatFloat(v, 'f', -1, 64)), nil
}
return "", err
case StrType:
v, err := m.ReadString()
if err == nil {
return json.Number(v), nil
}
}
return "", TypeError{Method: NumberType, Encoded: t}
}
Expand Down
4 changes: 0 additions & 4 deletions msgp/read_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1348,9 +1348,5 @@ func ReadJSONNumberBytes(b []byte) (number json.Number, o []byte, err error) {
if err == nil {
return json.Number(strconv.FormatFloat(f, 'f', -1, 64)), o, nil
}
s, o, err := ReadStringBytes(b)
if err == nil {
return json.Number(s), o, nil
}
return "", nil, TypeError{Method: NumberType, Encoded: getType(b[0])}
}
4 changes: 2 additions & 2 deletions msgp/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ func (mw *Writer) WriteTime(t time.Time) error {
// WriteJSONNumber writes the json.Number to the stream as either integer or float.
func (mw *Writer) WriteJSONNumber(n json.Number) error {
if n == "" {
// Allow writing the empty string to cover the zero value.
return mw.push(wfixstr(uint8(0)))
// The zero value outputs the 0 integer.
return mw.push(0)
}
ii, err := n.Int64()
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions msgp/write_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ func AppendIntf(b []byte, i interface{}) ([]byte, error) {
// An error will be returned if the json.Number returns error as both integer and float.
func AppendJSONNumber(b []byte, n json.Number) ([]byte, error) {
if n == "" {
// Allow writing the empty string to cover the zero value.
return append(b, mfixstr), nil
// The zero value outputs the 0 integer.
return append(b, 0), nil
}
ii, err := n.Int64()
if err == nil {
Expand Down

0 comments on commit eaf6fc4

Please sign in to comment.