99designs / 99designs/gqlgen

encoding/json/v2 package

Đang mở
#3,683 6 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Go
Star
10.8k
Fork
1.3k
Merge trung bình
2 ngày 36 phút
Pull request đã merge (30 ngày)
26

Mô tả

The `encoding/json/v2` package release date is yet to be determined, but I think it’s a good time to start thinking about `encoding/json/v2` package and its impact on gqlgen.

The `encoding/json/v2` package is planned to provide functionality compatible with the `encoding/json` package, and it seems that the internals of the existing `encoding/json` package will be built using the `encoding/json/v2` package.
This means that gqlgen can benefit from performance improvements without requiring any changes. So far, there are no issues.

The problem arises when gqlgen directly uses the `encoding/json/v2` package. I would like to clarify the benefits and issues that may occur in that case.

## Reference
https://github.com/golang/go/issues/71497
https://pkg.go.dev/github.com/go-json-experiment/json

### Additional Information (edit: added by Steve Coffman)

In #2842 there was a proposal to allow a pluggable JSON implementation for gqlgen. Currently the JSON marshalling cannot be easily replaced with a different implementation such as`encoding/json/v2` (or other alternatives). We would like to be able to allow this to be a choice.

As in that proposal, we would add to gqlgen an API that accepts a custom implementation supplied directly to the transport at fields:

```go
type Decoder interface {
Decode(v interface{}) error
UseNumber()
}

type Json interface {
Marshal(v interface{}) ([]byte, error)
NewDecoder(r io.Reader) Decoder
}

type POST struct {
// Map of all headers that are added to graphql response. If not
// set, only one header: Content-Type: application/json will be set.
ResponseHeaders map[string][]string
Json Json // Json being an interface so it can be implemented however the user wants
}
```

The library can then supply a default implementation based on `encoding/json`:

```go
var DefaultJson Json = jsonImpl{}

type jsonImpl struct{}

func (jsonImpl) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

func (jsonImpl) NewDecoder(r io.Reader) Decoder {
return json.NewDecoder(r)
}
```

Since there already exists [a file](https://github.com/99designs/gqlgen/blob/master/graphql/handler/transport/util.go) that handles most JSON marshalling, it can be updated to accept that JSON implementation and fallback if one is not supplied:

```go
func writeJson(json Json, w io.Writer, response *graphql.Response) {
if json == nil {
json = DefaultJson
}
b, err := json.Marshal(response)
if err != nil {
panic(err)
}
w.Write(b)
}
```

This will add a slight overhead of checking for the implementation, but will not break existing codebases.

This will also allow us to easily add a go version build tag when `encoding/json/v2` is released, such that we can immediately upgrade to it if that Go version is available.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.