Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: move json_marshal to basic_feature #748

Merged
merged 4 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 0 additions & 80 deletions content/en/docs/hertz/reference/json.md

This file was deleted.

52 changes: 52 additions & 0 deletions content/en/docs/hertz/tutorials/basic-feature/json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: "JSON Marshal Library"
linkTitle: "JSON Marshal Library"
date: 2023-08-03
weight: 19
description: >
---

By default, Hertz integrates with and uses [Sonic](https://github.com/bytedance/sonic) for serializing `ctx.JSON` interface and deserialization requests as defined in the `binding` package.
Sonic is an ultra-high performance golang json library, also see Sonic [README](https://github.com/bytedance/sonic) for details.

The following are requirements to enable Sonic:

- Go 1.16 or above
- Linux / darwin OS / Windows
- Amd64 CPU with AVX instruction set

Sonic automatically fallback to golang's `encoding/json` library when the above requirements have not been satisfied.

## Bringing Your Own JSON Marshal Library

If Sonic does not meet your needs, you may provide your own implementation by calling `ResetJSONMarshal` for render and `ResetJSONUnmarshaler` for binding.

```go
import (
"encoding/json"

"github.com/bytedance/go-tagexpr/v2/binding"
"github.com/cloudwego/hertz/pkg/app/server/render"
)

func main() {
// Render
render.ResetJSONMarshal(json.Marshal)

// Binding
binding.ResetJSONUnmarshaler(json.Unmarshal)
}
```

## Conditional Compilation

Hertz supports conditional compilation to control the actual json library used, you can use `-tags stdjson` to choose to use the standard library.

```go
go build -tags stdjson
```

## Sonic related issues

If there are issues related to Sonic, please refer to Sonic [README](https://github.com/bytedance/sonic) or propose an [issue](https://github.com/bytedance/sonic/issues).

80 changes: 0 additions & 80 deletions content/zh/docs/hertz/reference/json.md

This file was deleted.

49 changes: 49 additions & 0 deletions content/zh/docs/hertz/tutorials/basic-feature/json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
title: "JSON Marshal 库"
linkTitle: "JSON Marshal 库"
date: 2023-08-03
weight: 19
description: >
---

Hertz 默认集成并使用 [Sonic](https://github.com/bytedance/sonic) 用于序列化 `ctx.JSON` 接口,以及反序列化 `binding` 包中的请求。Sonic 是一款超高性能 golang json 库,详情参考 Sonic [README](https://github.com/bytedance/sonic) 。

开启 Sonic 需要满足以下条件:

- Go 1.16 以上
- Linux / darwin OS / Windows
- Amd64 CPU with AVX instruction set

当上述条件不能满足时,Sonic 会自动 fallback 到 golang 的 encoding/json 库。

## 自定义 JSON Marshall 库

如果 Sonic 不能够满足您的需求,你可以使用以下方式自定义 json marshal 库的实现:

```go
import (
"encoding/json"

"github.com/bytedance/go-tagexpr/v2/binding"
"github.com/cloudwego/hertz/pkg/app/server/render"
)
func main() {
// Render
li-jin-gou marked this conversation as resolved.
Show resolved Hide resolved
render.ResetJSONMarshal(json.Marshal)

// Binding
binding.ResetJSONUnmarshaler(json.Unmarshal)
}
```

## 条件编译

Hertz 支持条件编译来控制实际使用的 json 库,你可以通过 `-tags stdjson` 来选择使用标准库。

```go
go build -tags stdjson
```

## Sonic 相关问题

若出现与 Sonic 相关的问题,可参考 Sonic [README](https://github.com/bytedance/sonic) 或提 [issue](https://github.com/bytedance/sonic/issues) 解决。
Loading