Skip to content

Commit f2cd277

Browse files
committed
doc: update documents for Golang user
1 parent 7ffd0ae commit f2cd277

File tree

7 files changed

+209
-20
lines changed

7 files changed

+209
-20
lines changed

README.md

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,47 @@ The main optimization in sonic-rs is the use of SIMD. However, we do not use the
2121

2222
More details about optimization can be found in [performance.md](docs/performance.md).
2323

24+
***For Golang user to use `sonic_rs`, please see [for_Golang_user_zh.md](docs/for_Golang_user.md)***
25+
26+
- [sonic-rs](#sonic-rs)
27+
- [Requirements/Notes](#requirementsnotes)
28+
- [Quick to use sonic-rs](#quick-to-use-sonic-rs)
29+
- [Features](#features)
30+
- [Benchmark](#benchmark)
31+
- [Deserialize Struct](#deserialize-struct)
32+
- [Deserialize Untyped](#deserialize-untyped)
33+
- [Serialize Untyped](#serialize-untyped)
34+
- [Serialize Struct](#serialize-struct)
35+
- [Get from JSON](#get-from-json)
36+
- [Usage](#usage)
37+
- [Serde into Rust Type](#serde-into-rust-type)
38+
- [Get a field from JSON](#get-a-field-from-json)
39+
- [Parse and Serialize into untyped Value](#parse-and-serialize-into-untyped-value)
40+
- [JSON Iterator](#json-iterator)
41+
- [JSON RawValue & Number & RawNumber](#json-rawvalue--number--rawnumber)
42+
- [Error handle](#error-handle)
43+
- [FAQs](#faqs)
44+
- [About UTF-8](#about-utf-8)
45+
- [About floating point precision](#about-floating-point-precision)
46+
- [Acknowledgement](#acknowledgement)
47+
- [Contributing](#contributing)
48+
2449
## Requirements/Notes
2550

2651
1. Support x86_64 or aarch64. Note that the performance in aarch64 is lower and needs optimization.
2752
2. Requires Rust nightly version, as we use the `packed_simd` crate.
53+
3. please add the compile options `-C target-cpu=native`
54+
55+
## Quick to use sonic-rs
56+
57+
To ensure that SIMD instruction is used in sonic-rs, you need to add rustflags `-C target-cpu=native` and compile on the host machine. For example, Rust flags can be configured in Cargo [config](.cargo/config).
58+
59+
Add sonic-rs in `Cargo.toml`
60+
61+
```
62+
[dependencies]
63+
sonic-rs = 0.2
64+
```
2865

2966
## Features
3067
1. Serde into Rust struct as `serde_json` and `serde`.
@@ -39,15 +76,6 @@ More details about optimization can be found in [performance.md](docs/performanc
3976

4077
6. The floating parsing percision is as Rust std in default.
4178

42-
## Quick to use sonic-rs
43-
44-
To ensure that SIMD instruction is used in sonic-rs, you need to add rustflags `-C target-cpu=native` and compile on the host machine. For example, Rust flags can be configured in Cargo [config](.cargo/config).
45-
46-
Add sonic-rs in `Cargo.toml`
47-
```
48-
[dependencies]
49-
sonic-rs = 0.2
50-
```
5179

5280
## Benchmark
5381

README_ZH.md

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,36 @@ sonic-rs 的主要优化是使用 SIMD。然而,sonic-rs 没有使用来自`si
2121

2222
有关优化的更多细节,请参见 [performance_zh.md](docs/performance_zh.md)
2323

24+
***对于 Golang 用户迁移 Rust 使用 `sonic_rs`, 请参考 [for_Golang_user.md](docs/for_Golang_user_zh.md)***
25+
26+
- [sonic-rs](#sonic-rs)
27+
- [要求/注意事项](#要求注意事项)
28+
- [如何使用 sonic-rs](#如何使用-sonic-rs)
29+
- [功能](#功能)
30+
- [基准测试](#基准测试)
31+
- [解析到结构体](#解析到结构体)
32+
- [解析到document](#解析到-document)
33+
- [序列化document](#序列化-document)
34+
- [序列化Rust结构体](#序列化-rust-结构体)
35+
- [从JSON中获取](#从-json-中获取)
36+
- [用法](#用法)
37+
- [对Rust类型解析/序列化](#对-rust-类型解析序列化)
38+
- [从JSON中获取字段](#从-json-中获取字段)
39+
- [解析/序列化document](#解析序列化-document)
40+
- [JSON Iterator](#json-iterator)
41+
- [JSON RawValue & Number & RawNumber](#json-rawvalue--number--rawnumber)
42+
- [错误处理](#错误处理)
43+
- [常见问题](#常见问题)
44+
- [关于UTF-8](#关于-utf-8)
45+
- [关于浮点数精度](#关于浮点数精度)
46+
- [致谢](#致谢)
47+
- [如何贡献](#如何贡献)
48+
2449
## ***要求/注意事项***
2550

2651
1. 支持 x86_64 或 aarch64,aarch64 的性能较低,需要优化。
2752
2. 需要 Rust nightly 版本,因为 sonic-rs 使用了 `packed_simd` 包。
28-
29-
## 功能
30-
31-
1. JSON 与 Rust 结构体之间的序列化,基于兼容 `serde_json``serde`
32-
2. JSON 与 document 之间的序列化,document是可变数据结构
33-
3. 从 JSON 中获取特定字段
34-
4. 将 JSON 解析为惰性迭代器
35-
5. 在默认情况下支持 `RawValue``Number``RawNumber`(就像 Golang 的 `JsonNumber`)。
36-
6. 浮点数精度默认和 Rust 标准库对齐
53+
3. 在编译选项中开启 `-C target-cpu=native`
3754

3855
## 如何使用 sonic-rs
3956

@@ -45,6 +62,15 @@ sonic-rs 的主要优化是使用 SIMD。然而,sonic-rs 没有使用来自`si
4562
sonic-rs = 0.2
4663
```
4764

65+
## 功能
66+
67+
1. JSON 与 Rust 结构体之间的序列化,基于兼容 `serde_json``serde`
68+
2. JSON 与 document 之间的序列化,document是可变数据结构
69+
3. 从 JSON 中获取特定字段
70+
4. 将 JSON 解析为惰性迭代器
71+
5. 在默认情况下支持 `RawValue``Number``RawNumber`(就像 Golang 的 `JsonNumber`)。
72+
6. 浮点数精度默认和 Rust 标准库对齐
73+
4874

4975
## 基准测试
5076

docs/for_Golang_user.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Golang to Rust migration
2+
3+
Current version:
4+
5+
`sonic-rs = "0.2.4"`
6+
7+
Corresponding API references:
8+
9+
- Parsing into Golang structures or strong types:
10+
11+
sonic-go/encoding-json Unmarshal => sonic_rs::from_str/from_slice
12+
13+
sonic-go/encoding-json Marshal => sonic_rs::to_string/to_vec, etc.
14+
15+
- Parsing into Golang interface{}/any or sonic-go AST:
16+
17+
If it is a standalone `interface{}`, it is recommended to use `sonic_rs::Document` for better performance.
18+
19+
If it is an `interface{}` inside a Golang structure, it is recommended to replace `interface{}/any` with `serde_json::Value`. Note: `sonic_rs::Value` and `sonic_rs::Document` are not currently supported for embedding into Rust structures but will be supported later.
20+
21+
- Using gjson/jsonparser for on-demand parsing:
22+
23+
Regarding gjson/jsonparser get API:
24+
25+
The gjson/jsonparser get API itself does not perform strict JSON validation, so you can use `sonic_rs::get_unchecked` for replacement. The sonic_rs get API will return a `Result<LazyValue>`, `LazyValue` can be further ***parsed into the corresponding type** by using `as_bool, as_str`, etc. If you need to get the original raw JSON, ***without parsing***, please use `as_raw_str, as_raw_slice` API. Refer to the example: [get_from.rs](examples/get_from.rs)
26+
27+
Regarding jsonparser `ArrayEach` and `ObjectEach` API:
28+
29+
The gjson/jsonparser get API itself does not perform strict JSON validation, so you can use `sonic_rs::to_object_iter_unchecked` for replacement. Refer to the example [iterator.rs](examples/iterator.rs)
30+
31+
If you need to get multiple fields from JSON, it is recommended to use `get_many`. Reference example: [get_many.rs](examples/get_many.rs)
32+
33+
- Parsing into Golang JsonNumber:
34+
35+
Please use `sonic_rs::RawNumber` directly.
36+
37+
- Parsing into Golang RawMessage:
38+
39+
Please use `sonic_rs::RawValue` directly.

docs/for_Golang_user_zh.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## Golang 迁移 Rust
2+
3+
目前版本:
4+
5+
` sonic-rs = "0.2.4" `
6+
7+
8+
对应 API 参考:
9+
10+
- 解析到 Golang 结构体等强类型:
11+
12+
sonic-go/encoding-json Unmarshal => sonic_rs::from_str/from_slice
13+
14+
sonic-go/encoding-json Marshal => sonic_rs::to_string/to_vec 等
15+
16+
- 解析到 Golang interface{}/any 或 sonic-go AST:
17+
18+
如果是单独的 `interface{}`, 建议使用 `sonic_rs::Document`,性能更优。
19+
20+
如果是 Golang 结构体中的 `interface{}`, 建议将 `interface{}/any` 替换为 `serde_json::Value` 即可。 注意: `sonic_rs::Value``sonic_rs::Document` 暂时不支持嵌入到 Rust 结构体中, 后续会进行支持。
21+
22+
- 使用 gjson/jsonparser 按需解析:
23+
24+
关于 gjson/jsonparser get API:
25+
26+
gjson/jsonparser get API 本身未做严格的JSON 校验,因此可以使用 `sonic_rs::get_unchecked` 进行平替。sonic_rs get API 会返回一个 `Result<LazyValue>`, `LazyValue` 可以用 `as_bool, as_str`等将 JSON 进一步***解析成对应的类型**, 如果需要拿到原始的raw JSON, ***不做解析***,请使用 `as_raw_str, as_raw_slice` 等 API. 参考例子: [get_from.rs](examples/get_from.rs)
27+
28+
29+
关于 jsonparser `ArrayEach``ObjectEach` API:
30+
31+
gjson/jsonparser get API 本身未做严格的JSON 校验,因此可以使用 `sonic_rs::to_object_iter_unchecked` 等进行平替。参考例子 [iterator.rs](examples/iterator.rs)
32+
33+
34+
如果需要从 JSON 中拿到多个字段,推荐使用 `get_many`. 参考例子: [get_many.rs](examples/get_many.rs)
35+
36+
37+
- 解析到 Golang JsonNumber:
38+
39+
请直接使用 `sonic_rs::RawNumber`
40+
41+
- 解析到 Golang RawMessage:
42+
43+
请直接使用 `sonic_rs::RawValue`
44+
45+
46+
47+
48+
49+
50+
51+

examples/get_many.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use sonic_rs::pointer;
2+
use sonic_rs::PointerNode;
3+
4+
fn main() {
5+
let json = r#"
6+
{"u": 123, "a": {"b" : {"c": [null, "found"]}}}"#;
7+
8+
// build a pointer tree, representing multile json path
9+
let mut tree = sonic_rs::PointerTree::new();
10+
11+
tree.add_path(&["u"]);
12+
tree.add_path(&pointer!["a", "b", "c", 1]);
13+
14+
let nodes = unsafe { sonic_rs::get_many_unchecked(json, &tree) };
15+
16+
// the node order is as the order of `add_path`
17+
for val in nodes.unwrap() {
18+
println!("{}", val.as_raw_str());
19+
// 123
20+
// "found"
21+
}
22+
}

examples/iterator.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use bytes::Bytes;
2-
use sonic_rs::{to_array_iter, JsonValue};
2+
use faststr::FastStr;
3+
use sonic_rs::{to_array_iter, to_object_iter_unchecked, JsonValue};
34

45
fn main() {
56
let json = Bytes::from(r#"[1, 2, 3, 4, 5, 6]"#);
@@ -11,6 +12,8 @@ fn main() {
1112
let json = Bytes::from(r#"[1, 2, 3, 4, 5, 6"#);
1213
let iter = to_array_iter(&json);
1314
for elem in iter {
15+
// do something for each elem
16+
1417
// deal with errors when invalid json
1518
if elem.is_err() {
1619
assert_eq!(
@@ -19,4 +22,24 @@ fn main() {
1922
);
2023
}
2124
}
25+
26+
let json = FastStr::from(r#"{"a": null, "b":[1, 2, 3]}"#);
27+
let iter = unsafe { to_object_iter_unchecked(&json) };
28+
for ret in iter {
29+
// deal with errors
30+
if ret.is_err() {
31+
println!("{}", ret.unwrap_err());
32+
return;
33+
}
34+
35+
let (k, v) = ret.unwrap();
36+
if k == "a" {
37+
assert!(v.is_null());
38+
} else if k == "b" {
39+
let iter = to_array_iter(v.as_raw_str());
40+
for (i, v) in iter.enumerate() {
41+
assert_eq!(i + 1, v.as_u64().unwrap() as usize);
42+
}
43+
}
44+
}
2245
}

src/pointer/tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl PointerTree {
2222

2323
/// we build tree and return value according by the order of path.
2424
/// Allow the repeated path.
25-
pub fn add_path<Path: Iterator>(&mut self, path: Path)
25+
pub fn add_path<Path: IntoIterator>(&mut self, path: Path)
2626
where
2727
Path::Item: PointerTrait,
2828
{

0 commit comments

Comments
 (0)