diff --git a/dgraph/cmd/alpha/http.go b/dgraph/cmd/alpha/http.go index e816ebd2bc0..dec263c079a 100644 --- a/dgraph/cmd/alpha/http.go +++ b/dgraph/cmd/alpha/http.go @@ -17,6 +17,7 @@ package alpha import ( + "bytes" "context" "encoding/json" "fmt" @@ -117,13 +118,27 @@ func queryHandler(w http.ResponseWriter, r *http.Request) { return } - response := map[string]interface{}{} + var out bytes.Buffer + writeEntry := func(key string, js []byte) { + out.WriteRune('"') + out.WriteString(key) + out.WriteRune('"') + out.WriteRune(':') + out.Write(js) + } e := query.Extensions{ Txn: resp.Txn, Latency: resp.Latency, } - response["extensions"] = e + js, err := json.Marshal(e) + if err != nil { + x.SetStatusWithData(w, x.Error, err.Error()) + return + } + out.WriteRune('{') + writeEntry("extensions", js) + out.WriteRune(',') // User can either ask for schema or have a query. if len(resp.Schema) > 0 { @@ -135,18 +150,16 @@ func queryHandler(w http.ResponseWriter, r *http.Request) { x.SetStatusWithData(w, x.Error, "Unable to marshal schema") return } - mp := map[string]interface{}{} - mp["schema"] = json.RawMessage(string(js)) - response["data"] = mp - } else { - response["data"] = json.RawMessage(string(resp.Json)) - } - if js, err := json.Marshal(response); err == nil { - w.Write(js) + writeEntry("data", nil) + out.WriteRune('{') + writeEntry("schema", js) + out.WriteRune('}') } else { - x.SetStatusWithData(w, x.Error, "Unable to marshal response") + writeEntry("data", resp.Json) } + out.WriteRune('}') + w.Write(out.Bytes()) } func mutationHandler(w http.ResponseWriter, r *http.Request) { diff --git a/query/outputnode.go b/query/outputnode.go index 8e39b272053..647a7058a5e 100644 --- a/query/outputnode.go +++ b/query/outputnode.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "sort" - "strconv" "strings" "time" @@ -145,10 +144,8 @@ func (fj *fastJsonNode) IsEmpty() bool { func valToBytes(v types.Val) ([]byte, error) { switch v.Tid { - case types.BinaryID: - // Encode to base64 and add "" around the value. - b := fmt.Sprintf("%q", v.Value.([]byte)) - return []byte(b), nil + case types.BinaryID, types.StringID, types.DefaultID: + return []byte(fmt.Sprintf("%q", v.Value)), nil case types.IntID: return []byte(fmt.Sprintf("%d", v.Value)), nil case types.FloatID: @@ -158,8 +155,6 @@ func valToBytes(v types.Val) ([]byte, error) { return []byte("true"), nil } return []byte("false"), nil - case types.StringID, types.DefaultID: - return []byte(strconv.Quote(v.Value.(string))), nil case types.DateTimeID: return v.Value.(time.Time).MarshalJSON() case types.GeoID: