From 67e6f91d3f998c19d0a23d292852af2589a03d15 Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Wed, 5 Apr 2017 16:28:08 +0200 Subject: [PATCH] use encoding/json to encode scalars There are some edge cases that are better handled by the proven encoder of encoding/json, for example special characters in strings. --- internal/exec/exec.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/internal/exec/exec.go b/internal/exec/exec.go index e6f1a4ebd0..f529bc2494 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -3,8 +3,8 @@ package exec import ( "bytes" "context" + "encoding/json" "reflect" - "strconv" "sync" "github.com/neelance/graphql-go/errors" @@ -261,24 +261,12 @@ func (r *Request) execSelectionSet(ctx context.Context, sels []selected.Selectio out.WriteByte(']') case *schema.Scalar: - var b []byte // TODO use scratch - switch t.Name { - case "Int": - out.Write(strconv.AppendInt(b, resolver.Int(), 10)) - case "Float": - out.Write(strconv.AppendFloat(b, resolver.Float(), 'f', -1, 64)) - case "String": - out.Write(strconv.AppendQuote(b, resolver.String())) - case "Boolean": - out.Write(strconv.AppendBool(b, resolver.Bool())) - default: - v := resolver.Interface().(marshaler) - data, err := v.MarshalJSON() - if err != nil { - panic(errors.Errorf("could not marshal %v", v)) - } - out.Write(data) + v := resolver.Interface() + data, err := json.Marshal(v) + if err != nil { + panic(errors.Errorf("could not marshal %v", v)) } + out.Write(data) case *schema.Enum: out.WriteByte('"')