Merge pull request #423 from AndreiBerezin/fix-null-string
fix null after MarshalText work
diff --git a/Makefile b/Makefile
index 44d3434..ee7f689 100644
--- a/Makefile
+++ b/Makefile
@@ -50,7 +50,8 @@
./tests/intern.go \
./tests/nocopy.go \
./tests/escaping.go \
- ./tests/nested_marshaler.go
+ ./tests/nested_marshaler.go \
+ ./tests/text_marshaler.go
bin/easyjson -snake_case ./tests/snake.go
bin/easyjson -omit_empty ./tests/omitempty.go
bin/easyjson -build_tags=use_easyjson -disable_members_unescape ./benchmark/data.go
diff --git a/gen/encoder.go b/gen/encoder.go
index 22db5e9..ed6d6ad 100644
--- a/gen/encoder.go
+++ b/gen/encoder.go
@@ -242,7 +242,7 @@
// NOTE: extra check for TextMarshaler. It overrides default methods.
if reflect.PtrTo(key).Implements(reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()) {
- fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawBytesString(("+tmpVar+"Name).MarshalText()"+")"))
+ fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawText(("+tmpVar+"Name).MarshalText()"+")"))
} else if keyEnc != "" {
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf(keyEnc, tmpVar+"Name"))
} else {
diff --git a/jwriter/writer.go b/jwriter/writer.go
index 6f1403a..432bade 100644
--- a/jwriter/writer.go
+++ b/jwriter/writer.go
@@ -69,18 +69,6 @@
w.Buffer.AppendString(s)
}
-// RawBytesString appends string from bytes to the buffer.
-func (w *Writer) RawBytesString(data []byte, err error) {
- switch {
- case w.Error != nil:
- return
- case err != nil:
- w.Error = err
- default:
- w.String(string(data))
- }
-}
-
// Raw appends raw binary data to the buffer or sets the error if it is given. Useful for
// calling with results of MarshalJSON-like functions.
func (w *Writer) Raw(data []byte, err error) {
@@ -104,10 +92,8 @@
return
case err != nil:
w.Error = err
- case len(data) > 0:
- w.String(string(data))
default:
- w.RawString("null")
+ w.String(string(data))
}
}
diff --git a/tests/text_marshaler.go b/tests/text_marshaler.go
new file mode 100644
index 0000000..8d3b51e
--- /dev/null
+++ b/tests/text_marshaler.go
@@ -0,0 +1,15 @@
+package tests
+
+import (
+ "strings"
+)
+
+//easyjson:json
+type StructWrappedTextMarshaler struct {
+ Value namedWithTextMarshaler
+}
+type namedWithTextMarshaler string
+
+func (n namedWithTextMarshaler) MarshalText() ([]byte, error) {
+ return []byte(strings.ToUpper(string(n))), nil
+}
diff --git a/tests/text_marshaler_test.go b/tests/text_marshaler_test.go
new file mode 100644
index 0000000..33a63ee
--- /dev/null
+++ b/tests/text_marshaler_test.go
@@ -0,0 +1,40 @@
+package tests
+
+import (
+ "testing"
+
+ "github.com/mailru/easyjson"
+)
+
+func TestStructWithTextMarshalerMarshal(t *testing.T) {
+ tests := []struct {
+ name string
+ input StructWrappedTextMarshaler
+ expected string
+ }{
+ {
+ name: "Filled struct",
+ input: StructWrappedTextMarshaler{
+ Value: namedWithTextMarshaler("test"),
+ },
+ expected: `{"Value":"TEST"}`,
+ },
+ {
+ name: "Empty struct",
+ input: StructWrappedTextMarshaler{},
+ expected: `{"Value":""}`,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ marshaled, err := easyjson.Marshal(test.input)
+ if err != nil {
+ t.Errorf("easyjson.Marshal failed: %v", err)
+ }
+ if string(marshaled) != test.expected {
+ t.Errorf("easyjson.Marshal output incorrect: got %s, want %s", string(marshaled), test.expected)
+ }
+ })
+ }
+}