Skip to content

Commit d34851c

Browse files
authored
Opinionated config for -json=sonic (#84)
1 parent 287c805 commit d34851c

File tree

4 files changed

+39
-57
lines changed

4 files changed

+39
-57
lines changed

README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ As you can see, the `-target` supports default `golang`, any git URI, or a local
2222
### Set custom template variables
2323
Change any of the following values by passing `-option="Value"` CLI flag to `webrpc-gen`.
2424

25-
| webrpc-gen -option | Default | Description | Added in |
26-
|-----------------------|------------|-----------------------------------------------------------------------------|----------|
27-
| `-pkg=<name>` | `"proto"` | package name | v0.5.0 |
28-
| `-client` | `false` | generate client code | v0.5.0 |
29-
| `-server` | `false` | generate server code | v0.5.0 |
30-
| `-types=false` | `true` | don't generate types | v0.13.0 |
31-
| `-json=github.com/bytedance/sonic`|| use [sonic](https://github.com/bytedance/sonic) for JSON encoding | v0.18.0 |
32-
| `-json=jsoniter` | `""` | use [jsoniter](https://github.com/json-iterator/go) for JSON encoding | v0.12.0 |
33-
| `-json=<pkg>` | | use other drop-in replacement JSON encoding package | v0.18.0 |
34-
| `-fixEmptyArrays` | `false` | force empty array `[]` instead of `null` in JSON (see Go [#27589][go27589]) | v0.13.0 |
35-
| `-errorStackTrace` | `false` | enables error stack traces | v0.14.0 |
36-
| `-webrpcHeader=false` | `true` | enable client send webrpc version in http headers | v0.16.0 |
25+
| webrpc-gen -option | Default | Description | Added in |
26+
|-----------------------|-----------|-----------------------------------------------------------------------------|----------|
27+
| `-pkg=<name>` | `"proto"` | package name | v0.5.0 |
28+
| `-client` | `false` | generate client code | v0.5.0 |
29+
| `-server` | `false` | generate server code | v0.5.0 |
30+
| `-types=false` | `true` | don't generate types | v0.13.0 |
31+
| `-json=sonic` | | use [sonic](https://github.com/bytedance/sonic) for JSON encoding | v0.18.0 |
32+
| `-json=jsoniter` | | use [jsoniter](https://github.com/json-iterator/go) for JSON encoding | v0.12.0 |
33+
| `-json=<pkg>` | | use alternative drop-in replacement import path for JSON encoding package | v0.18.0 |
34+
| `-fixEmptyArrays` | `false` | `encoding/json`: fix `null` arrays with reflect (see Go [#27589][go27589]) | v0.13.0 |
35+
| `-errorStackTrace` | `false` | enables error stack traces | v0.14.0 |
36+
| `-webrpcHeader=false` | `true` | enable client send webrpc version in http headers | v0.16.0 |
3737

3838
Example:
3939
```

_examples/golang-basics/example.gen.go

+14-44
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_examples/golang-basics/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package main
22

3-
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -json=github.com/bytedance/sonic -fixEmptyArrays -out=./example.gen.go
3+
//go:generate go run github.com/webrpc/webrpc/cmd/webrpc-gen -schema=example.ridl -target=../../../gen-golang -pkg=main -server -client -json=sonic -out=./example.gen.go
44

55
import (
66
"context"

imports.go.tmpl

+12
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ import (
7171
{{- /* Already imported in the stdlib section. */ -}}
7272
{{- else if eq $opts.json "jsoniter" -}}
7373
{{- set $imports "github.com/json-iterator/go" "jsoniter" -}}
74+
{{- else if eq $opts.json "sonic" -}}
75+
{{- set $imports "github.com/bytedance/sonic" "" -}}
7476
{{- else -}}
7577
{{- set $imports $opts.json "json" -}}
7678
{{- end -}}
@@ -106,6 +108,16 @@ import (
106108
{{- if eq $opts.json "jsoniter" }}
107109

108110
var json = jsoniter.ConfigCompatibleWithStandardLibrary
111+
112+
{{- else if eq $opts.json "sonic" }}
113+
114+
// Opinionated config for -json=sonic, see https://github.com/bytedance/sonic/blob/main/api.go.
115+
var json = sonic.Config{
116+
NoNullSliceOrMap: true, // Encode empty Array or Object as '[]' or '{}' instead of 'null'.
117+
CompactMarshaler: true,
118+
CopyString : true,
119+
ValidateString : true,
120+
}.Froze()
109121
{{- end -}}
110122

111123
{{- end -}}

0 commit comments

Comments
 (0)