diff --git a/gen/decoder.go b/gen/decoder.go index 82558a7..80f8d2c 100644 --- a/gen/decoder.go +++ b/gen/decoder.go @@ -68,8 +68,8 @@ func (g *Generator) genTypeDecoder(t reflect.Type, out string, tags fieldTags, i unmarshalerIface = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem() if reflect.PtrTo(t).Implements(unmarshalerIface) { - fmt.Fprintln(g.out, ws+"if data := in.UnsafeString(); in.Ok() {") - fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText([]byte(data)) )") + fmt.Fprintln(g.out, ws+"if data := in.UnsafeBytes(); in.Ok() {") + fmt.Fprintln(g.out, ws+" in.AddError( ("+out+").UnmarshalText(data) )") fmt.Fprintln(g.out, ws+"}") return nil } diff --git a/jlexer/lexer.go b/jlexer/lexer.go index ae8344e..d4c16f1 100644 --- a/jlexer/lexer.go +++ b/jlexer/lexer.go @@ -618,6 +618,12 @@ func (r *Lexer) UnsafeString() string { return ret } +// UnsafeBytes returns the byte slice if the token is a string literal. +func (r *Lexer) UnsafeBytes() []byte { + _, ret := r.unsafeString() + return ret +} + // String reads a string literal. func (r *Lexer) String() string { if r.token.kind == tokenUndef && r.Ok() {