Skip to content

Commit 7dcf687

Browse files
authored
Merge pull request #423 from AndreiBerezin/fix-null-string
fix null after MarshalText work
2 parents ef22ec5 + 9b7ae67 commit 7dcf687

File tree

5 files changed

+59
-17
lines changed

5 files changed

+59
-17
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ generate: build
5050
./tests/intern.go \
5151
./tests/nocopy.go \
5252
./tests/escaping.go \
53-
./tests/nested_marshaler.go
53+
./tests/nested_marshaler.go \
54+
./tests/text_marshaler.go
5455
bin/easyjson -snake_case ./tests/snake.go
5556
bin/easyjson -omit_empty ./tests/omitempty.go
5657
bin/easyjson -build_tags=use_easyjson -disable_members_unescape ./benchmark/data.go

gen/encoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (g *Generator) genTypeEncoderNoCheck(t reflect.Type, in string, tags fieldT
242242

243243
// NOTE: extra check for TextMarshaler. It overrides default methods.
244244
if reflect.PtrTo(key).Implements(reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()) {
245-
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawBytesString(("+tmpVar+"Name).MarshalText()"+")"))
245+
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf("out.RawText(("+tmpVar+"Name).MarshalText()"+")"))
246246
} else if keyEnc != "" {
247247
fmt.Fprintln(g.out, ws+" "+fmt.Sprintf(keyEnc, tmpVar+"Name"))
248248
} else {

jwriter/writer.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,6 @@ func (w *Writer) RawString(s string) {
6969
w.Buffer.AppendString(s)
7070
}
7171

72-
// RawBytesString appends string from bytes to the buffer.
73-
func (w *Writer) RawBytesString(data []byte, err error) {
74-
switch {
75-
case w.Error != nil:
76-
return
77-
case err != nil:
78-
w.Error = err
79-
default:
80-
w.String(string(data))
81-
}
82-
}
83-
8472
// Raw appends raw binary data to the buffer or sets the error if it is given. Useful for
8573
// calling with results of MarshalJSON-like functions.
8674
func (w *Writer) Raw(data []byte, err error) {
@@ -104,10 +92,8 @@ func (w *Writer) RawText(data []byte, err error) {
10492
return
10593
case err != nil:
10694
w.Error = err
107-
case len(data) > 0:
108-
w.String(string(data))
10995
default:
110-
w.RawString("null")
96+
w.String(string(data))
11197
}
11298
}
11399

tests/text_marshaler.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package tests
2+
3+
import (
4+
"strings"
5+
)
6+
7+
//easyjson:json
8+
type StructWrappedTextMarshaler struct {
9+
Value namedWithTextMarshaler
10+
}
11+
type namedWithTextMarshaler string
12+
13+
func (n namedWithTextMarshaler) MarshalText() ([]byte, error) {
14+
return []byte(strings.ToUpper(string(n))), nil
15+
}

tests/text_marshaler_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tests
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mailru/easyjson"
7+
)
8+
9+
func TestStructWithTextMarshalerMarshal(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input StructWrappedTextMarshaler
13+
expected string
14+
}{
15+
{
16+
name: "Filled struct",
17+
input: StructWrappedTextMarshaler{
18+
Value: namedWithTextMarshaler("test"),
19+
},
20+
expected: `{"Value":"TEST"}`,
21+
},
22+
{
23+
name: "Empty struct",
24+
input: StructWrappedTextMarshaler{},
25+
expected: `{"Value":""}`,
26+
},
27+
}
28+
29+
for _, test := range tests {
30+
t.Run(test.name, func(t *testing.T) {
31+
marshaled, err := easyjson.Marshal(test.input)
32+
if err != nil {
33+
t.Errorf("easyjson.Marshal failed: %v", err)
34+
}
35+
if string(marshaled) != test.expected {
36+
t.Errorf("easyjson.Marshal output incorrect: got %s, want %s", string(marshaled), test.expected)
37+
}
38+
})
39+
}
40+
}

0 commit comments

Comments
 (0)