Merge pull request #423 from AndreiBerezin/fix-null-string
fix null after MarshalText work
diff --git a/Makefile b/Makefile
index 043bae8..ee7f689 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,6 @@
+VERSION := $(shell git describe --tags --always --dirty)
+COMMIT := $(shell git rev-parse --short HEAD)
+
all: test
clean:
@@ -6,7 +9,8 @@
rm -rf benchmark/*_easyjson.go
build:
- go build -o ./bin/easyjson ./easyjson
+ go build -ldflags="-s -w -X 'main.Version=$(VERSION)' -X 'main.Commit=$(COMMIT)'" -o ./bin/easyjson ./easyjson
+
generate: build
bin/easyjson -stubs \
diff --git a/easyjson/main.go b/easyjson/main.go
index d337c84..55be0ac 100644
--- a/easyjson/main.go
+++ b/easyjson/main.go
@@ -16,6 +16,11 @@
"github.com/mailru/easyjson/parser"
)
+var (
+ Version = "dev" //
+ Commit = "none"
+)
+
var buildTags = flag.String("build_tags", "", "build tags to add to generated file")
var genBuildFlags = flag.String("gen_build_flags", "", "build flags when running the generator while bootstrapping")
var snakeCase = flag.Bool("snake_case", false, "use snake_case names instead of CamelCase by default")
@@ -31,6 +36,7 @@
var processPkg = flag.Bool("pkg", false, "process the whole package instead of just the given file")
var disallowUnknownFields = flag.Bool("disallow_unknown_fields", false, "return error if any unknown field in json appeared")
var skipMemberNameUnescaping = flag.Bool("disable_members_unescape", false, "don't perform unescaping of member names to improve performance")
+var showVersion = flag.Bool("version", false, "print version and exit")
func generate(fname string) (err error) {
fInfo, err := os.Stat(fname)
@@ -98,6 +104,11 @@
files := flag.Args()
+ if *showVersion {
+ fmt.Printf("easyjson generator\nversion: %s\ncommit: %s\n", Version, Commit)
+ os.Exit(0)
+ }
+
gofile := os.Getenv("GOFILE")
if *processPkg {
gofile = filepath.Dir(gofile)
diff --git a/jwriter/writer.go b/jwriter/writer.go
index 894c198..432bade 100644
--- a/jwriter/writer.go
+++ b/jwriter/writer.go
@@ -2,7 +2,9 @@
package jwriter
import (
+ "fmt"
"io"
+ "math"
"strconv"
"unicode/utf8"
@@ -234,11 +236,19 @@
}
func (w *Writer) Float32(n float32) {
+ if w.checkIsUnsupportedFloat(float64(n)) {
+ return
+ }
+
w.Buffer.EnsureSpace(20)
w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 32)
}
func (w *Writer) Float32Str(n float32) {
+ if w.checkIsUnsupportedFloat(float64(n)) {
+ return
+ }
+
w.Buffer.EnsureSpace(20)
w.Buffer.Buf = append(w.Buffer.Buf, '"')
w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 32)
@@ -246,11 +256,19 @@
}
func (w *Writer) Float64(n float64) {
+ if w.checkIsUnsupportedFloat(n) {
+ return
+ }
+
w.Buffer.EnsureSpace(20)
w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, n, 'g', -1, 64)
}
func (w *Writer) Float64Str(n float64) {
+ if w.checkIsUnsupportedFloat(n) {
+ return
+ }
+
w.Buffer.EnsureSpace(20)
w.Buffer.Buf = append(w.Buffer.Buf, '"')
w.Buffer.Buf = strconv.AppendFloat(w.Buffer.Buf, float64(n), 'g', -1, 64)
@@ -401,3 +419,12 @@
w.Buffer.Buf = append(w.Buffer.Buf, byte(padChar), byte(padChar))
}
}
+
+func (w *Writer) checkIsUnsupportedFloat(val float64) bool {
+ isUnsupported := math.IsNaN(val) || math.IsInf(val, 0)
+ if isUnsupported && w.Error == nil {
+ w.Error = fmt.Errorf("json: unsupported value: %v", val)
+ }
+
+ return isUnsupported
+}
diff --git a/tests/errors.go b/tests/errors.go
index 14360fc..2a545d1 100644
--- a/tests/errors.go
+++ b/tests/errors.go
@@ -24,3 +24,9 @@
//easyjson:json
type ErrorIntMap map[uint32]string
+
+//easyjson:json
+type ErrorFloatTypes struct {
+ Float64 float64 `json:"float64"`
+ Float32 float32 `json:"float32"`
+}
diff --git a/tests/errors_test.go b/tests/errors_test.go
index 40fa335..756ef11 100644
--- a/tests/errors_test.go
+++ b/tests/errors_test.go
@@ -1,6 +1,7 @@
package tests
import (
+ "math"
"testing"
"github.com/mailru/easyjson/jlexer"
@@ -283,3 +284,40 @@
}
}
}
+
+func TestUnsupportedFloatValues(t *testing.T) {
+ for i, test := range []struct {
+ Value ErrorFloatTypes
+ ExpectedErr string
+ }{
+ {
+ Value: ErrorFloatTypes{Float64: math.NaN()},
+ ExpectedErr: "json: unsupported value: NaN",
+ },
+ {
+ Value: ErrorFloatTypes{Float64: math.Inf(1)},
+ ExpectedErr: "json: unsupported value: +Inf",
+ },
+ {
+ Value: ErrorFloatTypes{Float64: math.Inf(-1)},
+ ExpectedErr: "json: unsupported value: -Inf",
+ },
+ {
+ Value: ErrorFloatTypes{Float32: float32(math.NaN())},
+ ExpectedErr: "json: unsupported value: NaN",
+ },
+ {
+ Value: ErrorFloatTypes{Float32: float32(math.Inf(1))},
+ ExpectedErr: "json: unsupported value: +Inf",
+ },
+ {
+ Value: ErrorFloatTypes{Float32: float32(math.Inf(-1))},
+ ExpectedErr: "json: unsupported value: -Inf",
+ },
+ } {
+ _, err := test.Value.MarshalJSON()
+ if err == nil || err.Error() != test.ExpectedErr {
+ t.Errorf("[%d] TestUnsupportedFloatValues(): error: want %s, got %v", i, test.ExpectedErr, err)
+ }
+ }
+}